2020-10-20

it2023-11-15  64

C++创建一个通讯录,可添加、删除、存盘、显示、读盘、查找,复杂过来格式变了,也懒得一条一条整理了,有需要的自己整理整理吧😬😬😬😬😬😬😬😬😬😬😬😬😬😬😬😬

#include #include #include #include #include using namespace std;class txl {public: txl(); char inter_face();//首页 void add_person();//添加通讯录 void del_person();//删除通讯录 void select(); //查找联系人 void show_all();//显示通讯录 void save_new();//存盘 void read_new();//读盘private: string name; //姓名 string phone; //电话号码 string email;//email地址 string addr; //地址 string postcode; //邮编};txl::txl(){ name = “\0”; phone="\0"; email="\0"; addr = “\0”; postcode = “\0”;}//开始 char txl::inter_face(){ system(“cls”) ; cout <<endl; cout<<“" <<endl <<“虚拟通讯录" <<endl <<"1.添加通讯录” <<endl <<"2.删除通讯录" <<endl <<"3.查找联系人" <<endl <<"4.显示通讯录" <<endl <<"5.电话簿存盘" <<endl <<"6.读出电话簿" <<endl <<"7.退出 " <<endl <<"” <<endl <<endl <<“请选择操作请一直点击"; char choose; cin >>choose; fflush(stdin); return choose;}//1添加联系人void txl:: add_person(){ cout <<“请输入新联系人信息” <<endl; cout<<“姓名:”; cin >>name; cout<<endl; fflush(stdin); cout <<“电话号码:”; cin >>phone; cout<<endl; fflush(stdin); cout <<“email地址:”; cin >>email; cout<<endl; fflush(stdin); cout <<"地址: "; cin >>addr; cout<<endl; fflush(stdin) ; cout <<"邮编: "; cin >>postcode; cout<<endl; fflush(stdin); save_new() ; cout <<endl <<endl <<"保存成功继续点击” <<endl <<endl; system(“pause”) ;}//2删除联系人void txl:: del_person(){ ofstream outData(“temp. txt”, ios:: out); ifstream inData(“txl. txt”, ios:: in); if (!outData||!inData) { cout <<endl <<“联系人不存在!*继续点击” <<endl; system(“pause”) ; return; } string sign; cout <<endl <<"请输入要查找人的姓名或电话: “; cin >>sign; string str1; bool flag = true; string str; while (inData >>name >>phone) { getline(inData, str) ; if ((signname) || (signphone) ) { cout <<endl <<“你要查找并删除的联系人: " <<endl <<endl; cout <<“姓名 电话 email 地址 邮编”<<endl; cout <<str1 <<endl; cout <<setiosflags(ios::left) <<setw(20) <<name<<” " <<phone <<str <<endl; flag = false; break; } outData <<setiosflags(ios:: left) <<setw(20) <<name<<” " <<phone<<str <<endl; } if (flag) { cout <<endl <<“联系人不存在!继续点击" <<endl <<endl; } else { while (getline(inData, str) ) { outData <<str <<endl; } outData. close(); inData. close(); ofstream out(“txl. txt”, ios::out) ; ifstream in(“temp. txt”, ios::in) ; if (!out || !in) { cout <<endl <<"文件打开失败继续点击” <<endl <<endl; system(“pause”) ; return; } while (getline(in,str)) { out <<str <<endl; } out. close() ; in. close() ; cout <<endl <<“联系人不存在!*继续点击” <<endl <<endl; } system(“pause”) ;}//3查找联系人 void txl:: select(){ ifstream inData(“txl. txt”, ios:: in) ; if (! inData) { cout <<endl <<“联系人不存在!继续点击" <<endl; system(“pause”) ; return; } string sign; cout <<endl <<"输入你想查找的联系人的姓名或电话: "; cin >>sign; fflush(stdin); string str1; bool flag = true; string str; while (inData >>name >>phone) { getline(inData, str); if ((namesign) || (phonesign) ) { cout <<endl <<"你要查找的联系人是: " <<endl <<endl; cout <<"姓名 电话 email 地址 邮编 “<<endl; cout <<str1 <<endl; cout <<setiosflags(ios::left) <<setw(17) <<name<<phone <<str <<endl; flag = false; system(“pause”) ; break; } }}//4显示通讯录void txl::show_all(){ ifstream inData(“txl. txt”, ios:: in); if (! inData) { cout <<endl <<” 没有找到联系人继续点击” <<endl; system(“pause”); return; } bool flag = true; string record; while (getline(inData,record)) { if (flag) { cout <<endl << "显示通讯录: "<<endl; cout <<“姓名 电话号码 email 地址 邮编”<<endl; } cout << record <<endl; flag = false; } if (flag) { cout <<endl <<“联系人不存在!继续点击”<<endl<<endl; } else { cout <<endl <<“完成!*继续点击” <<endl <<endl; } system(“pause”);}//5电话簿存盘 void txl:: save_new(){ofstream out(“txl.txt”, ios::out) ;ifstream in(“temp.txt”, ios:: in) ; if (!out || ! in) { cout <<endl <<“存盘成功!继续点击" <<endl <<endl; system(“pause”); return; }system(“pause”) ;} //6读出电话簿void txl::read_new(){ ifstream inData(“txl. txt”, ios::in); if (!inData) { cout <<endl <<" 没有找到联系人继续点击” <<endl; system(“pause”) ; return; } bool flag = true; string record; while (getline(inData, record)) { if (flag) { cout <<endl <<"读出通讯录: "<<endl; cout <<“姓名 电话号码 email 地址 邮编”<<endl; } cout << record <<endl; flag = false; } cout<<“继续点击”<<endl; if (flag) { cout <<endl <<“通讯录无内容!*继续点击”; } system(“pause”);}enum power{ADD = ‘1’ , DEL = ‘2’ , Select = ‘3’,SHOW = ‘4’ , Save = ‘5’ , Read=‘6’ ,END = ‘7’ };int main(){ char choose; txl abj; while (choose = abj. inter_face()) { switch (choose) { case ADD: abj. add_person() ;//添加通讯录 break; case DEL: abj. del_person() ;//删除联系人 break; case Select: abj. select() ;//查找联系人 break; case SHOW: abj. show_all() ;//联系人 break; case Save: abj. save_new(); //存盘 break; case Read: abj. read_new(); //读盘 break; case END: cout <<endl <<“退出!*继续点击” <<endl <<endl; exit(0) ; default: break; } } return 0;}

最新回复(0)