/* C++电话本程序 */ /* 1.可实现txt文件的读取和保存。 */ /* 2.可实现联系人的增添、删除和修改。 */ /* 3.可实现通过联系人查询号码。 */ /* 4.可实现通过号码查询联系人。 */ /* 5.可实现全部联系人的显示。 */ /* 运行环境vs2010||codeblocks */ #include<iostream> #include<string> #include<vector> #include<fstream> #include<set> #include<stdlib.h> using namespace std; #define file_name1 "phonenumin.txt" //使用宏定义输出文件名 #define file_name2 "phonenumout.txt" class Phonenumtext{ public: string name; string num; Phonenumtext(string mz,string hm){ name=mz; num=hm; } }; class all{ public: vector<Phonenumtext> phone; bool addnum(string mz,string hm); bool delnum(int bianhao); void displaynum(vector<Phonenumtext>::iterator iter1); void displayallnum(); int change(int bianhao); void find1(string hm); void find2(string mz); void readfromtxt(); void savetxt(); }ans; bool all::addnum(string mz,string hm){ Phonenumtext tmp(mz,hm); for(int i=0;i<phone.size();i++){ if(mz==phone[i].name){ cout<<"该联系人已经存在!"<<endl; return false; } } phone.push_back(tmp); cout<< "添加联系人 "<<tmp.name<<" 成功" << endl; return false; } bool all::delnum(int bianhao){ if(bianhao>phone.size() || bianhao<1){ cout << "编号输入错误" << endl; return false; } vector<Phonenumtext>::iterator iter1; iter1=phone.begin()+bianhao-1; phone.erase(iter1); return true; } int all::change(int bianhao){ if(bianhao>phone.size()||bianhao<1){ cout<<"编号输入错误"<<endl; return 0; } vector<Phonenumtext>::iterator iter1; iter1=phone.begin()+bianhao-1; cout<<"输入需要修改后的新号码:"<<endl; string str; cin>>str; iter1->num=str; return 1; } void all::displayallnum(){ if(phone.size()==0){ cout << "没有联系人信息" << endl; return; } cout << "编号\t姓名\t号码" << endl; for(vector<Phonenumtext>::iterator iter1=phone.begin();iter1!=phone.end();iter1++){ displaynum(iter1); } } void all::displaynum(vector<Phonenumtext>::iterator iter1){ cout << (int)(iter1-phone.begin()+1) << "\t" << iter1->name << "\t"; cout << "\t" << iter1->num << "\t\t" <<endl; } void all::find1(string hm){ //输入号码查询联系人 for(vector<Phonenumtext>::iterator iter1=phone.begin();iter1!=phone.end();iter1++){ if(iter1->num==hm) cout<<"该号码的联系人为:"<<iter1->name<<endl; return; } cout<<"无该号码的联系人!"<<endl; return ; } void all::find2(string mz){ //输入联系人查询号码 for(vector<Phonenumtext>::iterator iter1=phone.begin();iter1!=phone.end();iter1++){ if(iter1->name==mz) cout<<"改联系人的号码为:"<<iter1->num<<endl; return; } cout<<"无该联系人的号码!"<<endl; return; } void all::readfromtxt(){ //从文件读取 ifstream fin(file_name1); string mz,hm; while(fin >> mz>>hm){ addnum(mz,hm); } fin.close(); cout << "读取到" << phone.size() << "组数据" << endl; system("pause"); } void all::savetxt(){ //保存至文件 ofstream fout(file_name2); for(vector<Phonenumtext>::iterator iter1=phone.begin();iter1!=phone.end();iter1++){ fout << "联系人" <<"\t"<< iter1->name << endl; fout<<"号码"<<"\t"<<iter1->num<<endl; } fout.close(); cout << "存储完毕" << endl; } void menu_add(){ //添加联系人菜单 string name,num; cout << "->添加联系人" << endl; cout << "您正在添加第 " << ans.phone.size()+1 <<" 个联系人" << endl; cout << "请输入联系人姓名:" << endl; cin >> name; cout<<"请输入联系人号码:"; cin>>num; ans.addnum(name,num); return; } void menu_del(){ //联系人删除菜单 cout << "->删除联系人" << endl; if( ans.phone.size()==0){ cout << "没有联系人信息,无法执行删除操作" << endl; return; } ans.displayallnum(); cout << "请输入要删除联系人的编号:" << endl; int ipt; cin >> ipt; if(ans.delnum(ipt)) cout << "删除成功" << endl; else cout << "删除失败" << endl; return; } void menu_change(){ ans.displayallnum(); cout<<"->输入需要修改联系人的编号"<<endl; int x; cin>>x; if(ans.change(x)) cout << "您已成功修改第 " << ans.phone.size() <<" 个联系人" << endl; else menu_change(); } void menu_find(){ //查询菜单 cout<<"->查询"<<endl; if( ans.phone.size()==0){ cout << "没有联系人信息,无法执行查询操作" << endl; return; } cout<<"请选择您要进行的操作:"<<endl; cout<<"\t1.输入号码查询联系人"<<endl; cout<<"\t2.输入联系人查询号码"<<endl; cout<<"\t3.返回"<<endl; int x; string str; cin>>x; switch(x){ case 1:cout<<"请输入需要查询的联系人的号码:"<<endl; cin>>str; ans.find1(str); break; case 2:cout<<"请输入需要查询的联系人的姓名:"<<endl; cin>>str; ans.find2(str); break; case 3: return; default: cout << "输入有误" << endl; } } void menu1(){ //主菜单 int ipt; system("cls"); while(true){ cout<<"\t*******************************"<<endl; cout<<"\t\t欢迎使用您的电话本"<<endl; cout<<"\t*******************************"<<endl<<endl; cout << "请选择您要进行的操作:" << endl; cout << " 1.添加联系人" << endl; cout << " 2.删除联系人" << endl; cout << " 3.显示全部联系人" << endl; cout << " 4.查找" << endl; cout << " 5.修改" << endl; cout << " 6.储存到文件" << endl; cout << " 7.退出程序" << endl; cin >> ipt; switch(ipt){ case 1:menu_add();break; case 2:menu_del();break; case 3:ans.displayallnum();break; case 4:menu_find();break; case 5:menu_change();break; case 6:ans.savetxt();break; case 7:exit(0);break; default:cout << "输入有误,请重新输入" << endl;break; } system("pause"); system("cls"); } } int main(){ int ipt1; string str1; system("color A"); cout<<"\t*******************************"<<endl; cout<<"\t\t欢迎使用您的电话本"<<endl; cout<<"\t*******************************"<<endl<<endl; retry: cout << "请选择数据来源:" << endl; cout << "1.从文件读入" << endl; cout << "2.键盘录入" << endl; cout << "请输入:" << endl; cin >> ipt1; switch(ipt1){ case 1: ans.readfromtxt();break; case 2:break; default:cout << "输入错误,请重新输入" << endl;goto retry;break; } menu1(); return 0; }