实例介绍
【实例截图】
【核心代码】
//======================================================================== //木沙江·麦提热依木 //20112601106 //通讯录 //2012.7.8 //======================================================================= #include<iostream> #include<string> #include<fstream> #include<iomanip> using namespace std; //======================================================================= struct man // 构造函数部分 { char name[10]; char postcode[10]; char email[10]; char add[20]; char tel[10]; int num; }m[100]; //======================================================================= int n=100; //构造函数初始化 static int b; void sort(); void search(); void error() {cout<<"........错误!!请从新输入:"<<endl;cout<<"\n";} //====================================================================== //**********************数据输入部分*********************************************** void data_input() //定义函数作用功能 { int a; cout<<"请输入通讯信息,依次输入:序号\n姓名\n邮编\nemail\n地址\n电话号码\n输入0结束"<<endl; for(a=0;a<n;a ) { cin>>m[a].num; if(m[a].num==0) { b=a; break; } cin>>m[a].name>>m[a].postcode>>m[a].email>>m[a].add>>m[a].tel; } system("pause"); } //========================================================================= //************************************数据显示部分********************************************* void data_show() { int i; cout<<setiosflags(ios::left)<<setw(2)<<"序号"<<" "<<setw(15)<<"姓名"<<" "<<setw(20)<<"邮编"<<" "<<setw(12)<<"E-mail"<<" "<<setw(15)<<"地址"<<" "<<setw(20)<<"电话"<<endl; for(i=0;i<n;i ) { cout<<setiosflags(ios::left)<<setw(5)<<m[i].num<<" "<<setw(15)<<m[i].name<<" "<<setw(20)<<m[i].postcode<<" "<<setw(12)<<m[i].email<<" "<<setw(15)<<m[i].add<<" "<<setw(20)<<m[i].tel<<endl; if(m[i].num==0)break; } } //======================================================================== //***************************删除函数********************************************** void dalete() { cout<<"请选择您要删除人的序号:"; int a; cin>>a; for(int i=a-1;i<n;i ) { if(a==m[i].num) { int j; for(j=i;j<n;j ) { strcpy(m[j].name,m[j 1].name); strcpy(m[j].postcode,m[j 1].postcode); strcpy(m[j].email,m[j 1].email); strcpy(m[j].add,m[j 1].add); strcpy(m[j].tel,m[j 1].tel); } n--; data_show(); cout<<"删除成功"<<endl; break; } else { error(); dalete(); } } } //========================================================================= void search1() //函数搜索部分 { data_show(); cout<<"请输入您要查找的序号:"<<endl; int a; cin>>a; int flag=0; for(int i=0;i<n;i ) { if(a==m[i].num) { cout<<"(1).name:"; cout<<m[i].name<<endl; cout<<"(2).postcode:"; cout<<m[i].postcode<<endl; cout<<"(3).email:"; cout<<m[i].email<<endl; cout<<"(4).add:"; cout<<m[i].add<<endl; cout<<"(5).telephone:"; cout<<m[i].tel<<endl; cout<<endl; flag=1; search(); } } if(flag==0) cout<<"---抱歉没有您要查询的信息---"<<endl; } //======================================================================== void search2() // 函数搜索部分 //按照名字搜索通讯录里的相关数据 { data_show(); cout<<"请输入您要查找的姓名:"<<endl; string a; cin>>a; int flag=0; for(int i=0;i<n;i ) { if(a==m[i].name) { cout<<"(1).name:"; cout<<m[i].name<<endl; cout<<"(2).postcode:"; cout<<m[i].postcode<<endl; cout<<"(3).email:"; cout<<m[i].email<<endl; cout<<"(4).add:"; cout<<m[i].add<<endl; cout<<"(5).telephone:"; cout<<m[i].tel<<endl; cout<<endl; flag=1;search(); } } if(flag==0) cout<<"---抱歉没有您要查询的信息---"<<endl; } //========================================================================= void search3() { data_show(); cout<<"请输入您要查找的郵編:"<<endl; string a; cin>>a; int flag=0; for(int i=0;i<n;i ) { if(a==m[i].postcode) { cout<<"(1).name:"; cout<<m[i].name<<endl; cout<<"(2).postcode:"; cout<<m[i].postcode<<endl; cout<<"(3).email:"; cout<<m[i].email<<endl; cout<<"(4).add:"; cout<<m[i].add<<endl; cout<<"(5).telephone:"; cout<<m[i].tel<<endl; cout<<endl; flag=1;search(); } } if(flag==0) cout<<"---抱歉没有您要查询的信息---"<<endl; } //============================================================================ void search4() { data_show(); cout<<"请输入您要查找的e_mail:"<<endl; string a; cin>>a; int flag=0; for(int i=0;i<n;i ) { if(a==m[i].email) { cout<<"(1).name:"; cout<<m[i].name<<endl; cout<<"(2).postcode:"; cout<<m[i].postcode<<endl; cout<<"(3).email:"; cout<<m[i].email<<endl; cout<<"(4).add:"; cout<<m[i].add<<endl; cout<<"(5).telephone:"; cout<<m[i].tel<<endl; cout<<endl; flag=1;search(); } } if(flag==0) cout<<"---抱歉没有您要查询的信息---"<<endl; } //========================================================================= void search5() { data_show(); cout<<"请输入您要查找的地址:"<<endl; string a; cin>>a; int flag=0; for(int i=0;i<n;i ) { if(a==m[i].add) { cout<<"(1).name:"; cout<<m[i].name<<endl; cout<<"(2).postcode:"; cout<<m[i].postcode<<endl; cout<<"(3).email:"; cout<<m[i].email<<endl; cout<<"(4).add:"; cout<<m[i].add<<endl; cout<<"(5).telephone:"; cout<<m[i].tel<<endl; cout<<endl; flag=1;search(); } } if(flag==0) cout<<"---抱歉没有您要查询的信息---"<<endl; } //========================================================================= void search6() { data_show(); cout<<"请输入您要查找的电话:"<<endl; string a; cin>>a; int flag=0; for(int i=0;i<n;i ) { if(a==m[i].tel) { cout<<"(1).name:"; cout<<m[i].name<<endl; cout<<"(2).postcode:"; cout<<m[i].postcode<<endl; cout<<"(3).email:"; cout<<m[i].email<<endl; cout<<"(4).add:"; cout<<m[i].add<<endl; cout<<"(5).telephone:"; cout<<m[i].tel<<endl; cout<<endl; flag=1; search(); } } if(flag==0) cout<<"---抱歉没有您要查询的信息---"<<endl; } //============================================================================= void search() { cout<<"(1)序号(2)姓名(3)邮编(4)e_mail(5)地址(6)电话(7)结束"<<endl; cout<<"请输入选项:"; int r; cin>>r; switch(r) { case 1:search1();break; case 2:search2();break; case 3:search3();break; case 4:search4();break; case 5:search5();break; case 6:search6();break; case 7:cout<<"-----GOOD LUCK-----"<<endl;return;break; default:error(); } } //============================================================================ void compile() { data_show(); cout<<"请选择您要编辑的信息序号:"; int i; cin>>i; i--; if(i>=0&&i<n) { for(;;) { cout<<"number:"<<m[i].num<<endl; cout<<"(1).name:"; cout<<m[i].name<<endl; cout<<"(2).postcode:"; cout<<m[i].postcode<<endl; cout<<"(3).email:"; cout<<m[i].email<<endl; cout<<"(4).address:"; cout<<m[i].add<<endl;cout<<"(5).telephone:"; cout<<m[i].tel<<endl;cout<<"(6).exit"<<endl; cout<<endl; cout<<"请输入选项:"; int r; cin>>r; switch(r) { case 1:cout<<"name:";cin>>m[i].name;break; case 2:cout<<"postcode:"; cin>>m[i].postcode;break; case 3:cout<<"birthday:"; cin>>m[i].email; break; case 4:cout<<"address:"; cin>>m[i].add;break; case 5:cout<<"telephone:"; cin>>m[i].tel;break; case 6:cout<<"-----修改成功-----"<<endl;return;break; default:error(); } } } else { error(); compile(); } } //========================================================================= void sort1() { int i,j; char temp1[20],temp2[20],temp3[20],temp4[20],temp5[20]; for(i=0;i<b-1;i ) { for(j=0;j<b-i-1;j ) if((strcmp(m[j].name,m[j 1].name))>0) { strcpy(temp1,m[j].name); strcpy(temp2,m[j].postcode); strcpy(temp3,m[j].email); strcpy(temp4,m[j].add); strcpy(temp5,m[j].tel); strcpy(m[j].name,m[j 1].name); strcpy(m[j].postcode,m[j 1].postcode); strcpy(m[j].email,m[j 1].email); strcpy(m[j].add,m[j 1].add); strcpy(m[j].tel,m[j 1].tel); strcpy(m[j 1].name,temp1); strcpy(m[j 1].postcode,temp2); strcpy(m[j 1].email,temp3); strcpy(m[j 1].add,temp4); strcpy(m[j 1].tel,temp5); } } data_show(); cout<<"sort sucess!"<<endl; sort(); } //========================================================================== void sort2() { int i,j; char temp1[20],temp2[20],temp3[20],temp4[20],temp5[20]; for(i=0;i<b-1;i ) { for(j=0;j<b-i-1;j ) if((strcmp(m[j].postcode,m[j 1].postcode))>0) { strcpy(temp1,m[j].name); strcpy(temp2,m[j].postcode); strcpy(temp3,m[j].email); strcpy(temp4,m[j].add); strcpy(temp5,m[j].tel); strcpy(m[j].name,m[j 1].name); strcpy(m[j].postcode,m[j 1].postcode); strcpy(m[j].email,m[j 1].email); strcpy(m[j].add,m[j 1].add); strcpy(m[j].tel,m[j 1].tel); strcpy(m[j 1].name,temp1); strcpy(m[j 1].postcode,temp2); strcpy(m[j 1].email,temp3); strcpy(m[j 1].add,temp4); strcpy(m[j 1].tel,temp5); } } data_show(); cout<<"sort sucess!"<<endl; sort(); } //============================================================================ void sort3() { int i,j; char temp1[20],temp2[20],temp3[20],temp4[20],temp5[20]; for(i=0;i<b-1;i ) { for(j=0;j<b-i-1;j ) if((strcmp(m[j].email,m[j 1].email))>0) { strcpy(temp1,m[j].name); strcpy(temp2,m[j].postcode); strcpy(temp3,m[j].email); strcpy(temp4,m[j].add); strcpy(temp5,m[j].tel); strcpy(m[j].name,m[j 1].name); strcpy(m[j].postcode,m[j 1].postcode); strcpy(m[j].email,m[j 1].email); strcpy(m[j].add,m[j 1].add); strcpy(m[j].tel,m[j 1].tel); strcpy(m[j 1].name,temp1); strcpy(m[j 1].postcode,temp2); strcpy(m[j 1].email,temp3); strcpy(m[j 1].add,temp4); strcpy(m[j 1].tel,temp5); } } data_show(); cout<<"sort sucess!"<<endl; sort(); } //=========================================================================== void sort4() { int i,j; char temp1[20],temp2[20],temp3[20],temp4[20],temp5[20]; for(i=0;i<b-1;i ) { for(j=0;j<b-i-1;j ) if((strcmp(m[j].add,m[j 1].add))>0) { strcpy(temp1,m[j].name); strcpy(temp2,m[j].postcode); strcpy(temp3,m[j].email); strcpy(temp4,m[j].add); strcpy(temp5,m[j].tel); strcpy(m[j].name,m[j 1].name); strcpy(m[j].postcode,m[j 1].postcode); strcpy(m[j].email,m[j 1].email); strcpy(m[j].add,m[j 1].add); strcpy(m[j].tel,m[j 1].tel); strcpy(m[j 1].name,temp1); strcpy(m[j 1].postcode,temp2); strcpy(m[j 1].email,temp3); strcpy(m[j 1].add,temp4); strcpy(m[j 1].tel,temp5); } } data_show(); cout<<"sort sucess!"<<endl; sort(); } //========================================================================== void sort5() { int i,j; char temp1[20],temp2[20],temp3[20],temp4[20],temp5[20]; for(i=0;i<b-1;i ) { for(j=0;j<b-i-1;j ) if((strcmp(m[j].tel,m[j 1].tel))>0) { strcpy(temp1,m[j].name); strcpy(temp2,m[j].postcode); strcpy(temp3,m[j].email); strcpy(temp4,m[j].add); strcpy(temp5,m[j].tel); strcpy(m[j].name,m[j 1].name); strcpy(m[j].postcode,m[j 1].postcode); strcpy(m[j].email,m[j 1].email); strcpy(m[j].add,m[j 1].add); strcpy(m[j].tel,m[j 1].tel); strcpy(m[j 1].name,temp1); strcpy(m[j 1].postcode,temp2); strcpy(m[j 1].email,temp3); strcpy(m[j 1].add,temp4); strcpy(m[j 1].tel,temp5); } } data_show(); cout<<"sort sucess!"<<endl; sort(); } //=============================================================================== void sort() { cout<<"(1)姓名(2)邮编(3)e_mail(4)地址(5)电话(6)结束"<<endl; cout<<"请输入选项:"; int r; cin>>r; switch(r) { case 1:sort1();break; case 2:sort2();break; case 3:sort3();break; case 4:sort4();break; case 5:sort5();break; case 6:cout<<"-----GOOD LUCK-----"<<endl;return;break; default:error(); } } //========================================================================= void save() { int i; ofstream outfile("1.txt",ios::out); if(!outfile) { cerr<<"open 1.txt error!"<<endl; exit(1); } for(i=0;i<b;i ) { outfile<<setiosflags(ios::left)<<setw(5)<<m[i].num<<" "<<setw(15)<<m[i].name<<" "<<setw(20)<<m[i].postcode<<" "<<setw(12)<<m[i].email<<" "<<setw(15)<<m[i].add<<" "<<setw(11)<<m[i].tel<<endl; } outfile.close(); cout<<endl; cout<<"资料已保存"<<endl; } //========================================================================== void load() { ifstream infile("1.txt",ios::out); if(!infile) { cerr<<"open 1.txt error!"<<endl; exit(1); } for(int i=0; i < 100; i ) { infile>>m[i].num; infile>>m[i].name; infile>>m[i].postcode; infile>>m[i].email; infile>>m[i].add; infile>>m[i].tel; } } //========================================================================= void main() { cout<<"*******************************************************\n"; cout<<"-----------------通讯录管理系统---------------------\n"; cout<<"功能菜单:\n"; cout<<"1.浏览通讯信息请选择\n"; cout<<"2.创建通讯信息请选择\n"; cout<<"3.删除通讯信息请选择\n"; cout<<"4.查询通讯信息请选择\n"; cout<<"5.编辑通信信息请选择\n"; cout<<"6.各種排序请选择\n"; cout<<"7.将通讯录写入文件请选择\n"; cout<<"8.从文件读入通讯录请选择\n"; cout<<"9.退出请选择\n"; cout<<"(注:最大储存量为100人)\n"; cout<<"***************************************************\n"; cout<<endl; for(;;) { int i; cout<<"请输入您要选择的选项:"; cin>>i; cout<<endl; if(i>0&&i<=9){switch(i) { case 1:data_show();continue; case 2:data_input();continue; case 3:dalete();continue; case 4:search();continue; case 5:compile();continue; case 6:sort();continue; case 7:save();continue; case 8:load();continue; case 9:cout<<"--------------------谢谢使用--------------------\n";exit(0); default:break;} cout<<endl;} else error(); } } /* ———————结束————————————————————*/
标签: 源码
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论