用c++编写一个以班为单位进行通讯录信息管理
时间: 2023-09-07 20:12:16 浏览: 100
好的,以下是一个基于 C++ 的班级通讯录信息管理程序,实现了添加、修改、查找、删除等功能:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 记录结构体
struct Record {
int id; // 序号
string name; // 姓名
string gender; // 性别
int age; // 年龄
string phone; // 电话
string hometown; // 籍贯
string address; // 家庭住址
string mailing_address; // 通信地址
string postal_code; // 邮编
};
// 班级通讯录类
class AddressBook {
public:
// 添加记录
void addRecord() {
Record record;
record.id = records.size() + 1;
cout << "姓名:";
cin >> record.name;
cout << "性别:";
cin >> record.gender;
cout << "年龄:";
cin >> record.age;
cout << "电话:";
cin >> record.phone;
cout << "籍贯:";
cin >> record.hometown;
cout << "家庭住址:";
cin >> record.address;
cout << "通信地址:";
cin >> record.mailing_address;
cout << "邮编:";
cin >> record.postal_code;
records.push_back(record);
cout << "记录添加成功!" << endl;
}
// 修改记录
void modifyRecord() {
int id;
cout << "请输入要修改的记录的序号:";
cin >> id;
if (id <= 0 || id > records.size()) {
cout << "找不到该记录!" << endl;
return;
}
Record& record = records[id - 1];
cout << "姓名(" << record.name << "):";
cin >> record.name;
cout << "性别(" << record.gender << "):";
cin >> record.gender;
cout << "年龄(" << record.age << "):";
cin >> record.age;
cout << "电话(" << record.phone << "):";
cin >> record.phone;
cout << "籍贯(" << record.hometown << "):";
cin >> record.hometown;
cout << "家庭住址(" << record.address << "):";
cin >> record.address;
cout << "通信地址(" << record.mailing_address << "):";
cin >> record.mailing_address;
cout << "邮编(" << record.postal_code << "):";
cin >> record.postal_code;
cout << "记录修改成功!" << endl;
}
// 查找记录
void searchRecord() {
string keyword;
cout << "请输入关键字:";
cin >> keyword;
vector<Record> results;
for (const auto& record : records) {
if (record.name.find(keyword) != string::npos ||
record.phone.find(keyword) != string::npos ||
record.hometown.find(keyword) != string::npos ||
record.address.find(keyword) != string::npos ||
record.mailing_address.find(keyword) != string::npos ||
record.postal_code.find(keyword) != string::npos) {
results.push_back(record);
}
}
if (results.empty()) {
cout << "没有找到匹配的记录!" << endl;
} else {
cout << "共找到" << results.size() << "条记录:" << endl;
for (const auto& record : results) {
printRecord(record);
}
}
}
// 删除记录
void deleteRecord() {
int id;
cout << "请输入要删除的记录的序号:";
cin >> id;
if (id <= 0 || id > records.size()) {
cout << "找不到该记录!" << endl;
return;
}
records.erase(records.begin() + id - 1);
cout << "记录删除成功!" << endl;
}
// 显示所有记录
void showAllRecords() {
if (records.empty()) {
cout << "没有记录!" << endl;
} else {
for (const auto& record : records) {
printRecord(record);
}
}
}
private:
// 记录列表
vector<Record> records;
// 打印记录
void printRecord(const Record& record) {
cout << "序号:" << record.id << endl;
cout << "姓名:" << record.name << endl;
cout << "性别:" << record.gender << endl;
cout << "年龄:" << record.age << endl;
cout << "电话:" << record.phone << endl;
cout << "籍贯:" << record.hometown << endl;
cout << "家庭住址:" << record.address << endl;
cout << "通信地址:" << record.mailing_address << endl;
cout << "邮编:" << record.postal_code << endl;
cout << endl;
}
};
// 程序入口
int main() {
AddressBook addressBook;
while (true) {
cout << "请选择功能:" << endl;
cout << "1. 添加记录" << endl;
cout << "2. 修改记录" << endl;
cout << "3. 查找记录" << endl;
cout << "4. 删除记录" << endl;
cout << "5. 显示所有记录" << endl;
cout << "6. 退出程序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addressBook.addRecord();
break;
case 2:
addressBook.modifyRecord();
break;
case 3:
addressBook.searchRecord();
break;
case 4:
addressBook.deleteRecord();
break;
case 5:
addressBook.showAllRecords();
break;
case 6:
return 0;
default:
cout << "无效的选择!" << endl;
}
}
}
```
以上是一个简单的班级通讯录信息管理程序,可以根据实际需求进行修改和完善。
阅读全文