帮我用c++写一个通信录程序:并实现以下要求:基本要求: 定义人员(person)类,其中包括姓名、性别、电话号码、地址、邮政编码、邮箱、QQ号码和类别。 功能要求: 设计菜单实现功能选择; 输入功能:输入人员信息,并以txt格式保存到文件中; 查询功能: 能够根据姓名、电话精确查询人员信息; 能够根据地址进行模糊查询人员信息; 根据人员类别查询人员信息并输出显示; 能根据姓名、电话修改人员信息; 能根据姓名、电话删除人员信息; 根据姓名对人员信息排序输出; 根据QQ号码对人员信息排序输出; 根据电话号码对人员信息排序输出; 考虑重名问题的处理。 所有的增加、修改、删除能同步到txt文件;也从txt文件读取数据到程序。
时间: 2024-01-30 12:02:00 浏览: 103
好的,以下是一个简单的通信录程序,可以满足以上要求。请注意,这只是一个示例程序,具体实现可能会因人而异。
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class Person {
public:
string name;
string gender;
string phone;
string address;
string zipcode;
string email;
string qq;
string category;
};
void printPerson(Person p) {
cout << "姓名:" << p.name << endl;
cout << "性别:" << p.gender << endl;
cout << "电话:" << p.phone << endl;
cout << "地址:" << p.address << endl;
cout << "邮编:" << p.zipcode << endl;
cout << "邮箱:" << p.email << endl;
cout << "QQ号码:" << p.qq << endl;
cout << "类别:" << p.category << endl;
}
bool cmpByName(Person p1, Person p2) {
return p1.name < p2.name;
}
bool cmpByQQ(Person p1, Person p2) {
return p1.qq < p2.qq;
}
bool cmpByPhone(Person p1, Person p2) {
return p1.phone < p2.phone;
}
int main() {
vector<Person> contacts;
string filename = "contacts.txt";
ifstream infile(filename);
if (infile) {
// 从文件中读取数据
string line;
while (getline(infile, line)) {
Person p;
p.name = line;
getline(infile, p.gender);
getline(infile, p.phone);
getline(infile, p.address);
getline(infile, p.zipcode);
getline(infile, p.email);
getline(infile, p.qq);
getline(infile, p.category);
contacts.push_back(p);
}
infile.close();
}
while (true) {
cout << "请选择功能:" << endl;
cout << "1. 输入人员信息" << endl;
cout << "2. 查询人员信息" << endl;
cout << "3. 修改人员信息" << endl;
cout << "4. 删除人员信息" << endl;
cout << "5. 根据姓名排序输出" << endl;
cout << "6. 根据QQ号码排序输出" << endl;
cout << "7. 根据电话号码排序输出" << endl;
cout << "8. 退出程序" << endl;
int choice;
cin >> choice;
if (choice == 1) {
// 输入人员信息
Person p;
cout << "请输入姓名:";
cin >> p.name;
cout << "请输入性别:";
cin >> p.gender;
cout << "请输入电话:";
cin >> p.phone;
cout << "请输入地址:";
cin >> p.address;
cout << "请输入邮编:";
cin >> p.zipcode;
cout << "请输入邮箱:";
cin >> p.email;
cout << "请输入QQ号码:";
cin >> p.qq;
cout << "请输入类别:";
cin >> p.category;
contacts.push_back(p);
// 写入文件
ofstream outfile(filename, ios::app);
outfile << p.name << endl;
outfile << p.gender << endl;
outfile << p.phone << endl;
outfile << p.address << endl;
outfile << p.zipcode << endl;
outfile << p.email << endl;
outfile << p.qq << endl;
outfile << p.category << endl;
outfile.close();
cout << "添加成功!" << endl;
}
else if (choice == 2) {
// 查询人员信息
cout << "请选择查询方式:" << endl;
cout << "1. 根据姓名精确查询" << endl;
cout << "2. 根据电话精确查询" << endl;
cout << "3. 根据地址模糊查询" << endl;
cout << "4. 根据类别查询" << endl;
int queryChoice;
cin >> queryChoice;
if (queryChoice == 1) {
// 根据姓名精确查询
string name;
cout << "请输入姓名:";
cin >> name;
bool found = false;
for (Person p : contacts) {
if (p.name == name) {
printPerson(p);
found = true;
}
}
if (!found) {
cout << "未找到匹配的人员信息!" << endl;
}
}
else if (queryChoice == 2) {
// 根据电话精确查询
string phone;
cout << "请输入电话:";
cin >> phone;
bool found = false;
for (Person p : contacts) {
if (p.phone == phone) {
printPerson(p);
found = true;
}
}
if (!found) {
cout << "未找到匹配的人员信息!" << endl;
}
}
else if (queryChoice == 3) {
// 根据地址模糊查询
string address;
cout << "请输入地址:";
cin >> address;
bool found = false;
for (Person p : contacts) {
if (p.address.find(address) != string::npos) {
printPerson(p);
found = true;
}
}
if (!found) {
cout << "未找到匹配的人员信息!" << endl;
}
}
else if (queryChoice == 4) {
// 根据类别查询
string category;
cout << "请输入类别:";
cin >> category;
bool found = false;
for (Person p : contacts) {
if (p.category == category) {
printPerson(p);
found = true;
}
}
if (!found) {
cout << "未找到匹配的人员信息!" << endl;
}
}
else {
cout << "输入无效!" << endl;
}
}
else if (choice == 3) {
// 修改人员信息
string name, phone;
cout << "请输入要修改的人员姓名:";
cin >> name;
cout << "请输入要修改的人员电话:";
cin >> phone;
bool found = false;
for (int i = 0; i < contacts.size(); i++) {
if (contacts[i].name == name && contacts[i].phone == phone) {
Person p;
cout << "请输入新的信息:" << endl;
cout << "姓名:";
cin >> p.name;
cout << "性别:";
cin >> p.gender;
cout << "电话:";
cin >> p.phone;
cout << "地址:";
cin >> p.address;
cout << "邮编:";
cin >> p.zipcode;
cout << "邮箱:";
cin >> p.email;
cout << "QQ号码:";
cin >> p.qq;
cout << "类别:";
cin >> p.category;
contacts[i] = p;
found = true;
break;
}
}
if (found) {
// 更新文件
ofstream outfile(filename);
for (Person p : contacts) {
outfile << p.name << endl;
outfile << p.gender << endl;
outfile << p.phone << endl;
outfile << p.address << endl;
outfile << p.zipcode << endl;
outfile << p.email << endl;
outfile << p.qq << endl;
outfile << p.category << endl;
}
outfile.close();
cout << "修改成功!" << endl;
}
else {
cout << "未找到匹配的人员信息!" << endl;
}
}
else if (choice == 4) {
// 删除人员信息
string name, phone;
cout << "请输入要删除的人员姓名:";
cin >> name;
cout << "请输入要删除的人员电话:";
cin >> phone;
bool found = false;
for (int i = 0; i < contacts.size(); i++) {
if (contacts[i].name == name && contacts[i].phone == phone) {
contacts.erase(contacts.begin() + i);
found = true;
break;
}
}
if (found) {
// 更新文件
ofstream outfile(filename);
for (Person p : contacts) {
outfile << p.name << endl;
outfile << p.gender << endl;
outfile << p.phone << endl;
outfile << p.address << endl;
outfile << p.zipcode << endl;
outfile << p.email << endl;
outfile << p.qq << endl;
outfile << p.category << endl;
}
outfile.close();
cout << "删除成功!" << endl;
}
else {
cout << "未找到匹配的人员信息!" << endl;
}
}
else if (choice == 5) {
// 根据姓名排序输出
sort(contacts.begin(), contacts.end(), cmpByName);
for (Person p : contacts) {
printPerson(p);
}
}
else if (choice == 6) {
// 根据QQ号码排序输出
sort(contacts.begin(), contacts.end(), cmpByQQ);
for (Person p : contacts) {
printPerson(p);
}
}
else if (choice == 7) {
// 根据电话号码排序输出
sort(contacts.begin(), contacts.end(), cmpByPhone);
for (Person p : contacts) {
printPerson(p);
}
}
else if (choice == 8) {
// 退出程序
break;
}
else {
cout << "输入无效!" << endl;
}
}
return 0;
}
```
阅读全文
相关推荐















