用VS实现c++管理系统,要求有工程师的信息添加、修改、删除、排序、查找、显示、保存,然后退出。工程师要有工号、姓名、性别、学历、年龄、工资、地址、电话。
时间: 2023-12-22 09:04:24 浏览: 60
以下是用VS实现c++管理系统的代码,包括工程师信息的添加、修改、删除、排序、查找、显示、保存和退出功能。其中工程师包括工号、姓名、性别、学历、年龄、工资、地址、电话等信息。
```c++
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<iomanip>
using namespace std;
// 工程师信息结构体
struct Engineer {
string id; // 工号
string name; // 姓名
string gender; // 性别
string education; // 学历
int age; // 年龄
double salary; // 工资
string address; // 地址
string phone; // 电话
};
// 全局变量
Engineer engineers[1000]; // 最多存储1000个工程师信息
int engineerCount = 0; // 当前工程师数量
// 函数声明
void addEngineer(); // 添加工程师信息
void modifyEngineer(); // 修改工程师信息
void deleteEngineer(); // 删除工程师信息
void sortEngineers(); // 排序工程师信息
void searchEngineer(); // 查找工程师信息
void displayEngineers(); // 显示所有工程师信息
void saveEngineers(); // 保存工程师信息到文件
void loadEngineers(); // 从文件中读取工程师信息
void exitSystem(); // 退出系统
int main() {
loadEngineers(); // 从文件中读取工程师信息
while (true) {
cout << "欢迎使用工程师管理系统!" << endl;
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:
modifyEngineer();
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 << "请输入工程师信息:" << endl;
Engineer engineer;
cout << "工号:";
cin >> engineer.id;
cout << "姓名:";
cin >> engineer.name;
cout << "性别:";
cin >> engineer.gender;
cout << "学历:";
cin >> engineer.education;
cout << "年龄:";
cin >> engineer.age;
cout << "工资:";
cin >> engineer.salary;
cout << "地址:";
cin >> engineer.address;
cout << "电话:";
cin >> engineer.phone;
engineers[engineerCount++] = engineer;
cout << "添加成功!" << endl;
}
// 修改工程师信息
void modifyEngineer() {
cout << "请输入要修改的工程师的工号:" << endl;
string id;
cin >> id;
for (int i = 0; i < engineerCount; i++) {
if (engineers[i].id == id) {
cout << "请输入新的工程师信息:" << endl;
cout << "工号:";
cin >> engineers[i].id;
cout << "姓名:";
cin >> engineers[i].name;
cout << "性别:";
cin >> engineers[i].gender;
cout << "学历:";
cin >> engineers[i].education;
cout << "年龄:"; cin >> engineers[i].age;
cout << "工资:";
cin >> engineers[i].salary;
cout << "地址:";
cin >> engineers[i].address;
cout << "电话:";
cin >> engineers[i].phone;
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该工程师!" << endl;
}
// 删除工程师信息
void deleteEngineer() {
cout << "请输入要删除的工程师的工号:" << endl;
string id;
cin >> id;
for (int i = 0; i < engineerCount; i++) {
if (engineers[i].id == id) {
for (int j = i; j < engineerCount - 1; j++) {
engineers[j] = engineers[j + 1];
}
engineerCount--;
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该工程师!" << endl;
}
// 排序工程师信息
void sortEngineers() {
cout << "请选择排序方式:" << endl;
cout << "1. 按工号排序" << endl;
cout << "2. 按姓名排序" << endl;
cout << "3. 按年龄排序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
for (int i = 0; i < engineerCount - 1; i++) {
for (int j = 0; j < engineerCount - i - 1; j++) {
if (engineers[j].id > engineers[j + 1].id) {
Engineer temp = engineers[j];
engineers[j] = engineers[j + 1];
engineers[j + 1] = temp;
}
}
}
cout << "按工号排序成功!" << endl;
break;
case 2:
for (int i = 0; i < engineerCount - 1; i++) {
for (int j = 0; j < engineerCount - i - 1; j++) {
if (engineers[j].name > engineers[j + 1].name) {
Engineer temp = engineers[j];
engineers[j] = engineers[j + 1];
engineers[j + 1] = temp;
}
}
}
cout << "按姓名排序成功!" << endl;
break;
case 3:
for (int i = 0; i < engineerCount - 1; i++) {
for (int j = 0; j < engineerCount - i - 1; j++) {
if (engineers[j].age > engineers[j + 1].age) {
Engineer temp = engineers[j];
engineers[j] = engineers[j + 1];
engineers[j + 1] = temp;
}
}
}
cout << "按年龄排序成功!" << endl;
break;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
// 查找工程师信息
void searchEngineer() {
cout << "请选择查找方式:" << endl;
cout << "1. 按工号查找" << endl;
cout << "2. 按姓名查找" << endl;
cout << "3. 按学历查找" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入要查找的工程师的工号:" << endl;
string id;
cin >> id;
for (int i = 0; i < engineerCount; i++) {
if (engineers[i].id == id) {
cout << "工号\t姓名\t性别\t学历\t年龄\t工资\t地址\t电话" << endl;
cout << engineers[i].id << "\t" << engineers[i].name << "\t" << engineers[i].gender << "\t" << engineers[i].education << "\t" << engineers[i].age << "\t" << engineers[i].salary << "\t" << engineers[i].address << "\t" << engineers[i].phone << endl;
return;
}
}
cout << "未找到该工程师!" << endl;
break;
case 2:
cout << "请输入要查找的工程师的姓名:" << endl;
string name;
cin >> name;
for (int i = 0; i < engineerCount; i++) {
if (engineers[i].name == name) {
cout << "工号\t姓名\t性别\t学历\t年龄\t工资\t地址\t电话" << endl;
cout << engineers[i].id << "\t" << engineers[i].name << "\t" << engineers[i].gender << "\t" << engineers[i].education << "\t" << engineers[i].age << "\t" << engineers[i].salary << "\t" << engineers[i].address << "\t" << engineers[i].phone << endl;
return;
}
}
cout << "未找到该工程师!" << endl;
break;
case 3:
cout << "请输入要查找的工程师的学历:" << endl;
string education;
cin >> education;
for (int i = 0; i < engineerCount; i++) {
if (engineers[i].education == education) {
cout << "工号\t姓名\t性别\t学历\t年龄\t工资\t地址\t电话" << endl;
cout << engineers[i].id << "\t" << engineers[i].name << "\t" << engineers[i].gender << "\t" << engineers[i].education << "\t" << engineers[i].age << "\t" << engineers[i].salary << "\t" << engineers[i].address << "\t" << engineers[i].phone << endl;
}
}
break;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
// 显示所有工程师信息
void displayEngineers() {
cout << "工号\t姓名\t性别\t学历\t年龄\t工资\t地址\t电话" << endl;
for (int i = 0; i < engineerCount; i++) {
cout << engineers[i].id << "\t" << engineers[i].name << "\t" << engineers[i].gender << "\t" << engineers[i].education << "\t" << engineers[i].age << "\t" << engineers[i].salary << "\t" << engineers[i].address << "\t" << engineers[i].phone << endl;
}
}
// 保存工程师信息到文件
void saveEngineers() {
ofstream fout("engineers.txt");
fout << engineerCount << endl;
for (int i = 0; i < engineerCount; 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");
if (!fin) {
cout << "未找到工程师信息文件!" << endl;
return;
}
fin >> engineerCount;
for (int i = 0; i < engineerCount; i++) {
fin >> engineers[i].id >> engineers[i].name >> engineers[i].gender >> engineers[i].education >> engineers[i].age >> engineers[i].salary >> engineers[i].address >> engineers[i].phone;
}
fin.close();
cout << "读取成功!" << endl;
}
// 退出系统
void exitSystem() {
cout << "感谢使用工程师管理系统!" << endl;
exit(0);
}
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)