写c++程序 设计一个通讯录系统,联系人基本信息包括:联系人姓名、手机号码、邮箱、工作单位、家庭住址等基本信息,完成下面的功能需求,根据功能需求选择合适的逻辑结构和存储结构。二、功能需求(1)增加功能:能添加新的联系人信息。(2)删除功能:能够删除通讯录中的联系人。(3)修改功能:能够对通讯录中的联系人信息进行修改。(4)查询功能:通过员工号查询员工信息,并显示该员工所有信息。(5)排序功能:可以对联系人信息排序,对联系人姓名按照字典序排序,之后显示排序后的通讯录所有联系人信息。(6)显示功能:显示通讯录中所有联系人信息。功能:能够从文本中读取联系人信息,对联系人信息更改之后可以将通讯录中联系人信息保存到文本中,方便下次读取。
时间: 2024-03-18 07:39:49 浏览: 46
好,这是一个比较详细的功能需求,我可以为你提供一个基本的 C++ 程序框架,你可以在此基础上进行修改和完善,实现你所需要的功能。程序主要分为以下几个部分:
1. 定义联系人结构体
```
struct Contact {
std::string name;
std::string phone;
std::string email;
std::string company;
std::string address;
};
```
2. 定义通讯录类
```
class AddressBook {
public:
// 添加联系人
void addContact(const Contact& contact);
// 删除联系人
void deleteContact(const std::string& name);
// 修改联系人信息
void modifyContact(const std::string& name, const Contact& contact);
// 查询联系人信息
void queryContact(const std::string& name);
// 排序联系人信息
void sortContacts();
// 显示所有联系人信息
void displayAllContacts();
// 从文件中读取联系人信息
void readContactsFromFile(const std::string& filename);
// 将联系人信息保存到文件中
void saveContactsToFile(const std::string& filename);
private:
std::vector<Contact> contacts_;
};
```
3. 实现通讯录类的各个方法
```
void AddressBook::addContact(const Contact& contact) {
contacts_.push_back(contact);
}
void AddressBook::deleteContact(const std::string& name) {
for (auto it = contacts_.begin(); it != contacts_.end(); ++it) {
if (it->name == name) {
contacts_.erase(it);
break;
}
}
}
void AddressBook::modifyContact(const std::string& name, const Contact& contact) {
for (auto& c : contacts_) {
if (c.name == name) {
c.phone = contact.phone;
c.email = contact.email;
c.company = contact.company;
c.address = contact.address;
break;
}
}
}
void AddressBook::queryContact(const std::string& name) {
for (const auto& c : contacts_) {
if (c.name == name) {
std::cout << "Name: " << c.name << std::endl;
std::cout << "Phone: " << c.phone << std::endl;
std::cout << "Email: " << c.email << std::endl;
std::cout << "Company: " << c.company << std::endl;
std::cout << "Address: " << c.address << std::endl;
return;
}
}
std::cout << "Contact not found." << std::endl;
}
void AddressBook::sortContacts() {
std::sort(contacts_.begin(), contacts_.end(), [](const Contact& c1, const Contact& c2) {
return c1.name < c2.name;
});
}
void AddressBook::displayAllContacts() {
for (const auto& c : contacts_) {
std::cout << "Name: " << c.name << std::endl;
std::cout << "Phone: " << c.phone << std::endl;
std::cout << "Email: " << c.email << std::endl;
std::cout << "Company: " << c.company << std::endl;
std::cout << "Address: " << c.address << std::endl;
std::cout << std::endl;
}
}
void AddressBook::readContactsFromFile(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Failed to open file: " << filename << std::endl;
return;
}
std::string line;
while (std::getline(file, line)) {
std::vector<std::string> fields;
std::istringstream iss(line);
std::string field;
while (std::getline(iss, field, ',')) {
fields.push_back(field);
}
if (fields.size() != 5) {
std::cerr << "Invalid file format." << std::endl;
return;
}
Contact contact;
contact.name = fields[0];
contact.phone = fields[1];
contact.email = fields[2];
contact.company = fields[3];
contact.address = fields[4];
addContact(contact);
}
}
void AddressBook::saveContactsToFile(const std::string& filename) {
std::ofstream file(filename);
if (!file.is_open()) {
std::cerr << "Failed to open file: " << filename << std::endl;
return;
}
for (const auto& c : contacts_) {
file << c.name << "," << c.phone << "," << c.email << "," << c.company << "," << c.address << std::endl;
}
}
```
4. 在主函数中使用通讯录类
```
int main() {
AddressBook addressBook;
addressBook.readContactsFromFile("contacts.txt");
while (true) {
int choice;
std::cout << "1. Add contact" << std::endl;
std::cout << "2. Delete contact" << std::endl;
std::cout << "3. Modify contact" << std::endl;
std::cout << "4. Query contact" << std::endl;
std::cout << "5. Sort contacts" << std::endl;
std::cout << "6. Display all contacts" << std::endl;
std::cout << "7. Save contacts to file" << std::endl;
std::cout << "8. Exit" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1: {
Contact contact;
std::cout << "Enter name: ";
std::cin >> contact.name;
std::cout << "Enter phone: ";
std::cin >> contact.phone;
std::cout << "Enter email: ";
std::cin >> contact.email;
std::cout << "Enter company: ";
std::cin >> contact.company;
std::cout << "Enter address: ";
std::cin >> contact.address;
addressBook.addContact(contact);
std::cout << "Contact added." << std::endl;
break;
}
case 2: {
std::string name;
std::cout << "Enter name: ";
std::cin >> name;
addressBook.deleteContact(name);
std::cout << "Contact deleted." << std::endl;
break;
}
case 3: {
std::string name;
Contact contact;
std::cout << "Enter name: ";
std::cin >> name;
std::cout << "Enter phone: ";
std::cin >> contact.phone;
std::cout << "Enter email: ";
std::cin >> contact.email;
std::cout << "Enter company: ";
std::cin >> contact.company;
std::cout << "Enter address: ";
std::cin >> contact.address;
addressBook.modifyContact(name, contact);
std::cout << "Contact modified." << std::endl;
break;
}
case 4: {
std::string name;
std::cout << "Enter name: ";
std::cin >> name;
addressBook.queryContact(name);
break;
}
case 5: {
addressBook.sortContacts();
std::cout << "Contacts sorted." << std::endl;
break;
}
case 6: {
addressBook.displayAllContacts();
break;
}
case 7: {
addressBook.saveContactsToFile("contacts.txt");
std::cout << "Contacts saved to file." << std::endl;
break;
}
case 8: {
return 0;
}
default: {
std::cout << "Invalid choice." << std::endl;
break;
}
}
}
return 0;
}
```
这个程序框架可以实现你所需的基本功能,你可以根据需求进行修改和完善。
阅读全文