通讯录管理系统 C++
Kai0807 2024-06-24 17:05:02 阅读 70
目录
一、前言
二、设计要求
三、概要设计
3.1 主界面设计
3.1.1 设计图
3.1.2设计代码
3.2 存储结构设计
3.3 系统功能设计
3.3.1 系统流程图编辑
3.3.2 系统子程序及功能介绍
四、详细设计
4.1 头文件
4.2 数据定义
4.3 系统主要子程序详细设计
4.3.1 主函数
4.3.2 添加联系人
4.3.3 显示联系人
4.3.4 以姓名查找联系人(返回联系人编号)
4.3.5 以性别查找联系人(返回联系人编号)
4.3.6 以年龄查找联系人(并返回联系人编号)
4.3.7 以手机号码查找联系人(并返回联系人编号)
4.3.8 以住址查找联系人(并返回联系人编号)
4.3.9删除联系人
4.3.10 查找联系人
4.3.11 修改联系人
4.3.12 备份通讯录
4.3.13 恢复通讯录
4.3.14清空通讯录
4.3.15 退出程序
五、源代码清单
一、前言
通讯录管理系统是一个比较实用的小型管理系统。随着生活节奏的加快,人们追求丰富便捷的电子产品体验,因此也提高对便捷方式的喜爱。当今社会下人们的交际范围越来越广泛,通讯录系统的出现,给人们的生活带来了极大便利和帮助,帮助人们处理繁琐复杂的工作,可以帮人们高效的处理和存储信息。
二、设计要求
使用C++实现一个通讯录管理系统,具有实现通讯录联系人的添加、 删除、查找、修改以及通讯录的清空、备份与还原等功能。
添加联系人:向通讯录中添加联系人,信息包括(姓名、性别、年龄、联系方式、住址)最多记录1000人。
显示联系人:显示通讯录中所有联系人信息。
删除联系人:按照用户需求进行删除指定联系人。
查找联系人:按照用户需求查看指定联系人信息。
修改联系人:按照用户需求重新修改指定联系人。
备份通讯录:将通讯录数据备份到文件中。
恢复通讯录:将备份的数据恢复到通讯录中。
清空通讯录:清空通讯录中所有信息。
退出通讯录:退出当前使用的通讯录。
三、概要设计
3.1 主界面设计
3.1.1 设计图
3.1.2设计代码
void showMenu(){ cout << "*********************" << endl << "*****1、添加联系人*****" << endl << "*****2、显示联系人*****" << endl << "*****3、删除联系人*****" << endl << "*****4、查找联系人*****" << endl << "*****5、修改联系人*****" << endl << "*****6、备份通讯录*****" << endl << "*****7、恢复通讯录*****" << endl << "*****8、清空通讯录*****" << endl << "*****0、退出通讯录*****" << endl << "*********************" << endl;}
3.2 存储结构设计
声明一个通讯录结构体,存储联系人结构体和人数。联系人结构体存储的内容有姓名、性别、年龄、电话号码、住址。
3.3 系统功能设计
3.3.1 系统流程图
3.3.2 系统子程序及功能介绍
函数名
| 功能
|
int main()
| 主函数
|
void showMenu()
| 菜单调用
|
bool addPerson(AddressBooks &addressBooks)
| 添加联系人
|
void showPersons(const AddressBooks &addressBooks)
| 显示联系人
|
int findPersonByName(const AddressBooks &addressBooks, const string &name)
int findPersonBySex(const AddressBooks &addressBooks, const string &sex)
int findPersonByAge(const AddressBooks &addressBooks, const int &age)
int findPersonByPhoneNumber(const AddressBooks &addressBooks, const string &phoneNumber)
int findPersonByAddress(const AddressBooks &addressBooks, const string &address)
| 按需求查找联系人
(返回联系人编号)
|
bool removePerson(AddressBooks &addressBooks)
| 删除联系人
|
bool findPerson(const AddressBooks &addressBooks)
| 查找联系人
|
bool modifyPerson(AddressBooks &addressBooks)
| 修改联系人
|
void backupAddressBooks(const AddressBooks &addressBooks)
| 备份通讯录
|
void restoreAddressBooks(AddressBooks &addressBooks)
| 恢复通讯录
|
void clearAddressBooks(AddressBooks &addressBooks)
| 清空联系人
|
closeSystem()
| 退出通讯录
|
四、详细设计
4.1 头文件
#include <iostream>#include <fstream>#include <vector>using namespace std;
4.2 数据定义
struct Persons{ string name; // 姓名 string sex; // 性别 unsigned int age; // 年龄 string phoneNumber; // 手机号码 string address; // 住址};class AddressBooks{public: vector<Persons> persons; // 通讯录中保存的联系人数组 static constexpr int MAXSIZE = 1e3 + 10; // 通讯录最大容量 AddressBooks() = default; ~AddressBooks() = default;};
4.3 系统主要子程序详细设计
4.3.1 主函数
int main(){ AddressBooks addressBooks; while (true) { showMenu(); // 菜单调用 int select; cin >> select; system("clear"); switch (select) { case 0: // 退出通讯录 { closeSystem(); break; } case 1: // 添加联系人 { if (addPerson(addressBooks) == true) { cout << "联系人添加成功" << endl; } else { cout << "通讯录人数已满" << endl; } break; } case 2: // 显示联系人 { showPersons(addressBooks); break; } case 3: // 删除联系人 { if (removePerson(addressBooks) == true) { cout << "删除成功" << endl; } else { cout << "删除失败" << endl; } break; } case 4: // 查找联系人 { if (!findPerson(addressBooks)) { cout << "查无此人" << endl; } break; } case 5: // 修改联系人 if (modifyPerson(addressBooks) == true) { cout << "修改成功" << endl; } else { cout << "修改失败" << endl; } break; case 6: // 备份通讯录 { backupAddressBooks(addressBooks); break; } case 7: // 恢复通讯录 { restoreAddressBooks(addressBooks); break; } case 8: // 清空通讯录 { clearAddressBooks(addressBooks); cout << "通讯录已清空" << endl; 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;}
4.3.2 添加联系人
在主菜单按“1”运行,分别输入姓名、性别、年龄、电话、住址。
bool addPerson(AddressBooks &addressBooks){ if (addressBooks.persons.size() < AddressBooks::MAXSIZE) { Persons person; int sex; cout << "请输入姓名:"; cin >> person.name; cout << "请输入性别:(男:1 女:0)"; cin >> sex; person.sex = sex == 1 ? "男" : "女"; cout << "请输入年龄:"; cin >> person.age; cout << "请输入手机号码:"; cin >> person.phoneNumber; cout << "请输入住址:"; cin >> person.address; addressBooks.persons.emplace_back(person); return true; } else { return false; }}
4.3.3 显示联系人
在主菜单按“2”运行,分别显示输入的所有联系人信息。
void showPersons(const AddressBooks &addressBooks){ if (!addressBooks.persons.empty()) { for (auto &person : addressBooks.persons) { cout << "姓名:" << person.name << "\t性别:" << person.sex << "\t年龄:" << person.age << "\t手机号码:" << person.phoneNumber << "\t住址:" << person.address << endl; } } else { cout << "通讯录为空" << endl; }}
4.3.4 以姓名查找联系人(返回联系人编号)
int findPersonByName(const AddressBooks &addressBooks, const string &name){ for (int i = 0; i < addressBooks.persons.size(); ++i) { if (addressBooks.persons[i].name == name) { return i; } } return -1;}
4.3.5 以性别查找联系人(返回联系人编号)
int findPersonBySex(const AddressBooks &addressBooks, const string &sex){ for (int i = 0; i < addressBooks.persons.size(); ++i) { if (addressBooks.persons[i].sex == sex) { return i; } } return -1;}
4.3.6 以年龄查找联系人(并返回联系人编号)
int findPersonByAge(const AddressBooks &addressBooks, const int &age){ for (int i = 0; i < addressBooks.persons.size(); ++i) { if (addressBooks.persons[i].age == age) { return i; } } return -1;}
4.3.7 以手机号码查找联系人(并返回联系人编号)
int findPersonByPhoneNumber(const AddressBooks &addressBooks, const string &phoneNumber){ for (int i = 0; i < addressBooks.persons.size(); ++i) { if (addressBooks.persons[i].phoneNumber == phoneNumber) { return i; } } return -1;}
4.3.8 以住址查找联系人(并返回联系人编号)
int findPersonByAddress(const AddressBooks &addressBooks, const string &address){ for (int i = 0; i < addressBooks.persons.size(); ++i) { if (addressBooks.persons[i].address == address) { return i; } } return -1;}
4.3.9删除联系人
在主菜单按“3”运行,选择按1.姓名2.性别3.年龄4.电话号码5.住址,然后输入一定的数据,通讯录有该联系人则删除,没有则输出删除失败。
bool removePerson(AddressBooks &addressBooks){ int select, index; cout << "请输入查询方式\n1.姓名\n2.性别\n3.年龄\n4.手机号码\n5.住址" << endl; cin >> select; switch (select) // 选择查询方式 { case 1: { cout << "请输入姓名:"; string name; cin >> name; index = findPersonByName(addressBooks, name); break; } case 2: { cout << "请输入性别:(男:1 女:0)"; int s; cin >> s; string sex = s == 1 ? "男" : "女"; index = findPersonBySex(addressBooks, sex); break; } case 3: { cout << "请输入年龄:"; int age; cin >> age; index = findPersonByAge(addressBooks, age); break; } case 4: { cout << "请输入手机号码:"; string phoneNumber; cin >> phoneNumber; index = findPersonByPhoneNumber(addressBooks, phoneNumber); break; } case 5: { cout << "请输入住址:"; string address; cin >> address; index = findPersonByAddress(addressBooks, address); break; } default: { index = -1; cout << "您输入有误" << endl; } } if (index != -1) { addressBooks.persons.erase(addressBooks.persons.begin() + index); return true; } return false;}
4.3.10 查找联系人
在主菜单按“4”运行,选择按1.姓名2.性别3.年龄4.电话号码5.住址,然后输入一定的数据,通讯录有该联系人则显示,没有则输出查无此人。
bool findPerson(const AddressBooks &addressBooks){ int select, index; cout << "请输入查询方式\n1.姓名\n2.性别\n3.年龄\n4.手机号码\n5.住址" << endl; cin >> select; switch (select) // 选择查询方式 { case 1: { cout << "请输入姓名:"; string name; cin >> name; index = findPersonByName(addressBooks, name); break; } case 2: { cout << "请输入性别:(男:1 女:0)"; int s; cin >> s; string sex = s == 1 ? "男" : "女"; index = findPersonBySex(addressBooks, sex); break; } case 3: { cout << "请输入年龄:"; int age; cin >> age; index = findPersonByAge(addressBooks, age); break; } case 4: { cout << "请输入手机号码:"; string phoneNumber; cin >> phoneNumber; index = findPersonByPhoneNumber(addressBooks, phoneNumber); break; } case 5: { cout << "请输入住址:"; string address; cin >> address; index = findPersonByAddress(addressBooks, address); break; } default: { index = -1; cout << "您输入有误" << endl; } } if (index != -1) { Persons person = addressBooks.persons[index]; cout << "姓名:" << person.name << "\t性别:" << person.sex << "\t年龄:" << person.age << "\t手机号码:" << person.phoneNumber << "\t住址:" << person.address << endl; return true; } return false;}
4.3.11 修改联系人
在主菜单按“5”运行,选择按1.姓名2.性别3.年龄4.电话号码5.住址,然后输入一定的数据,通讯录有该联系人提示用户输入数据并修改,没有则输出修改失败。
bool modifyPerson(AddressBooks &addressBooks){ int select, index; cout << "请输入查询方式\n1.姓名\n2.性别\n3.年龄\n4.手机号码\n5.住址" << endl; cin >> select; switch (select) // 选择查询方式 { case 1: { cout << "请输入姓名:"; string name; cin >> name; index = findPersonByName(addressBooks, name); break; } case 2: { cout << "请输入性别:(男:1 女:0)"; int s; cin >> s; string sex = s == 1 ? "男" : "女"; index = findPersonBySex(addressBooks, sex); break; } case 3: { cout << "请输入年龄:"; int age; cin >> age; index = findPersonByAge(addressBooks, age); break; } case 4: { cout << "请输入手机号码:"; string phoneNumber; cin >> phoneNumber; index = findPersonByPhoneNumber(addressBooks, phoneNumber); break; } case 5: { cout << "请输入住址:"; string address; cin >> address; index = findPersonByAddress(addressBooks, address); break; } default: { index = -1; cout << "您输入有误" << endl; } } if (index != -1) { Persons person; cout << "请输入修改后的姓名:"; cin >> person.name; cout << "请输入修改后的性别:(男:1 女:0)"; int s; cin >> s; person.sex = s == 1 ? "男" : "女"; cout << "请输入修改后的年龄:"; cin >> person.age; cout << "请输入修改后的手机号码:"; cin >> person.phoneNumber; cout << "请输入修改后的住址:"; cin >> person.address; addressBooks.persons[index] = person; return true; } return false;}
4.3.12 备份通讯录
在主菜单按“6”运行,将通讯录数据备份到文件中。
void backupAddressBooks(const AddressBooks &addressBooks){ ofstream ofs; ofs.open("addressbook.txt", ios::out); // 打开文件 ofs << "姓名\t\t性别\t\t年龄\t\t手机号码\t\t住址" << endl; for (auto &person : addressBooks.persons) { ofs << person.name << "\t\t" << person.sex << "\t\t" << person.age << "\t\t" << person.phoneNumber << "\t\t" << person.address << endl; } ofs.close(); // 关闭文件 cout << "备份成功" << endl;}
4.3.13 恢复通讯录
在主菜单按“7”运行,将文件数据恢复到通讯录中。
void restoreAddressBooks(AddressBooks &addressBooks){ ifstream ifs; ifs.open("addressbook.txt", ios::in); // 打开文件 if (ifs.is_open()) { string temp; getline(ifs, temp); // 将文件上方的标签输入到临时字符串 cout << temp; Persons person; clearAddressBooks(addressBooks); while (ifs >> person.name >> person.sex >> person.age >> person.phoneNumber >> person.address) { addressBooks.persons.emplace_back(person); } ifs.close(); // 关闭文件 cout << "恢复成功" << endl; } else { cout << "文件打开失败" << endl; }}
4.3.14清空通讯录
在主菜单按“8”运行,将通讯录中成员全部清空。
void clearAddressBooks(AddressBooks &addressBooks){ addressBooks.persons.clear();}
4.3.15 退出程序
在主菜单按“0”运行,退出通讯录管理系统。
void closeSystem(){ cout << "欢迎下次使用" << endl; exit(0); // 退出程序}
五、源代码清单
#include <iostream>#include <fstream>#include <vector>using namespace std;struct Persons{ string name; // 姓名 string sex; // 性别 unsigned int age; // 年龄 string phoneNumber; // 手机号码 string address; // 住址};class AddressBooks{public: vector<Persons> persons; // 通讯录中保存的联系人数组 static constexpr int MAXSIZE = 1e3 + 10; // 通讯录最大容量 AddressBooks() = default; ~AddressBooks() = default;};void showMenu();void closeSystem();bool addPerson(AddressBooks &addressBooks);void showPersons(const AddressBooks &addressBooks);int findPersonByName(const AddressBooks &addressBooks, const string &name);int findPersonBySex(const AddressBooks &addressBooks, const string &sex);int findPersonByAge(const AddressBooks &addressBooks, const int &age);int findPersonByPhoneNumber(const AddressBooks &addressBooks, const string &phoneNumber);int findPersonByAddress(const AddressBooks &addressBooks, const string &address);bool removePerson(AddressBooks &addressBooks);bool findPerson(const AddressBooks &addressBooks);bool modifyPerson(AddressBooks &addressBooks);void backupAddressBooks(const AddressBooks &addressBooks);void restoreAddressBooks(AddressBooks &addressBooks);void clearAddressBooks(AddressBooks &addressBooks);int main(){ AddressBooks addressBooks; while (true) { showMenu(); // 菜单调用 int select; cin >> select; system("clear"); switch (select) { case 0: // 退出通讯录 { closeSystem(); break; } case 1: // 添加联系人 { if (addPerson(addressBooks) == true) { cout << "联系人添加成功" << endl; } else { cout << "通讯录人数已满" << endl; } break; } case 2: // 显示联系人 { showPersons(addressBooks); break; } case 3: // 删除联系人 { if (removePerson(addressBooks) == true) { cout << "删除成功" << endl; } else { cout << "删除失败" << endl; } break; } case 4: // 查找联系人 { if (!findPerson(addressBooks)) { cout << "查无此人" << endl; } break; } case 5: // 修改联系人 if (modifyPerson(addressBooks) == true) { cout << "修改成功" << endl; } else { cout << "修改失败" << endl; } break; case 6: // 备份通讯录 { backupAddressBooks(addressBooks); break; } case 7: // 恢复通讯录 { restoreAddressBooks(addressBooks); break; } case 8: // 清空通讯录 { clearAddressBooks(addressBooks); cout << "通讯录已清空" << endl; 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 closeSystem(){ cout << "欢迎下次使用" << endl; exit(0); // 退出程序}bool addPerson(AddressBooks &addressBooks){ if (addressBooks.persons.size() < AddressBooks::MAXSIZE) { Persons person; int sex; cout << "请输入姓名:"; cin >> person.name; cout << "请输入性别:(男:1 女:0)"; cin >> sex; person.sex = sex == 1 ? "男" : "女"; cout << "请输入年龄:"; cin >> person.age; cout << "请输入手机号码:"; cin >> person.phoneNumber; cout << "请输入住址:"; cin >> person.address; addressBooks.persons.emplace_back(person); return true; } else { return false; }}void showPersons(const AddressBooks &addressBooks){ if (!addressBooks.persons.empty()) { for (auto &person : addressBooks.persons) { cout << "姓名:" << person.name << "\t性别:" << person.sex << "\t年龄:" << person.age << "\t手机号码:" << person.phoneNumber << "\t住址:" << person.address << endl; } } else { cout << "通讯录为空" << endl; }}int findPersonByName(const AddressBooks &addressBooks, const string &name){ for (int i = 0; i < addressBooks.persons.size(); ++i) { if (addressBooks.persons[i].name == name) { return i; } } return -1;}int findPersonBySex(const AddressBooks &addressBooks, const string &sex){ for (int i = 0; i < addressBooks.persons.size(); ++i) { if (addressBooks.persons[i].sex == sex) { return i; } } return -1;}int findPersonByAge(const AddressBooks &addressBooks, const int &age){ for (int i = 0; i < addressBooks.persons.size(); ++i) { if (addressBooks.persons[i].age == age) { return i; } } return -1;}int findPersonByPhoneNumber(const AddressBooks &addressBooks, const string &phoneNumber){ for (int i = 0; i < addressBooks.persons.size(); ++i) { if (addressBooks.persons[i].phoneNumber == phoneNumber) { return i; } } return -1;}int findPersonByAddress(const AddressBooks &addressBooks, const string &address){ for (int i = 0; i < addressBooks.persons.size(); ++i) { if (addressBooks.persons[i].address == address) { return i; } } return -1;}bool removePerson(AddressBooks &addressBooks){ int select, index; cout << "请输入查询方式\n1.姓名\n2.性别\n3.年龄\n4.手机号码\n5.住址" << endl; cin >> select; switch (select) // 选择查询方式 { case 1: { cout << "请输入姓名:"; string name; cin >> name; index = findPersonByName(addressBooks, name); break; } case 2: { cout << "请输入性别:(男:1 女:0)"; int s; cin >> s; string sex = s == 1 ? "男" : "女"; index = findPersonBySex(addressBooks, sex); break; } case 3: { cout << "请输入年龄:"; int age; cin >> age; index = findPersonByAge(addressBooks, age); break; } case 4: { cout << "请输入手机号码:"; string phoneNumber; cin >> phoneNumber; index = findPersonByPhoneNumber(addressBooks, phoneNumber); break; } case 5: { cout << "请输入住址:"; string address; cin >> address; index = findPersonByAddress(addressBooks, address); break; } default: { index = -1; cout << "您输入有误" << endl; } } if (index != -1) { addressBooks.persons.erase(addressBooks.persons.begin() + index); return true; } return false;}bool findPerson(const AddressBooks &addressBooks){ int select, index; cout << "请输入查询方式\n1.姓名\n2.性别\n3.年龄\n4.手机号码\n5.住址" << endl; cin >> select; switch (select) // 选择查询方式 { case 1: { cout << "请输入姓名:"; string name; cin >> name; index = findPersonByName(addressBooks, name); break; } case 2: { cout << "请输入性别:(男:1 女:0)"; int s; cin >> s; string sex = s == 1 ? "男" : "女"; index = findPersonBySex(addressBooks, sex); break; } case 3: { cout << "请输入年龄:"; int age; cin >> age; index = findPersonByAge(addressBooks, age); break; } case 4: { cout << "请输入手机号码:"; string phoneNumber; cin >> phoneNumber; index = findPersonByPhoneNumber(addressBooks, phoneNumber); break; } case 5: { cout << "请输入住址:"; string address; cin >> address; index = findPersonByAddress(addressBooks, address); break; } default: { index = -1; cout << "您输入有误" << endl; } } if (index != -1) { Persons person = addressBooks.persons[index]; cout << "姓名:" << person.name << "\t性别:" << person.sex << "\t年龄:" << person.age << "\t手机号码:" << person.phoneNumber << "\t住址:" << person.address << endl; return true; } return false;}bool modifyPerson(AddressBooks &addressBooks){ int select, index; cout << "请输入查询方式\n1.姓名\n2.性别\n3.年龄\n4.手机号码\n5.住址" << endl; cin >> select; switch (select) // 选择查询方式 { case 1: { cout << "请输入姓名:"; string name; cin >> name; index = findPersonByName(addressBooks, name); break; } case 2: { cout << "请输入性别:(男:1 女:0)"; int s; cin >> s; string sex = s == 1 ? "男" : "女"; index = findPersonBySex(addressBooks, sex); break; } case 3: { cout << "请输入年龄:"; int age; cin >> age; index = findPersonByAge(addressBooks, age); break; } case 4: { cout << "请输入手机号码:"; string phoneNumber; cin >> phoneNumber; index = findPersonByPhoneNumber(addressBooks, phoneNumber); break; } case 5: { cout << "请输入住址:"; string address; cin >> address; index = findPersonByAddress(addressBooks, address); break; } default: { index = -1; cout << "您输入有误" << endl; } } if (index != -1) { Persons person; cout << "请输入修改后的姓名:"; cin >> person.name; cout << "请输入修改后的性别:(男:1 女:0)"; int s; cin >> s; person.sex = s == 1 ? "男" : "女"; cout << "请输入修改后的年龄:"; cin >> person.age; cout << "请输入修改后的手机号码:"; cin >> person.phoneNumber; cout << "请输入修改后的住址:"; cin >> person.address; addressBooks.persons[index] = person; return true; } return false;}void backupAddressBooks(const AddressBooks &addressBooks){ ofstream ofs; ofs.open("addressbook.txt", ios::out); // 打开文件 ofs << "姓名\t\t性别\t\t年龄\t\t手机号码\t\t住址" << endl; for (auto &person : addressBooks.persons) { ofs << person.name << "\t\t" << person.sex << "\t\t" << person.age << "\t\t" << person.phoneNumber << "\t\t" << person.address << endl; } ofs.close(); // 关闭文件 cout << "备份成功" << endl;}void restoreAddressBooks(AddressBooks &addressBooks){ ifstream ifs; ifs.open("addressbook.txt", ios::in); // 打开文件 if (ifs.is_open()) { string temp; getline(ifs, temp); // 将文件上方的标签输入到临时字符串 cout << temp; Persons person; clearAddressBooks(addressBooks); while (ifs >> person.name >> person.sex >> person.age >> person.phoneNumber >> person.address) { addressBooks.persons.emplace_back(person); } ifs.close(); // 关闭文件 cout << "恢复成功" << endl; } else { cout << "文件打开失败" << endl; }}void clearAddressBooks(AddressBooks &addressBooks){ addressBooks.persons.clear();}
上一篇: Java 面试题:Java 的 Vector、ArrayList、LinkedList 有何区别?
下一篇: 华为OD机试C卷-- 伐木工(Java & JS & Python & C & C++)
本文标签
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。