通讯录程序源码
这应该是算我学习C++后,小试的第一个稍微大点的程序吧。一个简易的通讯录,实现了编辑,查询,保存以及打开的功能。本科时候曾经做过学生信息管理系统,不过那是用纯C。其实在以下的程序代码中,大部分也是C,属于C++的只有类、容器、文件以及string类的知识,对于指针倒没用到。感觉自己在指针这部分很不熟悉,要多练习。希望有兴趣学习C++的朋友可以多扩展一下我的这个程序,功能还是太少的。
#include<string> #include<fstream> #include<iostream> #include<vector> using namespace std; static int n=0; struct student{ string name; string number; string address; string telephone; string post; string mail; }; class information{ private: student st[10]; public: void add(string name, string number,string address,string telephone,string post,string mail); void print(int i) { cout<<"\t\t姓名:"<<st[i].name<<endl; cout<<"\t\t学号:"<<st[i].number<<endl; cout<<"\t\t地址:"<<st[i].address<<endl; cout<<"\t\t电话:"<<st[i].telephone<<endl; cout<<"\t\t邮编:"<<st[i].post<<endl; cout<<"\t\t邮箱:"<<st[i].mail<<endl; } void findname(string name); void findnumber(string number); void correct(string name); void del(string); void save(); void read(); }; void information::add(string name, string number,string address,string telephone,string post,string mail) { static int i=0; st[i].address=address; st[i].mail=mail; st[i].name=name; st[i].number=number; st[i].post=post; st[i].telephone=telephone; i++; n++; } void information::findname(string name) { int x=0; for(int i=0;i<10;i++) { if(st[i].name==name) { print(i); x=1; break; } } if(x==0) cout<<"the man can.t be found in the record!"<<endl; } void information::findnumber(string number) { int x=0; for(int i=0;i<10;i++) { if(st[i].number==number) { print(i); x=1; break; } } if(x==0) cout<<"the student can.t be found in the record!"<<endl; } void information::correct(string name) { string number; string address; string telephone; string post; string mail; int x=0; for(int i=0;i<10;i++) { if(st[i].name==name) { cout<<"\t输入要修改的姓名:"; cin>>name; st[i].name=name; cout<<"\t输入要修改的学号:"; cin>>number; st[i].number=number; cout<<"\t输入要修改的地址:"; cin>>address; st[i].address=address; cout<<"\t输入要修改的电话:"; cin>>telephone; st[i].telephone=telephone; cout<<"\t输入要修改的邮编:"; cin>>post; st[i].post=post; cout<<"\t输入要修改的邮箱:"; cin>>mail; st[i].mail=mail; print(i); x=1; } } if(x==0) cout<<"the man can.t be found in the record"<<endl; } void information::del(string name) { int x=0; for(int i=0;i<10;i++) { if(st[i].name==name) { st[i].address="0"; st[i].mail="0"; st[i].name="0"; st[i].number="0"; st[i].post="0"; st[i].telephone="0"; x=1; print(i); } } if(x==0) cout<<"the student can.t be found in the record"<<endl; } void information::save() { string fileName; second:cout<<"\t输入要保存的文件名:"; cin>>fileName; ofstream outFile(fileName.c_str()); if(!outFile){ cerr<<"\terror:unable to open output file: "<<fileName<<endl; goto second; } for(int i=0;i<n;i++) { outFile << "姓名:"<<st[i].name<<"\t"; outFile << "学号:"<<st[i].number<<"\t"; outFile << "地址:"<<st[i].address<<"\t"; outFile << "电话号码:"<<st[i].telephone<<"\t"; outFile << "邮编:"<<st[i].post<<"\t"; outFile << "E_MAIL:"<<st[i].mail<<endl; } outFile.close(); } void information::read() { vector<string> svec; string fileName,s; cin>>fileName; ifstream inFile(fileName.c_str()); if(!inFile){ cerr<<"\terror:unable to open output file: "<<fileName<<endl; } while(getline(inFile,s)) svec.push_back(s); for(vector<string>::iterator iter=svec.begin();iter!=svec.end();++iter) cout<<*iter<<endl<<endl; } int main() { information s; cout<<"\t★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★"<<endl; cout<<endl; cout<<"\t\t\twelcome to use the communication book"<<endl; cout<<"\t\t\t\t\t\tdesigned: hu"<<endl; cout<<"\t\t----------------------------------------------------"<<endl; begin:cout<<"\t\t| 1.编辑通信录 2.查询联系人 |"<<endl; cout<<"\t\t| 3.保存通信录 4.打开通记录 |"<<endl; cout<<"\t\t----------------------------------------------------"<<endl; string name; string number; string address; string telephone; string post; string mail; int val1; cout<<"\t\t请选择功能号:"; cin>>val1; switch(val1) { case 1: cout<<"\t\t(1) 增加联系人"<<endl; cout<<"\t\t(2) 修改联系人"<<endl; cout<<"\t\t(3) 删除联系人"<<endl; int val2; cout<<"\t\t请选择选项:"; cin>>val2; switch(val2) { case 1: cout<<"\t 输入姓名: "; cin>>name; cout<<"\t 输入学号: "; cin>>number; cout<<"\t 输入地址: "; cin>>address; cout<<"\t 输入电话: "; cin>>telephone; cout<<"\t 输入邮编: "; cin>>post; cout<<"\t 输入邮箱: "; cin>>mail; s.add(name,number,address,telephone,post,mail); cout<<"\t\t按0键退出系统,按其他键返回主菜单:"; int back_add; cin>>back_add; if(back_add!=0) goto begin; else goto end; break; case 2: cout<<"\t请输入要修改的学生姓名:"; cin>>name; s.correct(name); cout<<"\t\t按0键退出系统,按其他键返回主菜单:"; int back_correct; cin>>back_correct; if(back_correct!=0) goto begin; else goto end; break; case 3: cout<<"\t请输入要删除的学生信息的学生姓名:"; cin>>name; s.del(name); cout<<"\t\t按0键退出系统,按其他键返回主菜单:"; int back_del; cin>>back_del; if(back_del!=0) goto begin; else goto end; break; } break; case 2: cout<<"\t\t(1) 按学生姓名查询"<<endl; cout<<"\t\t(2) 按学生学号查询"<<endl; int val3; cout<<"\t\t请选择选项:"; cin>>val3; switch(val3) { case 1: cout<<"\t请输入查询的学生姓名:"; cin>>name; s.findname(name); cout<<"\t\t按0键退出系统,按其他键返回主菜单:"; int back_findname; cin>>back_findname; if(back_findname!=0) goto begin; else goto end; break; case 2: cout<<"\t请输入查询的学生学号:"; cin>>number; s.findnumber(number); cout<<"\t\t按0键退出系统,按其他键返回主菜单:"; int back_findnumber; cin>>back_findnumber; if(back_findnumber!=0) goto begin; else goto end; break; } break; case 3: cout<<"\t确定保存修改的记录吗(请输入y或n进行选择):"; char val4; cin>>val4; if(val4==\'n\') { cout<<"放弃记录保存!"<<endl; cout<<"\t\t按0键退出系统,按其他键返回主菜单:"; int back_save; cin>>back_save; if(back_save!=0) goto begin; else goto end; } else { s.save(); cout<<"\t保存成功!"<<endl; cout<<"\t\t按0键退出系统,按其他键返回主菜单:"; int back_save; cin>>back_save; if(back_save!=0) goto begin; else goto end; } break; case 4: cout<<"\t输入要打开的记录名:"; s.read(); cout<<"\t\t按0键退出系统,按其他键返回主菜单:"; int back_read; cin>>back_read; if(back_read!=0) goto begin; else goto end; break; } end:cout<<"\t**********成功退出系统,欢迎再次使用!**********"<<endl; return 0; }