c++信息管理系统,要求有工程师的信息添加、修改、删除、排序、查找、显示、保存,然后退出。工程师要有工号、姓名、性别、学历、年龄、工资、地址、电话。
时间: 2023-12-16 15:04:46 浏览: 33
以下是一个简单的C++信息管理系统,包括工程师的信息添加、修改、删除、排序、查找、显示、保存和退出功能。工程师的信息包括工号、姓名、性别、学历、年龄、工资、地址、电话。
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 工程师类
class Engineer {
public:
Engineer(int id, string name, string gender, string education, int age, double salary, string address, string phone) {
this->id = id;
this->name = name;
this->gender = gender;
this->education = education;
this->age = age;
this->salary = salary;
this->address = address;
this->phone = phone;
}
int getId() const {
return id;
}
string getName() const {
return name;
}
string getGender() const {
return gender;
}
string getEducation() const {
return education;
}
int getAge() const {
return age;
}
double getSalary() const {
return salary;
}
string getAddress() const {
return address;
}
string getPhone() const {
return phone;
}
void setName(string name) {
this->name = name;
}
void setGender(string gender) {
this->gender = gender;
}
void setEducation(string education) {
this->education = education;
}
void setAge(int age) {
this->age = age;
}
void setSalary(double salary) {
this->salary = salary;
}
void setAddress(string address) {
this->address = address;
}
void setPhone(string phone) {
this->phone = phone;
}
private:
int id; // 工号
string name; // 姓名
string gender; // 性别
string education; // 学历
int age; // 年龄
double salary; // 工资
string address; // 地址
string phone; // 电话
};
// 全局变量
vector<Engineer> engineers; // 工程师列表
string filename = "engineers.txt"; // 文件名
// 函数声明
void addEngineer();
void updateEngineer();
void deleteEngineer();
void sortEngineers();
void searchEngineer();
void displayEngineers();
void saveEngineers();
void loadEngineers();
void exitSystem();
// 主函数
int main() {
loadEngineers(); // 加载工程师列表
while (true) {
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;
int choice;
cin >> choice;
switch (choice) {
case 1:
addEngineer();
break;
case 2:
updateEngineer();
break;
case 3:
deleteEngineer();
break;
case 4:
sortEngineers();
break;
case 5:
searchEngineer();
break;
case 6:
displayEngineers();
break;
case 7:
saveEngineers();
break;
case 8:
exitSystem();
break;
default:
cout << "无效的操作,请重新选择" << endl;
break;
}
}
return 0;
}
// 添加工程师
void addEngineer() {
cout << "请输入工号:";
int id;
cin >> id;
cout << "请输入姓名:";
string name;
cin >> name;
cout << "请输入性别:";
string gender;
cin >> gender;
cout << "请输入学历:";
string education;
cin >> education;
cout << "请输入年龄:";
int age;
cin >> age;
cout << "请输入工资:";
double salary;
cin >> salary;
cout << "请输入地址:";
string address;
cin >> address;
cout << "请输入电话:";
string phone;
cin >> phone;
Engineer engineer(id, name, gender, education, age, salary, address, phone);
engineers.push_back(engineer);
cout << "添加成功" << endl;
}
// 修改工程师
void updateEngineer() {
cout << "请输入要修改的工程师的工号:";
int id;
cin >> id;
bool found = false;
for (int i = 0; i < engineers.size(); i++) {
if (engineers[i].getId() == id) {
found = true;
cout << "请输入新的姓名:";
string name;
cin >> name;
engineers[i].setName(name);
cout << "请输入新的性别:";
string gender;
cin >> gender;
engineers[i].setGender(gender);
cout << "请输入新的学历:";
string education;
cin >> education;
engineers[i].setEducation(education);
cout << "请输入新的年龄:";
int age;
cin >> age;
engineers[i].setAge(age);
cout << "请输入新的工资:";
double salary;
cin >> salary;
engineers[i].setSalary(salary);
cout << "请输入新的地址:";
string address;
cin >> address;
engineers[i].setAddress(address);
cout << "请输入新的电话:";
string phone;
cin >> phone;
engineers[i].setPhone(phone);
cout << "修改成功" << endl;
break;
}
}
if (!found) {
cout << "未找到该工程师" << endl;
}
}
// 删除工程师
void deleteEngineer() {
cout << "请输入要删除的工程师的工号:";
int id;
cin >> id;
bool found = false;
for (int i = 0; i < engineers.size(); i++) {
if (engineers[i].getId() == id) {
found = true;
engineers.erase(engineers.begin() + i);
cout << "删除成功" << endl;
break;
}
}
if (!found) {
cout << "未找到该工程师" << endl;
}
}
// 排序工程师
void sortEngineers() {
cout << "请选择排序方式:" << endl;
cout << "1. 按工号升序" << endl;
cout << "2. 按工号降序" << endl;
cout << "3. 按年龄升序" << endl;
cout << "4. 按年龄降序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
sort(engineers.begin(), engineers.end(), [](Engineer a, Engineer b) {
return a.getId() < b.getId();
});
cout << "按工号升序排序成功" << endl;
break;
case 2:
sort(engineers.begin(), engineers.end(), [](Engineer a, Engineer b) {
return a.getId() > b.getId();
});
cout << "按工号降序排序成功" << endl;
break;
case 3:
sort(engineers.begin(), engineers.end(), [](Engineer a, Engineer b) {
return a.getAge() < b.getAge();
});
cout << "按年龄升序排序成功" << endl;
break;
case 4:
sort(engineers.begin(), engineers.end(), [](Engineer a, Engineer b) {
return a.getAge() > b.getAge();
});
cout << "按年龄降序排序成功" << endl;
break;
default:
cout << "无效的排序方式,请重新选择" << endl;
break;
}
}
// 查找工程师
void searchEngineer() {
cout << "请选择查找方式:" << endl;
cout << "1. 按工号查找" << endl;
cout << "2. 按姓名查找" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入要查找的工程师的工号:";
int id;
cin >> id;
for (int i = 0; i < engineers.size(); i++) {
if (engineers[i].getId() == id) {
cout << "工号\t姓名\t性别\t学历\t年龄\t工资\t地址\t电话" << endl;
cout << engineers[i].getId() << "\t" << engineers[i].getName() << "\t" << engineers[i].getGender() << "\t" << engineers[i].getEducation() << "\t" << engineers[i].getAge() << "\t" << engineers[i].getSalary() << "\t" << engineers[i].getAddress() << "\t" << engineers[i].getPhone() << endl;
return;
}
}
cout << "未找到该工程师" << endl;
break;
case 2:
cout << "请输入要查找的工程师的姓名:";
string name;
cin >> name;
for (int i = 0; i < engineers.size(); i++) {
if (engineers[i].getName() == name) {
cout << "工号\t姓名\t性别\t学历\t年龄\t工资\t地址\t电话" << endl;
cout << engineers[i].getId() << "\t" << engineers[i].getName() << "\t" << engineers[i].getGender() << "\t" << engineers[i].getEducation() << "\t" << engineers[i].getAge() << "\t" << engineers[i].getSalary() << "\t" << engineers[i].getAddress() << "\t" << engineers[i].getPhone() << endl;
return;
}
}
cout << "未找到该工程师" << endl;
break;
default:
cout << "无效的查找方式,请重新选择" << endl;
break;
}
}
// 显示工程师
void displayEngineers() {
cout << "工号\t姓名\t性别\t学历\t年龄\t工资\t地址\t电话" << endl;
for (int i = 0; i < engineers.size(); i++) {
cout << engineers[i].getId() << "\t" << engineers[i].getName() << "\t" << engineers[i].getGender() << "\t" << engineers[i].getEducation() << "\t" << engineers[i].getAge() << "\t" << engineers[i].getSalary() << "\t" << engineers[i].getAddress() << "\t" << engineers[i].getPhone() << endl;
}
}
// 保存工程师
void saveEngineers() {
ofstream fout(filename);
for (int i = 0; i < engineers.size(); i++) {
fout << engineers[i].getId() << " " << engineers[i].getName() << " " << engineers[i].getGender() << " " << engineers[i].getEducation() << " " << engineers[i].getAge() << " " << engineers[i].getSalary() << " " << engineers[i].getAddress() << " " << engineers[i].getPhone() << endl;
}
fout.close();
cout << "保存成功" << endl;
}
// 加载工程师
void loadEngineers() {
ifstream fin(filename);
if (!fin) {
cout << "未找到文件,将创建新文件" << endl;
return;
}
while (!fin.eof()) {
int id;
string name;
string gender;
string education;
int age;
double salary;
string address;
string phone;
fin >> id >> name >> gender >> education >> age >> salary >> address >> phone;
if (fin.fail()) {
break;
}
Engineer engineer(id, name, gender, education, age, salary, address, phone);
engineers.push_back(engineer);
}
fin.close();
cout << "加载成功" << endl;
}
// 退出系统
void exitSystem() {
cout << "感谢使用" << endl;
exit(0);
}
```