你作为一个资深的VS2022使用者需要用c++类和对象、构造函数、对象数组、类的组合、运算符重载、虚函数、派生类、文件操作等写一个简短易懂的代码 1.问题描述 统计某公司人员信息包括编号、姓名、性别、电话号码、学历、工资,设计一个程序灵活的实现各种功能。2.功能要求 (1)添加功能 (2)查询功能 (3)显示功能 (4)编辑功能 (5)删除功能 (6)排序功能 (7)保存功能 (8)读取功能 我需要你保持代码可以在VS2022上正常运行
时间: 2023-07-10 07:21:40 浏览: 115
好的,可以参考下面的代码实现:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义员工类
class Employee {
public:
int id; // 员工编号
string name; // 员工姓名
char gender; // 员工性别
string phone; // 员工电话号码
string education; // 员工学历
double salary; // 员工工资
// 构造函数
Employee() {}
Employee(int id, string name, char gender, string phone, string education, double salary) :
id(id), name(name), gender(gender), phone(phone), education(education), salary(salary) {}
// 打印员工信息
void print() {
cout << "编号:" << id << ",姓名:" << name << ",性别:" << gender << ",电话号码:" << phone
<< ",学历:" << education << ",工资:" << salary << endl;
}
// 重载小于运算符,用于排序
bool operator< (const Employee& e) const {
return id < e.id;
}
};
// 定义员工管理类
class EmployeeManager {
private:
vector<Employee> employees; // 员工列表
public:
// 添加员工
void addEmployee() {
int id;
string name, phone, education;
char gender;
double salary;
cout << "请输入员工编号:";
cin >> id;
cout << "请输入员工姓名:";
cin >> name;
cout << "请输入员工性别(m/f):";
cin >> gender;
cout << "请输入员工电话号码:";
cin >> phone;
cout << "请输入员工学历:";
cin >> education;
cout << "请输入员工工资:";
cin >> salary;
Employee e(id, name, gender, phone, education, salary);
employees.push_back(e);
cout << "添加成功!" << endl;
}
// 查询员工
void queryEmployee() {
int id;
cout << "请输入要查询的员工编号:";
cin >> id;
for (int i = 0; i < employees.size(); i++) {
if (employees[i].id == id) {
employees[i].print();
return;
}
}
cout << "未找到该员工!" << endl;
}
// 显示所有员工
void showEmployees() {
if (employees.empty()) {
cout << "员工列表为空!" << endl;
return;
}
for (int i = 0; i < employees.size(); i++) {
employees[i].print();
}
}
// 编辑员工信息
void editEmployee() {
int id;
cout << "请输入要编辑的员工编号:";
cin >> id;
for (int i = 0; i < employees.size(); i++) {
if (employees[i].id == id) {
cout << "请输入员工姓名:";
cin >> employees[i].name;
cout << "请输入员工性别(m/f):";
cin >> employees[i].gender;
cout << "请输入员工电话号码:";
cin >> employees[i].phone;
cout << "请输入员工学历:";
cin >> employees[i].education;
cout << "请输入员工工资:";
cin >> employees[i].salary;
cout << "编辑成功!" << endl;
return;
}
}
cout << "未找到该员工!" << endl;
}
// 删除员工
void deleteEmployee() {
int id;
cout << "请输入要删除的员工编号:";
cin >> id;
for (int i = 0; i < employees.size(); i++) {
if (employees[i].id == id) {
employees.erase(employees.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该员工!" << endl;
}
// 按编号排序
void sortById() {
sort(employees.begin(), employees.end());
cout << "排序成功!" << endl;
}
// 保存员工信息到文件
void saveToFile() {
ofstream ofs("employees.txt", ios::out);
if (ofs.is_open()) {
for (int i = 0; i < employees.size(); i++) {
ofs << employees[i].id << " " << employees[i].name << " " << employees[i].gender << " "
<< employees[i].phone << " " << employees[i].education << " " << employees[i].salary << endl;
}
ofs.close();
cout << "保存成功!" << endl;
}
else {
cout << "文件打开失败!" << endl;
}
}
// 从文件读取员工信息
void readFromFile() {
ifstream ifs("employees.txt", ios::in);
if (ifs.is_open()) {
employees.clear();
int id;
string name, phone, education;
char gender;
double salary;
while (ifs >> id >> name >> gender >> phone >> education >> salary) {
Employee e(id, name, gender, phone, education, salary);
employees.push_back(e);
}
ifs.close();
cout << "读取成功!" << endl;
}
else {
cout << "文件打开失败!" << endl;
}
}
};
int main() {
EmployeeManager em; // 员工管理类
int choice;
do {
cout << "请选择功能:" << endl;
cout << "1.添加员工" << endl;
cout << "2.查询员工" << endl;
cout << "3.显示所有员工" << endl;
cout << "4.编辑员工信息" << endl;
cout << "5.删除员工" << endl;
cout << "6.按编号排序" << endl;
cout << "7.保存员工信息到文件" << endl;
cout << "8.从文件读取员工信息" << endl;
cout << "0.退出" << endl;
cin >> choice;
switch (choice) {
case 1:
em.addEmployee();
break;
case 2:
em.queryEmployee();
break;
case 3:
em.showEmployees();
break;
case 4:
em.editEmployee();
break;
case 5:
em.deleteEmployee();
break;
case 6:
em.sortById();
break;
case 7:
em.saveToFile();
break;
case 8:
em.readFromFile();
break;
case 0:
break;
default:
cout << "输入有误!" << endl;
break;
}
} while (choice != 0);
return 0;
}
```
在 VS2022 上运行时,需要注意:
1. 需要将文件后缀名改为 .cpp;
2. 可以选择使用 Visual C++ 2019 或者 Visual C++ 2022 编译器编译;
3. 测试时可以将员工信息保存到 employees.txt 文件中,读取时也需要使用该文件。
阅读全文