用VC实现c++管理系统,要求有工程师的信息添加、修改、删除、排序、查找、显示、保存,然后退出。工程师要有工号、姓名、性别、学历、年龄、工资、地址、电话。
时间: 2023-12-22 17:04:24 浏览: 28
以下是用VC实现c++管理系统的代码,包括工程师的信息添加、修改、删除、排序、查找、显示、保存和退出功能。其中工程师有工号、姓名、性别、学历、年龄、工资、地址、电话等信息。
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Engineer {
public:
string id; // 工号
string name; // 姓名
string gender; // 性别
string education; // 学历
int age; // 年龄
double salary; // 工资
string address; // 地址
string phone; // 电话
Engineer(string 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;
}
void display() {
cout << "工号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "学历:" << education << endl;
cout << "年龄:" << age << endl;
cout << "工资:" << salary << endl;
cout << "地址:" << address << endl;
cout << "电话:" << phone << endl;
}
};
vector<Engineer> engineers; // 存储所有工程师信息的vector
// 添加工程师信息
void addEngineer() {
string id, name, gender, education, address, phone;
int age;
double salary;
cout << "请输入工号:";
cin >> id;
cout << "请输入姓名:";
cin >> name;
cout << "请输入性别:";
cin >> gender;
cout << "请输入学历:";
cin >> education;
cout << "请输入年龄:";
cin >> age;
cout << "请输入工资:";
cin >> salary; cout << "请输入地址:";
cin >> address;
cout << "请输入电话:";
cin >> phone;
Engineer engineer(id, name, gender, education, age, salary, address, phone);
engineers.push_back(engineer);
cout << "添加成功!" << endl;
}
// 修改工程师信息
void modifyEngineer() {
string id;
cout << "请输入要修改的工程师的工号:";
cin >> id;
for (int i = 0; i < engineers.size(); i++) {
if (engineers[i].id == id) {
string name, gender, education, address, phone;
int age;
double salary;
cout << "请输入新的姓名:";
cin >> name;
cout << "请输入新的性别:";
cin >> gender;
cout << "请输入新的学历:";
cin >> education;
cout << "请输入新的年龄:";
cin >> age;
cout << "请输入新的工资:";
cin >> salary;
cout << "请输入新的地址:";
cin >> address;
cout << "请输入新的电话:";
cin >> phone;
engineers[i].name = name;
engineers[i].gender = gender;
engineers[i].education = education;
engineers[i].age = age;
engineers[i].salary = salary;
engineers[i].address = address;
engineers[i].phone = phone;
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该工程师!" << endl;
}
// 删除工程师信息
void deleteEngineer() {
string id;
cout << "请输入要删除的工程师的工号:";
cin >> id;
for (int i = 0; i < engineers.size(); i++) {
if (engineers[i].id == id) {
engineers.erase(engineers.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该工程师!" << endl;
}
// 按工号排序
bool cmpById(const Engineer& e1, const Engineer& e2) {
return e1.id < e2.id;
}
// 按姓名排序
bool cmpByName(const Engineer& e1, const Engineer& e2) {
return e1.name < e2.name;
}
// 按年龄排序
bool cmpByAge(const Engineer& e1, const Engineer& e2) {
return e1.age < e2.age;
}
// 排序工程师信息
void sortEngineers() {
int choice;
cout << "请选择排序方式:" << endl;
cout << "1.按工号排序" << endl;
cout << "2.按姓名排序" << endl;
cout << "3.按年龄排序" << endl;
cin >> choice;
switch (choice) {
case 1:
sort(engineers.begin(), engineers.end(), cmpById);
cout << "按工号排序成功!" << endl;
break;
case 2:
sort(engineers.begin(), engineers.end(), cmpByName);
cout << "按姓名排序成功!" << endl;
break;
case 3:
sort(engineers.begin(), engineers.end(), cmpByAge);
cout << "按年龄排序成功!" << endl;
break;
default:
cout << "无效的选择!" << endl;
break;
}
}
// 查找工程师信息
void searchEngineer() {
string id;
cout << "请输入要查找的工程师的工号:";
cin >> id;
for (int i = 0; i < engineers.size(); i++) {
if (engineers[i].id == id) {
engineers[i].display();
return;
}
}
cout << "未找到该工程师!" << endl;
}
// 显示所有工程师信息
void displayEngineers() {
for (int i = 0; i < engineers.size(); i++) {
engineers[i].display();
cout << endl;
}
}
// 保存工程师信息到文件
void saveEngineers() {
ofstream fout("engineers.txt");
for (int i = 0; i < engineers.size(); i++) {
fout << engineers[i].id << " " << engineers[i].name << " " << engineers[i].gender << " " << engineers[i].education << " " << engineers[i].age << " " << engineers[i].salary << " " << engineers[i].address << " " << engineers[i].phone << endl;
}
fout.close();
cout << "保存成功!" << endl;
}
// 从文件中读取工程师信息
void loadEngineers() {
ifstream fin("engineers.txt");
string id, name, gender, education, address, phone;
int age;
double salary;
while (fin >> id >> name >> gender >> education >> age >> salary >> address >> phone) {
Engineer engineer(id, name, gender, education, age, salary, address, phone);
engineers.push_back(engineer);
}
fin.close();
cout << "读取成功!" << endl;
}
int main() {
loadEngineers(); // 从文件中读取工程师信息
while (true) {
int choice;
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;
cin >> choice;
switch (choice) {
case 1:
addEngineer();
break;
case 2:
modifyEngineer();
break;
case 3:
deleteEngineer();
break;
case 4:
sortEngineers();
break;
case 5:
searchEngineer();
break;
case 6:
displayEngineers();
break;
case 7:
saveEngineers();
break;
case 8:
saveEngineers(); // 退出前保存工程师信息到文件
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "无效的选择!" << endl;
break;
}
}
return 0;
}
```