用c语言或者c++来实现模拟手机通信录管理系统 主要功能有: (1) 查看功能:选择此功能,列出手机通信录得分类,如A同时、B家人、C朋友、D同学等分类,但选中某分类时,像是出此类所有数据中的姓名和电话号码; (2) 增加功能:录入新数据(包括姓名、电话号码、分类),当录入重复的姓名和电话号码时,提示结果错误并取消录入。 (3) 修改功能:对选择(选择的方式自定)的联系人,修改其通信录的相关信息,但如果修改姓名,不能跟其他已有的联系人的姓名不能重复。 (4) 删除功能:选择某个联系人的姓名,可对此人的相应数据进行删除。 (5) 其他功能:自行分析设计有特色效果的功能。(作为加分点)如排序、分组等。 (6) 要求:设计合理的数据结构,实现合理的操作界面。
时间: 2024-03-24 19:40:15 浏览: 18
好的,这个项目可以用C或者C++来实现。下面是一个简单的代码示例:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Contact {
string name;
string phone;
string category;
};
vector<Contact> contacts;
void displayContacts() {
cout << "Categories:" << endl;
vector<string> categories;
for (auto c : contacts) {
if (find(categories.begin(), categories.end(), c.category) == categories.end()) {
categories.push_back(c.category);
}
}
for (auto c : categories) {
cout << c << endl;
}
cout << endl << "Contacts:" << endl;
for (auto c : contacts) {
cout << c.name << " - " << c.phone << " (" << c.category << ")" << endl;
}
}
void addContact() {
Contact c;
cout << "Enter name: ";
cin >> c.name;
cout << "Enter phone: ";
cin >> c.phone;
cout << "Enter category: ";
cin >> c.category;
auto it = find_if(contacts.begin(), contacts.end(), [&](Contact& x) {
return x.name == c.name || x.phone == c.phone;
});
if (it != contacts.end()) {
cout << "Error: Contact with same name or phone already exists" << endl;
} else {
contacts.push_back(c);
cout << "Contact added successfully" << endl;
}
}
void modifyContact() {
string name;
cout << "Enter name of contact to modify: ";
cin >> name;
auto it = find_if(contacts.begin(), contacts.end(), [&](Contact& x) {
return x.name == name;
});
if (it == contacts.end()) {
cout << "Error: Contact not found" << endl;
} else {
cout << "Enter new phone: ";
cin >> it->phone;
cout << "Enter new category: ";
cin >> it->category;
cout << "Contact modified successfully" << endl;
}
}
void deleteContact() {
string name;
cout << "Enter name of contact to delete: ";
cin >> name;
auto it = find_if(contacts.begin(), contacts.end(), [&](Contact& x) {
return x.name == name;
});
if (it == contacts.end()) {
cout << "Error: Contact not found" << endl;
} else {
contacts.erase(it);
cout << "Contact deleted successfully" << endl;
}
}
int main() {
int choice;
do {
cout << endl << "Phonebook" << endl;
cout << "1. Display contacts" << endl;
cout << "2. Add contact" << endl;
cout << "3. Modify contact" << endl;
cout << "4. Delete contact" << endl;
cout << "5. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
displayContacts();
break;
case 2:
addContact();
break;
case 3:
modifyContact();
break;
case 4:
deleteContact();
break;
case 5:
break;
default:
cout << "Invalid choice" << endl;
break;
}
} while (choice != 5);
return 0;
}
```
这个示例实现了基本的功能,包括查看、增加、修改和删除联系人。您可以根据自己的需求添加其他功能。