1、课程设计个人通讯录/主函数#include #include #include #include struct record char id11; char name20; char company100; char phone20; char mobile_phone15; char classif100; char e_mail30; char QQ12;student500; int num=0;/外部变量num为文件中的纪录数FILE *fp; void mainmenu();/主菜单void newrecord();/新添纪录void searchmenu();/查询菜单void s
2、earch_by_id();/按序号号查询void search_by_name();/按姓名查询void search_by_phone();/按电话查询void deletemenu();/删除菜单void delete_all();/删除所有void delete_one();/删除单个void change();/修改菜单void change_by_phone();/按电话修改void change_by_id();/按序号修改void change_by_name();/按姓名修改void show_all();/显示所有void readfromfile();/读取文件void
3、writetofile();/写入文件void delete_by_id();/按学号删除void delete_by_name();/按姓名删除void delete_by_phone();/按电话删除void main() readfromfile(); while (1) mainmenu(); /从文件导入void readfromfile() if(fp=fopen(student.bin,rb)=NULL) printf(nt通讯录文件不存在); if (fp=fopen(student.bin,wb)=NULL) printf(nt建立失败); exit(0); else pri
4、ntf(nt通讯录文件已建立); printf(nt按任意键进入主菜单); getch(); return; exit(0); fseek(fp,0,2); /*文件位置指针移动到文件末尾*/ if (ftell(fp)0) /*文件不为空*/ rewind(fp); /*文件位置指针移动到文件开始位置*/ for (num=0;!feof(fp) & fread(&studentnum,sizeof(struct record),1,fp);num+); printf(nt文件导入成功); printf(nt按任意键返回主菜单); getch(); return; printf(nt文件导入
5、成功); printf(nt通讯录文件中无任何纪录); printf(nt按任意键返回主菜单); getch(); return; /主菜单void mainmenu() char choic; system(cls); printf(nt* 主菜单*); printf(nt* 1-新添纪录2-查询菜单*); printf(nt* 3-删除菜单4-修改菜单*); printf(nt* 5-保存退出6-不保存退出*); printf(nt* *); printf(nt*); printf(nt请选择:); choic=getch(); switch (choic) case 1:newrecor
6、d();break; case 2:searchmenu();break; case 3:deletemenu();break; case 4:change();break; case 5:writetofile();break; case 6:exit(0); default:mainmenu(); /写入文件void writetofile() int i; if (fp=fopen(student.bin,wb)=NULL) printf(nt文件打开失败); for (i=0;inum;i+) if (fwrite(&studenti,sizeof(struct record),1,f
7、p)!=1) printf(nt写入文件错误!n); fclose(fp); printf(nt通讯录文件已保存); printf(nt按任意键退出程序nt); exit(0); /添加纪录void newrecord() printf(nt* 请输入通讯信息*n); printf(nt输入序号:); scanf(%s,&studentnum.id); printf(nt输入姓名:); scanf(%s,&studentnum.name); printf(nt输入电话号码:); scanf(%s,&studentnum.phone); printf(nt输入e-mail:); scanf(%s
8、,&studentnum.e_mail); printf(nt输入QQ号码:); scanf(%s,&studentnum.QQ); printf(nt公司:%s); scanf(%s,&pany); printf(nt移动电话:%s); scanf(%s,&studentnum.mobile_phone); printf(nt输入分类:); scanf(%s,&studentnum.classif); num+; printf(nt是否继续添加?(Y/N):); if (getch()=y) newrecord(); return; /查询菜单void searchmenu() char c
9、hoic; system(cls); printf(nt* 查询菜单*); printf(nt* 1-显示所有2-按序号查询*); printf(nt* 3-按姓名查询-按电话查询*); printf(nt* 5-返回主菜单*); printf(nt*); printf(nt请选择:); choic=getch(); switch (choic) case 1:show_all();break; case 2:search_by_id();break; case 3:search_by_name();break; case 4:search_by_phone();break; case 5:m
10、ainmenu();break; /显示所有void show_all() int i; system(cls); if(num!=0) printf(ntt* 以下为通讯录所有信息*); for (i=0;inum;i+) printf(nt序号:%s,studenti.id); printf(nt姓名:%s,studenti.name); printf(nt电话:%s,studenti.phone); printf(nte-mail:%s,studenti.e_mail); printf(ntQQ: %s,studenti.QQ); printf(nt公司:%s,pany); printf
11、(nt移动电话:%s,studenti.mobile_phone); printf(nt分类:%s,studenti.classif); printf(t); if (i+1num) printf(nt_); system(pause); printf(nt*); else printf(nt通讯录中无任何纪录); printf(ntt按任意键返回主菜单:); getch(); return; /按序号查询void search_by_id() int mark=0; int i; int a=0; printf(nt* 按序号查找*n); char id11; printf(nt请输入序号:
12、); scanf(%s,id); for(i=0;inum;i+) if (strcmp(studenti.id,id)=0) printf(nt* 以下是您查找的通讯信息*); printf(nt序号:%s,studenti.id); printf(nt姓名:%s,studenti.name); printf(nt电话:%s,studenti.phone); printf(nte-mail:%s,studenti.e_mail); printf(ntQQ: %s,studenti.QQ); printf(nt公司:%s,pany); printf(nt移动电话:%s,studenti.mob
13、ile_phone); printf(nt分类:%s,studenti.classif); printf(nt*); printf(nt按任意键返回主菜单:); mark+; getch(); return; if (mark=0) printf(nt没有改序号的信息); printf(nt按任意键返回主菜单); getch(); return; /按姓名查询void search_by_name() int mark=0; int i; int a=0; printf(nt* 按姓名查找*); char name20; printf(nt请输入姓名:); scanf(%s,name); fo
14、r(i=a;inum;i+) if (strcmp(studenti.name,name)=0) printf(nt* 以下是您查找的通讯信息*); printf(nt序号:%s,studenti.id); printf(nt姓名:%s,studenti.name); printf(nt电话:%s,studenti.phone); printf(nte-mail:%s,studenti.e_mail); printf(ntQQ: %s,studenti.QQ); printf(nt公司:%s,pany); printf(nt移动电话:%s,studenti.mobile_phone); pri
15、ntf(nt分类:%s,studenti.classif); printf(nt*); mark+; if (i+1)num) printf(nt是否继续查找相同名字的通讯信息:(y/n); if (getch()=y) a=i; continue; else return; else printf(nt按任意键返回主菜单); getch(); return; if(mark!=0) printf(nt没有相同姓名的通讯纪录); printf(nt按任意键返回主菜单); getch(); return; else printf(nt没有相同姓名的通讯纪录); printf(nt按任意键返回主菜
16、单); getch(); return; /按电话查询void search_by_phone() int mark=0; int i; int a=0; printf(nt* 按电话查找*); char phone10; printf(nt请输入电话:); scanf(%s,phone); for(i=0;inum;i+) if (strcmp(studenti.phone,phone)=0) printf(nt* 以下是您查找的通讯信息*); printf(nt序号:%s,studenti.id); printf(nt姓名:%s,studenti.name); printf(nt电话:%s
17、,studenti.phone); printf(nte-mail:%s,studenti.e_mail); printf(ntQQ: %s,studenti.QQ); printf(nt公司:%s,pany); printf(nt移动电话:%s,studenti.mobile_phone); printf(nt分类:%s,studenti.classif); printf(nt*); printf(nt按任意键返回主菜单:); mark+; getch(); return; if (mark=0) printf(nt没有改通讯的信息); printf(nt按任意键返回主菜单); getch(
18、); return; /删除菜单void deletemenu() char choic; if(num=0) printf(nt对不起,文件中无任何纪录); printf(nt按任意键返回主菜单); getch(); return; system(cls); printf(nt* 删除菜单*); printf(nt* 1-删除所有2-删除单个*); printf(nt* 3-返回主菜单*); printf(nt*); printf(nt请选择:); choic=getch(); switch (choic) case 1:delete_all();break; case 2:delete_o
19、ne();break; case 3:mainmenu();break; default:mainmenu();break; /删除所有void delete_all() printf(nt确认删除?(y/n); if (getch()=y) fclose(fp); if (fp=fopen(student.bin,wb)=NULL) printf(nt不能打开文件,删除失败); readfromfile(); num=0; printf(nt纪录已删除,按任意键返回主菜单); getch(); return; else return; /删除单个void delete_one() char
20、choic; printf(nt1-按序号删除2-按姓名删除3-按电话删除4-返回主菜单); printf(nt请选择:); choic=getch(); switch (choic) case 1:delete_by_id();break; case 2:delete_by_name();break; case 3:delete_by_phone();break; case 4:mainmenu();break; /按序号删除void delete_by_id() int i,j; int deletemark=0; char id11; printf(nt请输入要删除通讯的序号:); sc
21、anf(%s,id); if(num=0) printf(nt对不起,文件中无任何纪录); printf(nt按任意键返回主菜单); getch(); return; for (i=0;inum;i+) if (strcmp(studenti.id,id)=NULL) printf(nt以下是您要删除的通讯纪录:); printf(nt序号:%s,studenti.id); printf(nt姓名:%s,studenti.name); printf(nt电话:%s,studenti.phone); printf(nte-mail:%s,studenti.e_mail); printf(ntQQ: %s,studenti.QQ); printf(nt公司:%s,pany); printf(nt移动电话:%s,studenti.mobile_phone); printf(nt分类:%s,studenti.classif); printf(nt是否删除?(y/n); if (getch()=y) for (j=i;jnum-1;j+) /*纪录移动,从stud数组中删除之*/ studentj=studentj+1; num-; del
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1