#include #include #include FILE *ptr1; typedef struct { char name[20]; char phonenum[20]; int kind; char email[30]; } lm; lm linkman; void add(); void view(); void search(); void sc(); void dial(); void amend(); int main_menu() { char c; do{ system("cls"); printf("\t\t ════════════════\n"); printf("\t\t 手机通讯录小程序 \n"); printf("\t\t ════════════════\n"); printf("\t\t ┌────────────────┐\n"); printf("\t\t │ 1. 新增联系人 2. 全部联系人 │\n"); printf("\t\t │ │\n"); printf("\t\t │ 3. 查找联系人 4. 修改联系人 │\n"); printf("\t\t │ │\n"); printf("\t\t │ 5. 删除联系人 6. 拨号 │\n"); printf("\t\t │ │\n"); printf("\t\t │ 0. 退出程序 │\n"); printf("\t\t └────────────────┘\n"); printf("\t\t请您选择(0-6):"); c=getchar(); }while(c<'0'||c>'6'); return (c-'0'); } main() { while(1) { switch(main_menu()) { case 1: printf("\t\t --------新增联系人--------\n"); add(); break; case 2: printf("\t\t --------全部联系人--------\n"); view(); break; case 3: printf("\t\t --------查找联系人--------\n"); search(); break; case 4: printf("\t\t --------修改联系人--------\n"); amend(); break; case 5: printf("\t\t --------删除联系人---------\n"); sc(); break; case 6: printf("\t\t -----------拨号------------\n"); dial(); break; case 0: printf("感谢使用,再见\n"); system("pause"); exit(0); } } return 0; } void add() { int i=0; ptr1=fopen("storage.sto","rb+"); if(ptr1==NULL) { ptr1=fopen("storage.sto","wb+"); if(ptr1==NULL) { printf("无法建立文件\n"); exit(0); } } rewind(ptr1); while(!feof(ptr1)) { fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email); ++i; } fseek(ptr1,0,SEEK_END); printf("您目前有%d条联系人数据,您还可以新增%d条联系人数据(%d/15)\n",i,15-i,i); if(i==15) { system("pause"); return; } char ch='y'; while(ch!='n'&&ch!='N') { fflush(stdin); ++i; printf("姓名:"); gets(linkman.name); printf("电话号码:"); gets(linkman.phonenum); printf("分类(1 办公类 2 个人类 3 商务类):"); scanf("%d",&linkman.kind); while(linkman.kind<0||linkman.kind>3) { printf("请输入1-3内的数字:"); scanf("%d",&linkman.kind); } printf("电子邮箱:"); fflush(stdin); gets(linkman.email); fprintf(ptr1, "%20s%20s%5d%20s",linkman.name,linkman.phonenum,linkman.kind,linkman.email); if(i<15) printf("\n这是您的第%d条联系人,您可以继续新增%d条联系人(y/n)",i,15-i); else { printf("\n这是您的第%d条联系人,您无法继续新增联系人了(y/n)",i,15-i); system("pause"); return; } fflush(stdin); ch=getchar(); } fclose(ptr1); } void view() { int x,i=0; ptr1=fopen("storage.sto","rb+"); if(ptr1==NULL) { printf("您目前没有联系人的记录\n"); printf("【1】返回主目录\n"); printf("【2】新增联系人\n"); printf("【3】结束程序\n"); scanf("%d",&x); while(x<1||x>3) { printf("请输入1-3内的数字:"); scanf("%d",&x); } switch(x) { case 1:return;break; case 2:add();return;break; case 3:exit(0); } } rewind(ptr1); fflush(stdin); printf("请您选择查看联系人的范围:\n"); printf("【1】所有联系人\n"); printf("【2】办公联系人\n"); printf("【3】个人联系人\n"); printf("【4】商务联系人\n"); while(x<1||x>4) { printf("请输入1-4内的数字:"); scanf("%d",&x); } printf("序号 姓名 手机号码 分类 电子邮件\n"); printf("***************************************************************\n"); if(x==1) { while(!feof(ptr1)) { fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email); i++; //printf("%d %-10s %-10s %d %-10s",i,linkman.name,linkman.phonenum,linkman.kind,linkman.email); if(linkman.kind==1) printf("%d %-10s %-10s 办公类 %-10s",i,linkman.name,linkman.phonenum,linkman.email); if(linkman.kind==2) printf("%d %-10s %-10s 个人类 %-10s",i,linkman.name,linkman.phonenum,linkman.email); if(linkman.kind==3) printf("%d %-10s %-10s 商务类 %-10s",i,linkman.name,linkman.phonenum,linkman.email); printf("\n"); } } else { while(!feof(ptr1)) { fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email); switch(x) { case 2:if(linkman.kind==1)printf("%d %-10s %-10s 办公类 %-10s",++i,linkman.name,linkman.phonenum,linkman.email);break; case 3:if(linkman.kind==2)printf("%d %-10s %-10s 个人类 %-10s",++i,linkman.name,linkman.phonenum,linkman.email);break; case 4:if(linkman.kind==3)printf("%d %-10s %-10s 商务类 %-10s",++i,linkman.name,linkman.phonenum,linkman.email);break; } printf("\n"); } } printf("已经显示全部内容,共%d条\n",i); system("pause"); return; } void search() { int x,i=0,u=0; ptr1=fopen("storage.sto","rb+"); if(ptr1==NULL) { printf("您目前没有联系人的记录\n"); printf("【1】返回主目录\n"); printf("【2】新增联系人\n"); printf("【3】结束程序\n"); scanf("%d",&x); while(x<1||x>3) { printf("请输入1-3内的数字:"); scanf("%d",&x); } switch(x) { case 1:return;break; case 2:void add();break; case 3:exit(0); } } rewind(ptr1); fflush(stdin); printf("您想按照什么关键字搜索呢:\n"); printf("【1】模糊搜索\n"); printf("【2】姓名搜索\n"); printf("【3】手机号码搜索\n"); printf("【4】电子邮件地址搜索\n"); printf("【5】返回主菜单\n"); scanf("%d",&x); while(x<1||x>5) { printf("请输入1-5内的数字:"); scanf("%d",&x); } char keywords[20]; fflush(stdin); printf("请输入关键字:"); gets(keywords); while(!feof(ptr1)) { fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email); if(x==1) { if(keywords[0]==linkman.name[0]&&keywords[1]==linkman.name[1])u=1; } if(x==2) { if(strcmp(keywords,linkman.name)==0)u=1; } if(x==3) { if(strcmp(keywords,linkman.phonenum)==0)u=1; } if(x==4) { if(strcmp(keywords,linkman.email)==0)u=1; } if(u) { switch(linkman.kind) { case 1:printf("%d %-10s%-10s办公类%-10s",++i,linkman.name,linkman.phonenum,linkman.email);break; case 2:printf("%d %-10s%-10s个人类%-10s",++i,linkman.name,linkman.phonenum,linkman.email);break; case 3:printf("%d %-10s%-10s商务类%-10s",++i,linkman.name,linkman.phonenum,linkman.email);break; } } u=0; printf("\n"); } printf("已经显示全部搜索结果,共%d条\n",i); system("pause"); return; } void amend() { int x,i=0; FILE *ptr2; ptr1=fopen("storage.sto","rb+"); if(ptr1==NULL) { printf("您目前没有联系人的记录\n"); printf("【1】返回主目录\n"); printf("【2】新增联系人\n"); printf("【3】结束程序\n"); scanf("%d",&x); while(x<1||x>3) { printf("请输入1-3内的数字:"); scanf("%d",&x); } switch(x) { case 1:return;break; case 2:add();return;break; case 3:exit(0); } } rewind(ptr1); fflush(stdin); printf("序号 姓名 手机号码 分类 电子邮件\n"); printf("***************************************************************\n"); while(!feof(ptr1)) { fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email); i++; //printf("%d %-10s %-10s %d %-10s",i,linkman.name,linkman.phonenum,linkman.kind,linkman.email); if(linkman.kind==1) printf("%d %-10s %-10s 办公类 %-10s",++i,linkman.name,linkman.phonenum,linkman.email); if(linkman.kind==2) printf("%d %-10s %-10s 个人类 %-10s",++i,linkman.name,linkman.phonenum,linkman.email); if(linkman.kind==3) printf("%d %-10s %-10s 商务类 %-10s",++i,linkman.name,linkman.phonenum,linkman.email); printf("\n"); } printf("请从(0-%d)选择要修改的序号:",%i); scanf("%d",&x); while(x<0||x>i) { printf("请从(0-%d)选择要修改的序号:",%i); scanf("%d",&x); } ptr2=fopen("temp","wb+"); if(ptr2==NULL) { printf("ERROR"); exit(0); } return; } void sc() { return; } void dial() { return; }