【c++】通讯录管理系统

时间:2024-03-05 22:33:34
#include <iostream> #include <fstream> #include <cstring> using namespace std; const int MAX = 1e3 + 10; // 最大人数 struct Person { string name; // 姓名 string sex; // 性别 int age; // 年龄 string phone; // 电话号码 string address; // 住址 }; struct addressbook { Person personarray[MAX]; // 通讯录中保存的联系人数组 int size; // 通讯录中人员个数 }; void showMenu(); void addPerson(addressbook *abs); void showPerson(const addressbook *abs); int namefind(addressbook *abs); int phonefind(addressbook *abs); int addressfind(addressbook *abs); void deletePerson(addressbook *abs); void findPerson(addressbook *abs); void modifyPerson(addressbook *abs); void ofaddressbook(const addressbook *abs); void ifaddressbook(addressbook *abs); void cleanPerson(addressbook *abs); void end(); int main() { int select; addressbook abs; abs.size = 0; while (true) { showMenu(); // 菜单调用 cin >> select; system("clear"); switch (select) { case 0: // 退出通讯录 end(); break; case 1: // 添加联系人 addPerson(&abs); break; case 2: // 显示联系人 showPerson(&abs); break; case 3: // 删除联系人 deletePerson(&abs); break; case 4: // 查找联系人 findPerson(&abs); break; case 5: // 修改联系人 modifyPerson(&abs); break; case 6: // 备份通讯录 ofaddressbook(&abs); break; case 7: // 恢复通讯录 ifaddressbook(&abs); break; case 8: // 清空通讯录 cleanPerson(&abs); break; default: cout << "输入错误,请重新输入" << endl; cout << "请按任意键并回车继续" << endl; // MAC系统 cin >> select; // MAC系统 system("clear"); // MAC系统 // system("pause");WINDOWS系统 // system("cls");WINDOWS系统 continue; } cout << "请按任意键并回车继续" << endl; // MAC系统 cin >> select; // MAC系统 system("clear"); // MAC系统 // system("pause");WINDOWS系统 // system("cls");WINDOWS系统 } return 0; } void showMenu() { cout << "***********************" << endl << "*****1、添加联系人*****" << endl << "*****2、显示联系人*****" << endl << "*****3、删除联系人*****" << endl << "*****4、查找联系人*****" << endl << "*****5、修改联系人*****" << endl << "*****6、备份通讯录*****" << endl << "*****7、恢复通讯录*****" << endl << "*****8、清空通讯录*****" << endl << "*****0、退出通讯录*****" << endl << "***********************" << endl; } void addPerson(addressbook *abs) { if (abs->size == MAX) // 判断通讯录人数是否已满 { cout << "通讯录人数已满" << endl; } else { cout << "请输入姓名:"; cin >> abs->personarray[abs->size].name; cout << "请输入性别:"; cin >> abs->personarray[abs->size].sex; cout << "请输入年龄:"; cin >> abs->personarray[abs->size].age; cout << "请输入电话号码:"; cin >> abs->personarray[abs->size].phone; cout << "请输入住址:"; cin >> abs->personarray[abs->size].address; abs->size++; cout << "联系人添加成功" << endl; } } void showPerson(const addressbook *abs) { if (abs->size) // 判断人数是否为零 { for (int i = 0; i < abs->size; i++) { cout << "姓名:" << abs->personarray[i].name << "\t性别:" << abs->personarray[i].sex << "\t年龄:" << abs->personarray[i].age << "\t电话号码:" << abs->personarray[i].phone << "\t住址:" << abs->personarray[i].address << endl; } } else { cout << "通讯录为空" << endl; } } int namefind(addressbook *abs) { string name; cout << "请输入您要查找联系人的姓名" << endl; cin >> name; for (int i = 0; i < abs->size; i++) // 查找输入的联系人 { if (name.compare(abs->personarray[i].name) == 0) { return i; } } return -1; } int phonefind(addressbook *abs) { string phone; cout << "请输入您要查找联系人的电话号码" << endl; cin >> phone; for (int i = 0; i < abs->size; i++) // 查找输入的联系人 { if (phone.compare(abs->personarray[i].phone) == 0) { return i; } } return -1; } int addressfind(addressbook *abs) { string address; cout << "请输入您要查找联系人住址" << endl; cin >> address; for (int i = 0; i < abs->size; i++) // 查找输入的联系人 { if (address.compare(abs->personarray[i].address) == 0) { return i; } } return -1; } void deletePerson(addressbook *abs) { int n; cout << "请输入查询方式\n1.姓名\n2.电话号码\n3.住址" << endl; cin >> n; switch (n) // 选择查询方式 { case 1: n = namefind(abs); break; case 2: n = phonefind(abs); break; case 3: n = addressfind(abs); break; default: cout << "您输入有误,请重新输入" << endl; } system("clear"); if (n != -1) { abs->personarray[n] = abs->personarray[abs->size - 1]; // 替换将删除的联系人 abs->size--; // 通讯录人数-1 cout << "删除成功" << endl; } else { cout << "查无此人" << endl; } } void findPerson(addressbook *abs) { int n; cout << "请输入查询方式\n1.姓名\n2.电话号码\n3.住址" << endl; cin >> n; switch (n) // 选择查询方式 { case 1: n = namefind(abs); break; case 2: n = phonefind(abs); break; case 3: n = addressfind(abs); break; default: cout << "您输入有误,请重新输入" << endl; } system("clear"); if (n != -1) { cout << "姓名:" << abs->personarray[n].name << "\t性别:" << abs->personarray[n].sex << "\t年龄:" << abs->personarray[n].age << "\t电话号码:" << abs->personarray[n].phone << "\t住址:" << abs->personarray[n].address << endl; } else { cout << "查无此人" << endl; } } void modifyPerson(addressbook *abs) { int n; cout << "请输入查询方式\n1.姓名\n2.电话号码\n3.住址" << endl; cin >> n; switch (n) // 选择查询方式 { case 1: n = namefind(abs); break; case 2: n = phonefind(abs); break; case 3: n = addressfind(abs); break; default: cout << "您输入有误,请重新输入" << endl; } system("clear"); if (n != -1) { cout << "请输入修改后的姓名:"; cin >> abs->personarray[n].name; cout << "请输入修改后的性别:"; cin >> abs->personarray[n].sex; cout << "请输入修改后的年龄:"; cin >> abs->personarray[n].age; cout << "请输入修改后的电话号码:"; cin >> abs->personarray[n].phone; cout << "请输入修改后的住址:"; cin >> abs->personarray[n].address; cout << "修改成功" << endl; } else { cout << "查无此人" << endl; } } void ofaddressbook(const addressbook *abs) { ofstream ofs; ofs.open("addressbook.txt", ios::out); // 打开文件 ofs << "姓名\t性别\t年龄\t电话号码\t住址" << endl; for (int i = 0; i < abs->size; i++) { ofs << abs->personarray[i].name << "\t\t" << abs->personarray[i].sex << "\t\t" << abs->personarray[i].age << "\t\t" << abs->personarray[i].phone << "\t\t" << abs->personarray[i].address << endl; } ofs.close(); // 关闭文件 cout << "备份成功" << endl; } void ifaddressbook(addressbook *abs) { string temp; ifstream ifs; ifs.open("addressbook.txt", ios::in); // 打开文件 if (ifs.is_open()) { getline(ifs, temp); // 将文件上方的标签输入到临时字符串 while (ifs >> abs->personarray[abs->size].name >> abs->personarray[abs->size].sex >> abs->personarray[abs->size].age >> abs->personarray[abs->size].phone >> abs->personarray[abs->size].address) { abs->size++; // 人数+1 } ifs.close(); // 关闭文件 cout << "恢复成功" << endl; } else { cout << "文件打开失败" << endl; } } void cleanPerson(addressbook *abs) { abs->size = 0; // 将通讯录清空 cout << "通讯录已清空" << endl; } void end() { cout << "欢迎下次使用" << endl; exit(0); // 退出程序 }