《c语言 通讯录程序代码》由会员分享,可在线阅读,更多相关《c语言 通讯录程序代码(14页珍藏版)》请在金锄头文库上搜索。
1、c语言 通讯录程序代码#include #include #include #include #include class student public: char num20; char name20; char address40; char tel_num12; char post_code7; char e_mail40; ; class function:public student public: ; /主函数 int main function a; char select1; coutendl; int m=0; void add; void del; void find_nu
2、m; void find_name; void list_num; void list_name; void correct; void format; void show; do cout-通讯录-endl; cout*endl; cout*1.通讯录编辑 2.查询联系人 3.按序排列 4.格式化 5.显示通讯录 6.清屏 7.退出*endl; cout*endl; coutselect1; cout-endl; switch(select1) case1:cout1.添加 2.删除 3.修改 4.返回主菜单endl; char select2; cout请选择:select2; switc
3、h(select2) case 1:a.add; break; break; break; case2:a.del; case3:a.correct; case4:break; default:cout输入错误!endl; break; case2:cout1.姓名查找 2.学号查找 3.返回主菜单endl; char select3; coutselect3; endl; switch(select3) case1:a.find_name; break; case2:a.find_num; break; case3:break; default:cout输入错误!endl; break; c
4、ase3:cout1.按学号排序 2.按姓名排序 3.返回主菜单 coutselect4; switch(select4) case1:a.list_num; break; case2:a.list_name; break; case3:break; default:cout输入错误!endl; break; case4:coutselect5; if(select5=Y|select5=y) a.format; break; case5:a.show;break; case6:system(cls);break;/清屏函数 case7:m=1;cout谢谢使用!endl;break; def
5、ault:cout输入错误!endl; while(m!=1); /添加联系人 void function:add int i; couti; student stud140=0; ofstream outfile(学生通讯录.dat,ios:app|ios:binary); if (! outfile) for(int j=0;ji;j+) coutstud1j.num; coutstud1j.name; coutstud1j.address; coutstud1j.tel_num; coutstud1j.post_code; coutstud1j.e_mail; outfile.write
6、(char*)&stud1j,sizeof(stud1j); cerr打开错误,程序将退出!; abort; return 0; outfile.close ; /删除联系人 void function:del student stud240=0; char name20=0; coutname; ifstream infile(学生通讯录.dat,ios:binary); if (!infile) int k=0; while(1) infile.read(char*)&stud2k,sizeof(stud2k); if(infile.eof)break; k+; infile.close;
7、 remove(学生通讯录.dat); ofstream outfile(学生通讯录.dat,ios:binary); if (!outfile) int i=0; while(1) if(ik-1)break;/每删除一项就会减少一组数据,所以是k-1 if(!strcmp(stud2i.name,name) cerr打开错误,程序将退出!; abort; cerrk-1)break; outfile.write(char*)&stud2i,sizeof(stud2i); i+; outfile.close; if(k-1=0) remove(学生通讯录.dat); /修改联系人 void
8、function:correct student stud340=0; ifstream infile(学生通讯录.dat,ios:binary); int k=0; while(1) if(infile.eof)break; infile.read(char*)&stud3k,sizeof(stud3k); k+; infile.close; char name20=0; coutname; int x; for(int i=0;ik;i+) char select; if(!strcmp(stud3i.name,name) x=i; cout1.修改姓名 2.修改学号 3.修改地址 4.修
9、改邮编 5.修改电话号码 coutselect; case1:coutname1; strcpy(stud3x.name,name1); cout修改成功num1; strcpy(stud3x.num,num1); cout修改成功address1; strcpy(stud3x.address,address1); cout修改成功post_code1; strcpy(stud3x.post_code,post_code1); char name120=0; char num120=0; char address140=0; char post_code17=0; char tel_num12
10、0=0; char e_mail140=0; 6.修改E_MAILendl; switch(select) case2:cout输入要修改的学号:; case3:cout输入要修改的地址:; case4:cout输入要修改的邮编:; cout修改成功endl; break; case5:couttel_num1; strcpy(stud3x.tel_num,tel_num1); break; case6:coute_mail1; strcpy(stud3x.e_mail,e_mail1); cout修改成功endl; break; default:coutk-2)break; outfile.write(char*)&stud3l,sizeof(stud3l); l+; outfile.close; /按学号查询 void function:find_num student stud440=0; ifstream infile(学生通讯录.dat,ios:binary); if(infile.peek=EOF)