/****************************电子通讯录******************************************/
/*纯C版本,TC运行*/
/**************************作者:CHLAWS****************************************/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define LEN 10
typedef struct {
char name[8];
char tel[17];
char Address[50];
}person;
char filename[20]="telephone.dat";
FILE *fp;
void input();
void del();
void append();
void search();
void putout();
void Zhu_d();
void quit();
void modify();
void Zhu_d()
{
/*system("cls");*/
int choise;
char a;
do{
/*system("cls"); */
system("cls");
fflush(stdin);
printf(" ******************************************************************************** ");
printf(" welcome use this telephone book! ");
printf(" ******************************************************************************** ");
printf(" ");
printf(" 1:creat and save ");
printf(" 2:delete info ");
printf(" 3:insert and save ");
printf(" 4:search info ");
printf(" 5:putout info ");
printf(" 6:amend info ");
printf(" 7:exit system ");
printf(" please input you choise:");
fflush(stdin);
scanf("%d",&choise);
printf("choise = %d",choise);
switch(choise)
{
case 1: input(); break; /*输入并保存函数*/
case 2: del(); break; /*删除函数*/
case 3: append(); break; /*插入函数*/
case 4: search(); break; /*查找函数*/
case 5: putout(); break; /*输出函数*/
case 6: modify(); break; /*修改函数*/
case 7: quit(); break; /*退出函数*/
default :
system("cls");
printf(" input error,input blank(" ") to afresh do it ");
if((a = getche()) == ' ')
{
Zhu_d();
}
else
{
system("cls");
printf(" input error,you have lase chance to input blank(" ") to afresh do it ");
if((a = getche()) == ' ')
{
Zhu_d();
}
}
}
}while(choise<7 && choise>0);
}
void input()
{
char key[4];
int i=1;
person numbone;
if((fp=fopen(filename,"w")) == NULL)
{
printf(" can not open file! ");
exit(1);
}
system("cls");
/**********************修改******************************/
fprintf(fp,"%-10s%-20s%-50s ","name","telephone","address");
/********************************************************/
do{
printf(" input of%d friend name:",i);
fflush(stdin);
gets(numbone.name);
printf(" input of%d friend telephone:",i);
fflush(stdin);
gets(numbone.tel);
printf(" input of%d friend address:",i);
fflush(stdin);
gets(numbone.Address);
fprintf(fp," %-20s%-20s%-50s ",numbone.name,numbone.tel,numbone.Address);
printf("are you continue input data?(yes/no) ");
fflush(stdin);
gets(key);
i++;
}while(!strcmpi(key,"yes"));
printf(" finish input info ");
fclose(fp);
printf(" press any key to return! ");
getch();
}
void del()
{
char name[20];
person del;
int flag=0;
int m;
long offset;
if((fp=fopen(filename,"r+")) == NULL)
{
printf("file can not open ");
exit(1);
}
system("cls");
fflush(stdin);
printf("input you will delete info's name:");
gets(name);
/*puts(name);*/
rewind(fp);
while(!feof(fp))
{
offset = ftell(fp);
fgets(del.name,strlen(name)+1,fp);
if(strcmpi(name,del.name) == 0)
{
flag = 1;
break;
}
}
if(flag)
{
printf("already find, register: ");
fseek(fp,offset,SEEK_SET);
fscanf(fp,"%s%s%s",del.name,del.tel,del.Address);
printf("name:%s ",del.name);
printf("telephone:%s ",del.tel);
printf("address:%s ",del.Address);
printf("delete press 1,no delete press 0: ");
scanf("%d",&m);
if(m)
{
fseek(fp,offset,SEEK_SET);
fprintf(fp,"%-20s%-20s%-50s","","","");
}
}
else
{
printf("sorry,in data was not find you will delete info! ");
}
printf("work ok! ");
fclose(fp);
printf(" press any key to return ");
getch();
}
void append()
{
person one;
if((fp=fopen(filename,"a+")) == NULL)
{
printf("can not open file! ");
exit(1);
}
system("cls");
printf("input name: ");
fflush(stdin);
gets(one.name);
printf("input telephone: ");
fflush(stdin);
gets(one.tel);
printf("input address: ");
fflush(stdin);
gets(one.Address);
printf("work ok! ");
fprintf(fp,"%-20s%-20s%-50s ",one.name,one.tel,one.Address);
fclose(fp);
printf(" press any key to return ");
getch();
}
void search()
{
char Search_name[20];
person find;
int flag = 0;
if((fp=fopen(filename,"r+")) == NULL)
{
printf("can not open file ");
exit(1);
}
system("cls");
printf("input you will search name: ");
fflush(stdin);
gets(Search_name);
puts(Search_name);
printf("input ok ");
fflush(stdin);
rewind(fp);
while(!feof(fp))
{
fscanf(fp,"%s%s%s",find.name,find.tel,find.Address);
if(strcmpi(Search_name,find.name) == 0)
{
flag = 1;
printf("flag = %d ",flag);
break;
}
}
if(flag)
{
printf(" alread search you input info! ");
printf("name:%-20s telephone:%-20s address:%-50s ",find.name,find.tel,find.Address);
}
else
{
printf("sorry,not find you input data! ");
}
fclose(fp);
printf(" press any key to return ");
getch();
}
void putout()
{
char ch;
if((fp=fopen(filename,"r+")) == NULL)
{
printf("can not open file ");
exit(1);
}
system("cls");
rewind(fp);
ch = fgetc(fp);
while(ch != EOF)
{
putchar(ch);
ch = fgetc(fp);
}
printf(" input ok! ");
printf(" press any to return ");
fclose(fp);
getch();
}
void quit()
{
system("cls");
fflush(stdin);
exit(0);
}
void modify()
{
person amend;
person Search;
int k,flag = 0;
long offset;
if((fp=fopen(filename,"r+"))==NULL)
{
printf("can not open! ");
exit(1);
}
system("cls");
printf("in here you can amend data! ");
printf(" input will amend name,system if find it,will show! ");
printf("amend name:");
fflush(stdin);
gets(amend.name);
while(!feof(fp))
{
offset = ftell(fp);
fgets(Search.name,strlen(amend.name)+1,fp);
if(strcmpi(Search.name,amend.name) == 0)
{
flag = 1;
break;
}
}
if(flag)
{
printf("you input amend was find: ");
fseek(fp,offset,SEEK_SET);
fscanf(fp,"%s%s%s",Search.name,Search.tel,Search.Address);
printf("name:%s ",Search.name);
printf("telephone:%s ",Search.tel);
printf("address:%s ",Search.Address);
printf("delete press1,not press0: ");
fflush(stdin);
scanf("%d",&k);
}
if(k)
{
printf("input new name: ");
fflush(stdin);
gets(amend.name);
printf("input new telephone number: ");
fflush(stdin);
gets(amend.tel);
printf("input new address: ");
fflush(stdin);
gets(amend.Address);
fseek(fp,offset,SEEK_SET);
fprintf(fp,"%-20s%-20s%-50s",amend.name,amend.tel,amend.Address);
}
fclose(fp);
printf(" amend ok! ");
printf(" press any key to return! ");
getch();
return;
}
void main()
{
Zhu_d();
getch();
}
/*纯C版本,TC运行*/
/**************************作者:CHLAWS****************************************/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define LEN 10
typedef struct {
char name[8];
char tel[17];
char Address[50];
}person;
char filename[20]="telephone.dat";
FILE *fp;
void input();
void del();
void append();
void search();
void putout();
void Zhu_d();
void quit();
void modify();
void Zhu_d()
{
/*system("cls");*/
int choise;
char a;
do{
/*system("cls"); */
system("cls");
fflush(stdin);
printf(" ******************************************************************************** ");
printf(" welcome use this telephone book! ");
printf(" ******************************************************************************** ");
printf(" ");
printf(" 1:creat and save ");
printf(" 2:delete info ");
printf(" 3:insert and save ");
printf(" 4:search info ");
printf(" 5:putout info ");
printf(" 6:amend info ");
printf(" 7:exit system ");
printf(" please input you choise:");
fflush(stdin);
scanf("%d",&choise);
printf("choise = %d",choise);
switch(choise)
{
case 1: input(); break; /*输入并保存函数*/
case 2: del(); break; /*删除函数*/
case 3: append(); break; /*插入函数*/
case 4: search(); break; /*查找函数*/
case 5: putout(); break; /*输出函数*/
case 6: modify(); break; /*修改函数*/
case 7: quit(); break; /*退出函数*/
default :
system("cls");
printf(" input error,input blank(" ") to afresh do it ");
if((a = getche()) == ' ')
{
Zhu_d();
}
else
{
system("cls");
printf(" input error,you have lase chance to input blank(" ") to afresh do it ");
if((a = getche()) == ' ')
{
Zhu_d();
}
}
}
}while(choise<7 && choise>0);
}
void input()
{
char key[4];
int i=1;
person numbone;
if((fp=fopen(filename,"w")) == NULL)
{
printf(" can not open file! ");
exit(1);
}
system("cls");
/**********************修改******************************/
fprintf(fp,"%-10s%-20s%-50s ","name","telephone","address");
/********************************************************/
do{
printf(" input of%d friend name:",i);
fflush(stdin);
gets(numbone.name);
printf(" input of%d friend telephone:",i);
fflush(stdin);
gets(numbone.tel);
printf(" input of%d friend address:",i);
fflush(stdin);
gets(numbone.Address);
fprintf(fp," %-20s%-20s%-50s ",numbone.name,numbone.tel,numbone.Address);
printf("are you continue input data?(yes/no) ");
fflush(stdin);
gets(key);
i++;
}while(!strcmpi(key,"yes"));
printf(" finish input info ");
fclose(fp);
printf(" press any key to return! ");
getch();
}
void del()
{
char name[20];
person del;
int flag=0;
int m;
long offset;
if((fp=fopen(filename,"r+")) == NULL)
{
printf("file can not open ");
exit(1);
}
system("cls");
fflush(stdin);
printf("input you will delete info's name:");
gets(name);
/*puts(name);*/
rewind(fp);
while(!feof(fp))
{
offset = ftell(fp);
fgets(del.name,strlen(name)+1,fp);
if(strcmpi(name,del.name) == 0)
{
flag = 1;
break;
}
}
if(flag)
{
printf("already find, register: ");
fseek(fp,offset,SEEK_SET);
fscanf(fp,"%s%s%s",del.name,del.tel,del.Address);
printf("name:%s ",del.name);
printf("telephone:%s ",del.tel);
printf("address:%s ",del.Address);
printf("delete press 1,no delete press 0: ");
scanf("%d",&m);
if(m)
{
fseek(fp,offset,SEEK_SET);
fprintf(fp,"%-20s%-20s%-50s","","","");
}
}
else
{
printf("sorry,in data was not find you will delete info! ");
}
printf("work ok! ");
fclose(fp);
printf(" press any key to return ");
getch();
}
void append()
{
person one;
if((fp=fopen(filename,"a+")) == NULL)
{
printf("can not open file! ");
exit(1);
}
system("cls");
printf("input name: ");
fflush(stdin);
gets(one.name);
printf("input telephone: ");
fflush(stdin);
gets(one.tel);
printf("input address: ");
fflush(stdin);
gets(one.Address);
printf("work ok! ");
fprintf(fp,"%-20s%-20s%-50s ",one.name,one.tel,one.Address);
fclose(fp);
printf(" press any key to return ");
getch();
}
void search()
{
char Search_name[20];
person find;
int flag = 0;
if((fp=fopen(filename,"r+")) == NULL)
{
printf("can not open file ");
exit(1);
}
system("cls");
printf("input you will search name: ");
fflush(stdin);
gets(Search_name);
puts(Search_name);
printf("input ok ");
fflush(stdin);
rewind(fp);
while(!feof(fp))
{
fscanf(fp,"%s%s%s",find.name,find.tel,find.Address);
if(strcmpi(Search_name,find.name) == 0)
{
flag = 1;
printf("flag = %d ",flag);
break;
}
}
if(flag)
{
printf(" alread search you input info! ");
printf("name:%-20s telephone:%-20s address:%-50s ",find.name,find.tel,find.Address);
}
else
{
printf("sorry,not find you input data! ");
}
fclose(fp);
printf(" press any key to return ");
getch();
}
void putout()
{
char ch;
if((fp=fopen(filename,"r+")) == NULL)
{
printf("can not open file ");
exit(1);
}
system("cls");
rewind(fp);
ch = fgetc(fp);
while(ch != EOF)
{
putchar(ch);
ch = fgetc(fp);
}
printf(" input ok! ");
printf(" press any to return ");
fclose(fp);
getch();
}
void quit()
{
system("cls");
fflush(stdin);
exit(0);
}
void modify()
{
person amend;
person Search;
int k,flag = 0;
long offset;
if((fp=fopen(filename,"r+"))==NULL)
{
printf("can not open! ");
exit(1);
}
system("cls");
printf("in here you can amend data! ");
printf(" input will amend name,system if find it,will show! ");
printf("amend name:");
fflush(stdin);
gets(amend.name);
while(!feof(fp))
{
offset = ftell(fp);
fgets(Search.name,strlen(amend.name)+1,fp);
if(strcmpi(Search.name,amend.name) == 0)
{
flag = 1;
break;
}
}
if(flag)
{
printf("you input amend was find: ");
fseek(fp,offset,SEEK_SET);
fscanf(fp,"%s%s%s",Search.name,Search.tel,Search.Address);
printf("name:%s ",Search.name);
printf("telephone:%s ",Search.tel);
printf("address:%s ",Search.Address);
printf("delete press1,not press0: ");
fflush(stdin);
scanf("%d",&k);
}
if(k)
{
printf("input new name: ");
fflush(stdin);
gets(amend.name);
printf("input new telephone number: ");
fflush(stdin);
gets(amend.tel);
printf("input new address: ");
fflush(stdin);
gets(amend.Address);
fseek(fp,offset,SEEK_SET);
fprintf(fp,"%-20s%-20s%-50s",amend.name,amend.tel,amend.Address);
}
fclose(fp);
printf(" amend ok! ");
printf(" press any key to return! ");
getch();
return;
}
void main()
{
Zhu_d();
getch();
}