功能
- 添加联系人(姓名,电话,邮箱)
- 删除联系人(根据姓名)
- 查找联系人(根据姓名)
- 列出联系人(读取出所有的数据)
- 修改通讯录系统密码(修改password.txt)
效果图
源代码
编译时在连接器命令行加入
-std=c++11
完整代码
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>using namespace std;struct Contact {string name;string phone;string email;
};void saveContacts(const vector<Contact>& contacts) {ofstream file("contacts.txt");for (const auto& contact : contacts) {file << contact.name << "," << contact.phone << "," << contact.email << endl;}
}vector<Contact> loadContacts() {vector<Contact> contacts;ifstream file("contacts.txt");string line;while (getline(file, line)) {size_t pos1 = line.find(',');size_t pos2 = line.find(',', pos1 + 1);contacts.push_back({line.substr(0, pos1), line.substr(pos1 + 1, pos2 - pos1 - 1), line.substr(pos2 + 1)});}return contacts;
}string loadPassword() {string password;ifstream file("password.txt");if (file.is_open()) {getline(file, password);} else {password = "123456"; // 默认密码ofstream newFile("password.txt");newFile << password;}return password;
}void savePassword(const string& password) {ofstream file("password.txt");file << password;
}bool authenticate(const string& currentPassword) {string inputPassword;cout << "请输入密码 (Enter Password): ";cin >> inputPassword;return inputPassword == currentPassword;
}void changePassword(string& currentPassword) {string oldPassword, newPassword;cout << "请输入旧密码 (Enter Old Password): ";cin >> oldPassword;if (oldPassword == currentPassword) {cout << "请输入新密码 (Enter New Password): ";cin >> newPassword;currentPassword = newPassword;savePassword(currentPassword);cout << "密码已修改 (Password changed).\n";} else {cout << "旧密码错误 (Old password is incorrect).\n";}
}void addContact(vector<Contact>& contacts) {Contact contact;cout << "请输入联系人姓名 (Enter contact name): ";cin >> contact.name;cout << "请输入电话号码 (Enter phone number): ";cin >> contact.phone;cout << "请输入电子邮件 (Enter email): ";cin >> contact.email;contacts.push_back(contact);saveContacts(contacts);cout << "联系人已添加 (Contact added).\n";
}void deleteContact(vector<Contact>& contacts) {string name;cout << "请输入要删除的联系人姓名 (Enter the name of the contact to delete): ";cin >> name;auto it = remove_if(contacts.begin(), contacts.end(), [&name](const Contact& contact) {return contact.name == name;});if (it != contacts.end()) {contacts.erase(it, contacts.end());saveContacts(contacts);cout << "联系人已删除 (Contact deleted).\n";} else {cout << "未找到联系人 (Contact not found).\n";}
}void findContact(const vector<Contact>& contacts) {string name;cout << "请输入要查找的联系人姓名 (Enter the name of the contact to find): ";cin >> name;auto it = find_if(contacts.begin(), contacts.end(), [&name](const Contact& contact) {return contact.name == name;});if (it != contacts.end()) {cout << "姓名 (Name): " << it->name << ", 电话 (Phone): " << it->phone << ", 电子邮件 (Email): " << it->email << endl;} else {cout << "未找到联系人 (Contact not found).\n";}
}void displayAllContacts(const vector<Contact>& contacts) {cout << "所有联系人 (All Contacts):\n";for (const auto& contact : contacts) {cout << "姓名 (Name): " << contact.name << ", 电话 (Phone): " << contact.phone << ", 电子邮件 (Email): " << contact.email << endl;}
}int main() {vector<Contact> contacts = loadContacts();string currentPassword = loadPassword();if (!authenticate(currentPassword)) {cout << "密码错误,无法访问系统 (Incorrect password, unable to access the system).\n";return 1;}int choice;do {cout << "\n通讯录管理系统 (Address Book Management System):\n";cout << "1. 添加联系人 (Add Contact)\n";cout << "2. 删除联系人 (Delete Contact)\n";cout << "3. 查找联系人 (Find Contact)\n";cout << "4. 显示所有联系人 (Display All Contacts)\n";cout << "5. 修改密码 (Change Password)\n";cout << "6. 退出 (Exit)\n";cout << "请选择一个选项 (Please choose an option): ";cin >> choice;switch (choice) {case 1:addContact(contacts);break;case 2:deleteContact(contacts);break;case 3:findContact(contacts);break;case 4:displayAllContacts(contacts);break;case 5:changePassword(currentPassword);break;case 6:cout << "退出系统 (Exiting the system).\n";break;default:cout << "无效选项 (Invalid option).\n";}} while (choice != 6);return 0;
}
数据读/写方法(与上面无关)
数据保存在文件里的结构(数据用逗号分隔)
某人, 13800000000, qq@qqcom
---写--
void saveContacts(const vector<Contact>& contacts) {
ofstream file("contacts.txt"); // 打开或创建一个名为"contacts.txt"的输出文件流
for (const auto& contact : contacts) { // 遍历联系人向量
file << contact.name << "," << contact.phone << "," << contact.email << endl; // 将每个联系人的信息以特定格式写入文件,每行一个联系人,用逗号分隔各项信息
}
}
---读---
vector<Contact> loadContacts() {
vector<Contact> contacts; // 创建一个用于存储联系人的向量
ifstream file("contacts.txt"); // 打开一个名为"contacts.txt"的输入文件流
string line;
while (getline(file, line)) { // 逐行读取文件内容
size_t pos1 = line.find(','); // 查找第一个逗号的位置
size_t pos2 = line.find(',', pos1 + 1); // 查找第二个逗号的位置
contacts.push_back({line.substr(0, pos1), line.substr(pos1 + 1, pos2 - pos1 - 1), line.substr(pos2 + 1)}); // 根据逗号位置提取信息并创建联系人对象,添加到向量中
}
return contacts;
}