diff --git a/ContactManager.cpp b/ContactManager.cpp new file mode 100644 index 0000000..d4d3bbe --- /dev/null +++ b/ContactManager.cpp @@ -0,0 +1,50 @@ +#include "ContactManager.h" +#include + +void ContactManager::addContact() { + Contact newContact; + std::cout << "请输入联系人姓名: "; + std::cin >> newContact.name; + std::cout << "请输入联系人电话: "; + std::cin >> newContact.phone; + newContact.id = contacts.size() + 1; + contacts.push_back(newContact); +} + +void ContactManager::displayContacts() const { + std::cout << "通讯录如下:" << std::endl; + for (const auto& contact : contacts) { + std::cout << "ID:" << contact.id << ", 姓名: " << contact.name << ", 电话: " << contact.phone << std::endl; + } +} + +void ContactManager::findContact() const { + int id; + std::cout << "请输入要查找的联系人ID: "; + std::cin >> id; + + for (const auto& contact : contacts) { + if (contact.id == id) { + std::cout << "找到联系人:ID: " << contact.id << ", 姓名: " << contact.name << ", 电话: " << contact.phone << std::endl; + return; + } + } + + std::cout << "未找到联系人。" << std::endl; +} + +void ContactManager::deleteContact() { + int id; + std::cout << "请输入要删除的联系人ID: "; + std::cin >> id; + + for (auto it = contacts.begin(); it != contacts.end(); ++it) { + if (it->id == id) { + contacts.erase(it); + std::cout << "已删除联系人。" << std::endl; + return; + } + } + + std::cout << "未找到联系人。" << std::endl; +} \ No newline at end of file