(这个是我们c++作业)
此电话本可完成以下功能:
(1) 实现简单电话本功能,用姓名来搜索电话号码;
(2) 用户输入姓名,程序查找并输出结果;
(3) 用户可以通过输入,添加姓名和电话号码;
(4) 用户可以删除姓名和电话号码;
(5) 电话本可以保存在指定文件中;
(6) 电话可被从指定文件中读入到内存。
/*电话本程序 用户输入F表示用文件输入文件保存 用户输入输入K表示用键盘输入文件保存 用户输入Q表示查询电话号码 用户输入D表示删除记录 用户输入I表示插入电话号码 以空格分隔姓名和电话号码*/ #include<iostream> #include<map> #include<string> #include<fstream> using namespace std; ofstream myFile1;//键盘输入指令时所保存的电话本 对应文件file1 ofstream myFile2;//文件输入指令时所保存的电话本 对应文件file2 ifstream myFile3;//文件输入指令时指令文件 对应文件file3 ifstream myFile4;//事先写好的一个电话本(按“名字、空格、电话号码”的形式) 对应文件file4 class Phonebook{ public: map<string,string> phonebook; void Init1(); void Insert1(); void Find1(); void Delete1(); void Init2(); void Insert2(); void Find2(); void Delete2(); }; void Phonebook::Init1(){ myFile4.open("file4.txt"); string str1,str2; while(myFile4>>str1>>str2){ phonebook.insert(pair<string,string>(str1,str2)); myFile1<<str1<<" "<<str2<<endl; } } void Phonebook::Insert1(){ string str1,str2; cin>>str1>>str2; map<string,string>::iterator it; it = phonebook.find(str1); if(it!=phonebook.end()){ cout<<"This person"<<"("<<str1<<")"<<"doesn't exist"<<endl; } else{ phonebook.insert(pair<string,string>(str1,str2)); myFile1<<str1<<" "<<str2<<endl; } } void Phonebook::Find1(){ string str; cin>>str; map<string,string>::iterator it; it = phonebook.find(str); if(it==phonebook.end()){ cout<<"This person"<<"("<<str<<")"<<"doesn't exist"<<endl; } else{ cout<<it->second<<endl; } } void Phonebook::Delete1(){ string str; cin>>str; map<string,string>::iterator it; it = phonebook.find(str); if(it==phonebook.end()){ cout<<"This person"<<("<<str<<")<<"doesn't exist"<<endl; } else{ phonebook.erase(it); myFile1.close(); myFile1.open("file1.txt"); map<string,string>::iterator its; for(its=phonebook.begin();its!=phonebook.end();its++){ myFile1<<its->first<<" "<<its->second<<endl; } cout<<str<<" "<<"已被删除"<<endl; } } void Phonebook::Init2(){ myFile4.open("file4.txt"); string str1,str2; while(myFile4>>str1>>str2){ phonebook.insert(pair<string,string>(str1,str2)); myFile2<<str1<<" "<<str2<<endl; } } void Phonebook::Insert2(){ string str1,str2; myFile3>>str1>>str2; map<string,string>::iterator it; it = phonebook.find(str1); if(it!=phonebook.end()){ cout<<"This person"<<"("<<str1<<")"<<"doesn't exist"<<endl; } else{ phonebook.insert(pair<string,string>(str1,str2)); myFile2<<str1<<" "<<str2<<endl; } } void Phonebook::Find2(){ string str; myFile3>>str; map<string,string>::iterator it; it = phonebook.find(str); if(it==phonebook.end()){ cout<<"This person"<<"("<<str<<")"<<"doesn't exist"<<endl; } else{ cout<<it->second<<endl; } } void Phonebook::Delete2(){ string str; myFile3>>str; map<string,string>::iterator it; it = phonebook.find(str); if(it==phonebook.end()){ cout<<"This person"<<("<<str<<")<<"doesn't exist"<<endl; } else{ phonebook.erase(it); myFile2.close(); myFile2.open("file2.txt"); map<string,string>::iterator its; for(its=phonebook.begin();its!=phonebook.end();its++){ myFile2<<its->first<<" "<<its->second<<endl; } cout<<str<<" "<<"已被删除"<<endl; } } int main(){ Phonebook book; string str; cin>>str; if(str[0]=='K'){ myFile1.open("file1.txt"); book.Init1(); while(cin>>str){ if(str[0]=='I'){ book.Insert1(); } else if(str[0]=='Q'){ book.Find1(); } else if(str[0]=='D'){ book.Delete1(); } } } else if(str[0]=='F'){ myFile3.open("file3.txt"); myFile2.open("file2.txt"); book.Init2(); while(myFile3>>str){ if(str[0]=='I'){ book.Insert2(); } else if(str[0]=='Q'){ book.Find2(); } else if(str[0]=='D'){ book.Delete2(); } } } return 0; }