请帮我写一个通讯录信息管理系统 要求: (1)数据结构:通讯人员信息结点类型以及相应的结构体; (2)存储结构:选择单链表存储通讯人员信息。 (3)实现功能(以函数方式实现):通讯信息链表创建、通讯信息输入、通讯信息查询、通讯信息修改、 通讯信息删除、通讯信息排序、输出或保存到文件。 附:通讯录信息包括:编号、姓名、电话、通信地址、EMAIL 等。
时间: 2023-08-08 16:04:28 浏览: 91
好的,我可以帮你写一个通讯录信息管理系统。具体代码如下:
```c++
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// 通讯人员信息结构体
struct ContactPerson {
int id; // 编号
string name; // 姓名
string phone; // 电话
string address; // 通信地址
string email; // 邮箱
ContactPerson *next; // 指向下一个结点的指针
};
// 通讯信息链表创建
ContactPerson *createContactList() {
ContactPerson *head = new ContactPerson();
head->next = NULL;
return head;
}
// 通讯信息输入
void addContactPerson(ContactPerson *head) {
int id;
string name, phone, address, email;
cout << "请输入通讯人员信息:" << endl;
cout << "编号:";
cin >> id;
cout << "姓名:";
cin >> name;
cout << "电话:";
cin >> phone;
cout << "通信地址:";
cin >> address;
cout << "邮箱:";
cin >> email;
ContactPerson *newNode = new ContactPerson();
newNode->id = id;
newNode->name = name;
newNode->phone = phone;
newNode->address = address;
newNode->email = email;
// 插入到链表尾部
ContactPerson *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = newNode;
newNode->next = NULL;
cout << "添加成功!" << endl;
}
// 通讯信息查询
void searchContactPerson(ContactPerson *head) {
int choice;
string value;
cout << "请输入查询方式:" << endl;
cout << "1. 按编号查询" << endl;
cout << "2. 按姓名查询" << endl;
cin >> choice;
ContactPerson *p = head->next;
bool flag = false;
switch (choice) {
case 1: // 按编号查询
cout << "请输入要查询的编号:";
cin >> value;
while (p != NULL) {
if (to_string(p->id) == value) {
cout << "编号:" << p->id << " 姓名:" << p->name << " 电话:" << p->phone << " 通信地址:" << p->address << " 邮箱:" << p->email << endl;
flag = true;
}
p = p->next;
}
break;
case 2: // 按姓名查询
cout << "请输入要查询的姓名:";
cin >> value;
while (p != NULL) {
if (pname == value) {
cout << "编号:" << p->id << " 姓名:" << p->name << " 电话:" << p->phone << " 通信地址:" << p->address << " 邮箱:" << p->email << endl;
flag = true;
}
p = p->next;
}
break;
default:
cout << "输入有误!" << endl;
break;
}
if (!flag) {
cout << "未找到对应记录!" << endl;
}
}
// 通讯信息修改
void modifyContactPerson(ContactPerson *head) {
int choice;
string value;
cout << "请输入修改方式:" << endl;
cout << "1. 按编号修改" << endl;
cout << "2. 按姓名修改" << endl;
cin >> choice;
ContactPerson *p = head->next;
bool flag = false;
switch (choice) {
case 1: // 按编号修改
cout << "请输入要修改的编号:";
cin >> value;
while (p != NULL) {
if (to_string(p->id) == value) {
cout << "请输入修改后的姓名:";
cin >> p->name;
cout << "请输入修改后的电话:";
cin >> p->phone;
cout << "请输入修改后的通信地址:";
cin >> p->address;
cout << "请输入修改后的邮箱:";
cin >> p->email;
cout << "修改成功!" << endl;
flag = true;
}
p = p->next;
}
break;
case 2: // 按姓名修改
cout << "请输入要修改的姓名:";
cin >> value;
while (p != NULL) {
if (p->name == value) {
cout << "请输入修改后的编号:";
cin >> p->id;
cout << "请输入修改后的电话:";
cin >> p->phone;
cout << "请输入修改后的通信地址:";
cin >> p->address;
cout << "请输入修改后的邮箱:";
cin >> p->email;
cout << "修改成功!" << endl;
flag = true;
}
p = p->next;
}
break;
default:
cout << "输入有误!" << endl;
break;
}
if (!flag) {
cout << "未找到对应记录!" << endl;
}
}
// 通讯信息删除
void deleteContactPerson(ContactPerson *head) {
int choice;
string value;
cout << "请输入删除方式:" << endl;
cout << "1. 按编号删除" << endl;
cout << "2. 按姓名删除" << endl;
cin >> choice;
ContactPerson *p = head->next;
ContactPerson *pre = head;
bool flag = false;
switch (choice) {
case 1: // 按编号删除
cout << "请输入要删除的编号:";
cin >> value;
while (p != NULL) {
if (to_string(p->id) == value) {
pre->next = p->next;
delete p;
cout << "删除成功!" << endl;
flag = true;
break;
}
pre = p;
p = p->next;
}
break;
case 2: // 按姓名删除
cout << "请输入要删除的姓名:";
cin >> value;
while (p != NULL) {
if (p->name == value) {
pre->next = p->next;
delete p;
cout << "删除成功!" << endl;
flag = true;
break;
}
pre = p;
p = p->next;
}
break;
default:
cout << "输入有误!" << endl;
break;
}
if (!flag) {
cout << "未找到对应记录!" << endl;
}
}
// 通讯信息排序
void sortContactPerson(ContactPerson *head) {
int choice;
cout << "请输入排序方式:" << endl;
cout << "1. 按编号排序" << endl;
cout << "2. 按姓名排序" << endl;
cout << "3. 按电话排序" << endl;
cin >> choice;
bool flag = true;
ContactPerson *p, *q, *pre;
while (flag) {
flag = false;
pre = head;
p = pre->next;
q = p->next;
while (q != NULL) {
switch (choice) {
case 1: // 按编号排序
if (p->id > q->id) {
p->next = q->next;
q->next = p;
pre->next = q;
pre = q;
q = p->next;
flag = true;
} else {
pre = p;
p = q;
q = q->next;
}
break;
case 2: // 按姓名排序
if (p->name > q->name) {
p->next = q->next;
q->next = p;
pre->next = q;
pre = q;
q = p->next;
flag = true;
} else {
pre = p;
p = q;
q = q->next;
}
break;
case 3: // 按电话排序
if (p->phone > q->phone) {
p->next = q->next;
q->next = p;
pre->next = q;
pre = q;
q = p->next;
flag = true;
} else {
pre = p;
p = q;
q = q->next;
}
break;
default:
cout << "输入有误!" << endl;
break;
}
}
}
cout << "排序完成!" << endl;
}
// 输出通讯录信息到文件
void outputToFile(ContactPerson *head) {
string fileName;
cout << "请输入要保存到的文件名:";
cin >> fileName;
ofstream outFile(fileName);
if (outFile.is_open()) {
ContactPerson *p = head->next;
while (p != NULL) {
outFile << "编号:" << p->id << " 姓名:" << p->name << " 电话:" << p->phone << " 通信地址:" << p->address << " 邮箱:" << p->email << endl;
p = p->next;
}
cout << "保存成功!" << endl;
} else {
cout << "文件打开失败!" << endl;
}
outFile.close();
}
// 主函数
int main() {
ContactPerson *head = createContactList();
int choice = 0;
while (choice != 7) {
cout << "请选择功能:" << endl;
cout << "1. 添加通讯人员信息" << endl;
cout << "2. 查询通讯人员信息" << endl;
cout << "3. 修改通讯人员信息" << endl;
cout << "4. 删除通讯人员信息" << endl;
cout << "5. 排序通讯人员信息" << endl;
cout << "6. 输出通讯录信息到文件" << endl;
cout << "7. 退出" << endl;
cin >> choice;
switch (choice) {
case 1:
addContactPerson(head);
break;
case 2:
searchContactPerson(head);
break;
case 3:
modifyContactPerson(head);
break;
case 4:
deleteContactPerson(head);
break;
case 5:
sortContactPerson(head);
break;
case 6:
outputToFile(head);
break;
case 7:
break;
default:
cout << "输入有误!" << endl;
break;
}
}
return 0;
}
```
这个程序实现了你要求的所有功能,包括通讯信息链表创建、通讯信息输入、通讯信息查询、通讯信息修改、通讯信息删除、通讯信息排序、输出通讯录信息到文件。你可以根据需要进行修改,比如修改文件输出路径等。
阅读全文