编写C++代码,使用class类的方法,假没各戸数据包括:姓名、性別、眼多、申活、00、微信、公司。主要功能:1) 通沢 录信息录入功能;2通讯录信息修改、刪除功能:3,通讯录信息浏览功能:4)通讯录信息查询功能:5)按姓名排序功能:6)保行数据到文件。
时间: 2023-07-10 08:33:20 浏览: 81
好的,这是一个比较全面的通讯录管理系统,以下是C++代码实现:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
//定义联系人类
class Contact {
public:
string name; //姓名
string gender; //性别
string email; //邮箱
string phone; //电话
string qq; //QQ号
string wechat; //微信号
string company; //公司
Contact(string n, string g, string e, string p, string q, string w, string c) {
name = n;
gender = g;
email = e;
phone = p;
qq = q;
wechat = w;
company = c;
}
};
//定义通讯录类
class AddressBook {
private:
vector<Contact> contacts; //存储所有联系人信息的向量
public:
//添加联系人
void addContact() {
string n, g, e, p, q, w, c;
cout << "请输入姓名:";
cin >> n;
cout << "请输入性别:";
cin >> g;
cout << "请输入邮箱:";
cin >> e;
cout << "请输入电话:";
cin >> p;
cout << "请输入QQ号:";
cin >> q;
cout << "请输入微信号:";
cin >> w;
cout << "请输入公司:";
cin >> c;
Contact c1(n, g, e, p, q, w, c);
contacts.push_back(c1);
cout << "联系人添加成功!" << endl;
}
//修改联系人
void modifyContact() {
string n;
cout << "请输入要修改的联系人姓名:";
cin >> n;
bool flag = false;
for (int i = 0; i < contacts.size(); i++) {
if (contacts[i].name == n) {
string g, e, p, q, w, c;
cout << "请输入修改后的性别:";
cin >> g;
cout << "请输入修改后的邮箱:";
cin >> e;
cout << "请输入修改后的电话:";
cin >> p;
cout << "请输入修改后的QQ号:";
cin >> q;
cout << "请输入修改后的微信号:";
cin >> w;
cout << "请输入修改后的公司:";
cin >> c;
contacts[i].gender = g;
contacts[i].email = e;
contacts[i].phone = p;
contacts[i].qq = q;
contacts[i].wechat = w;
contacts[i].company = c;
flag = true;
cout << "联系人修改成功!" << endl;
break;
}
}
if (!flag) {
cout << "未找到该联系人!" << endl;
}
}
//删除联系人
void deleteContact() {
string n;
cout << "请输入要删除的联系人姓名:";
cin >> n;
bool flag = false;
for (int i = 0; i < contacts.size(); i++) {
if (contacts[i].name == n) {
contacts.erase(contacts.begin() + i);
flag = true;
cout << "联系人删除成功!" << endl;
break;
}
}
if (!flag) {
cout << "未找到该联系人!" << endl;
}
}
//浏览所有联系人
void showAllContacts() {
if (contacts.size() == 0) {
cout << "通讯录为空!" << endl;
return;
}
for (int i = 0; i < contacts.size(); i++) {
cout << "姓名:" << contacts[i].name << endl;
cout << "性别:" << contacts[i].gender << endl;
cout << "邮箱:" << contacts[i].email << endl;
cout << "电话:" << contacts[i].phone << endl;
cout << "QQ号:" << contacts[i].qq << endl;
cout << "微信号:" << contacts[i].wechat << endl;
cout << "公司:" << contacts[i].company << endl;
cout << endl;
}
}
//按姓名查询联系人
void searchByName() {
string n;
cout << "请输入要查询的联系人姓名:";
cin >> n;
bool flag = false;
for (int i = 0; i < contacts.size(); i++) {
if (contacts[i].name == n) {
cout << "姓名:" << contacts[i].name << endl;
cout << "性别:" << contacts[i].gender << endl;
cout << "邮箱:" << contacts[i].email << endl;
cout << "电话:" << contacts[i].phone << endl;
cout << "QQ号:" << contacts[i].qq << endl;
cout << "微信号:" << contacts[i].wechat << endl;
cout << "公司:" << contacts[i].company << endl;
flag = true;
break;
}
}
if (!flag) {
cout << "未找到该联系人!" << endl;
}
}
//按姓名排序
void sortByname() {
sort(contacts.begin(), contacts.end(), [](Contact a, Contact b) {return a.name < b.name; });
cout << "按姓名排序完成!" << endl;
}
//保存数据到文件
void saveToFile() {
ofstream out("contacts.txt");
for (int i = 0; i < contacts.size(); i++) {
out << contacts[i].name << " " << contacts[i].gender << " " << contacts[i].email << " "
<< contacts[i].phone << " " << contacts[i].qq << " " << contacts[i].wechat << " " << contacts[i].company << endl;
}
out.close();
cout << "数据保存成功!" << endl;
}
//从文件中读取数据
void readFromFile() {
ifstream in("contacts.txt");
if (!in) {
cout << "文件不存在!" << endl;
return;
}
string n, g, e, p, q, w, c;
while (in >> n >> g >> e >> p >> q >> w >> c) {
Contact c1(n, g, e, p, q, w, c);
contacts.push_back(c1);
}
in.close();
cout << "数据读取成功!" << endl;
}
};
int main() {
AddressBook ab;
int choice;
while (true) {
cout << "请选择操作:1.添加联系人 2.修改联系人 3.删除联系人 4.浏览所有联系人 5.按姓名查询联系人 6.按姓名排序 7.保存数据到文件 8.从文件中读取数据 0.退出" << endl;
cin >> choice;
switch (choice) {
case 1:
ab.addContact();
break;
case 2:
ab.modifyContact();
break;
case 3:
ab.deleteContact();
break;
case 4:
ab.showAllContacts();
break;
case 5:
ab.searchByName();
break;
case 6:
ab.sortByname();
break;
case 7:
ab.saveToFile();
break;
case 8:
ab.readFromFile();
break;
case 0:
cout << "已退出!" << endl;
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
注意,本程序还涉及到数据的保存与读取,需要将数据保存到文件中以便下次启动程序时读取。文件格式为每个联系人的信息占一行,不同信息之间用空格隔开。文件名可自行更改。
阅读全文