#include #include #include struct person { char name[20]; char tel1[12]; char tel2[12]; char email[20]; char addr[20]; }; char filename[20]; FILE *fp; void menu(); void creat(); void delete(); void xiugai(); void output(); void search(); void search1(); void search2(); void append(); main() { creat(); menu(); return 0; } void menu() { int m; while(1) { printf("\n\t\t*************欢迎来到【通讯录】************\n\n"); printf("\n\t\t***********添加, 请按1***********"); printf("\n\t\t***********查找, 请按2***********"); printf("\n\t\t***********修改, 请按3***********"); printf("\n\t\t***********删除, 请按4***********"); printf("\n\t\t***********显示全部联系人,请按5***********"); printf("\n\t\t***********退出, 请按0***********\n"); printf("\n\t\t*******************************************\n\n"); printf("Please select(0--5):"); scanf("%d",&m); if(m>=0&&m<=4) { switch(m) { case 1: append(); break; case 2: search(); break; case 3: xiugai(); break; case 4: delete(); break; case 5: output(); break; case 0: exit(0); } printf("\n\n操作完毕,请再次选择!"); } else printf("\n\n选择错误,请再次选择!"); } } void creat() { printf("\n请确定电话薄文件名:"); scanf("%s",filename); if((fp=fopen(filename,"at+"))==NULL) { printf("\n不能建电话薄录!"); exit(0); } fprintf(fp,"%-20s%-12s%-12s%-20s%-20s\n","姓名","电话号码1","电话号码2","电子邮箱","籍贯"); fclose(fp); } void output() { struct person one; if((fp=fopen(filename,"r"))==NULL) { printf("\n不能打开通讯录!"); exit(0); } printf("\n\n%20s\n","通 讯 录"); while(!feof(fp)) { fscanf(fp,"%s%s%s%s%s\n",one.name,one.tel1,one.tel2,one.email,one.addr); printf("%-20s%-12s%-12s%-20s%-20s",one.name,one.tel1,one.tel2,one.email,one.addr); } fclose(fp); } void append() { struct person one; search2(); if((fp=fopen(filename,"a"))==NULL) { printf("\n不能打开通讯录!"); exit(0); } printf("\n请输入添加的姓名、电话号码1、电话号码2、电子邮箱及籍贯(用空格隔开):\n"); scanf("%s%s%s%s%s",one.name,one.tel1,one.tel2,one.email,one.addr); fprintf(fp,"%-20s%-12s%-12s%-20s%-20s\n",one.name,one.tel,one.addr); fclose(fp); } void search() { int n; printf("\n\t\t*******************************"); printf("\n\t\t*****按姓名查找, 请按1*****"); printf("\n\t\t*****按电话号码查找,请按2*****"); printf("\n\t\t*****返回主菜单, 请按0*****"); printf("\n\t\t*******************************\n"); printf("Please select(0--2):"); scanf("%d",n); switch(n) { case 1: search1();break; case 2: search2();break; case 0: menu();break; default: printf("\n\n选择错误,请再次选择!"); } } void search1() { int k=0; char namekey[20]; struct person one; printf("\n请输入姓名:"); scanf("%s",namekey); if((fp=fopen(filename,"rb"))==NULL) { printf("\n不能打开电话薄!"); exit(0); } while(!feof(fp)) { fscanf(fp,"%s%s%s%s%s\n",one.name,one.tel1,one.tel2,one.email,one.addr); if(!strcmp(namekey,one.name)) { printf("\n\n已查到,记录为:"); printf("\n%-20s%-12s%-12s%-20s%-20s",one.name,one.tel1,one.tel2,one.email,one.addr); k=1; } } if(!k) printf("\n\n对不起,电话薄中没有此人的记录。"); fclose(fp); } void search2() { int k=0; char telkey[12]; struct person one; printf("\n请输入电话号码:"); scanf("%s",telkey); if((fp=fopen(filename,"rb"))==NULL) { printf("\n不能打开电话薄!"); exit(0); } while(!feof(fp)) { fscanf(fp,"%s%s%s%s%s\n",one.name,one.tel1,one.tel2,one.email,one.addr); if(!strcmp(telkey,one.tel1)||!strcmp(telkey,one.tel2)) { printf("\n\n已查到,记录为:"); printf("\n%-20s%-12s%-12s%-20s%-20s",one.name,one.tel1,one.tel2,one.email,one.addr); k=1; } } if(!k) printf("\n\n对不起,电话薄中没有此人的记录。"); fclose(fp); } void xiugai() { char name[20]; printf("请输入要修改的联系人的姓名:"); scanf("%s",name); if((fp=fopen(filename,"rb"))==NULL) { printf("\n不能打开电话薄!"); exit(0); } while(!feof(fp)) { fscanf(fp,"%s%s%s%s%s\n",one.name,one.tel1,one.tel2,one.email,one.addr); if(!strcmp(name,one.name)) { printf("\n\n已查到,记录为:"); printf("\n%-20s%-12s%-12s%-20s%-20s",one.name,one.tel1,one.tel2,one.email,one.addr); } } } void delete() { }