可以简化成六十或者七十多行左右吗
时间: 2023-09-18 21:06:40 浏览: 44
可以,以下是简化后的代码:
```c++
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
struct Person {
int age;
char function[12];
char gender[10];
};
struct Staff {
int number;
char name[12];
Person per;
};
Staff staffs[1000];
int staffCount = 0;
void addStaff() {
Staff s;
cout << "请输入员工编号、姓名、年龄、职位和性别:" << endl;
cin >> s.number >> s.name >> s.per.age >> s.per.function >> s.per.gender;
staffs[staffCount++] = s;
cout << "添加成功!" << endl;
}
void delStaff() {
int number;
cout << "请输入要删除的员工编号:" << endl;
cin >> number;
for (int i = 0; i < staffCount; i++) {
if (staffs[i].number == number) {
staffs[i] = staffs[--staffCount];
cout << "删除成功!" << endl;
return;
}
}
cout << "没有找到对应的员工信息" << endl;
}
void modifyStaff() {
int number;
cout << "请输入要修改的员工编号:" << endl;
cin >> number;
for (int i = 0; i < staffCount; i++) {
if (staffs[i].number == number) {
cout << "请输入要修改的员工姓名、年龄、职位和性别:" << endl;
cin >> staffs[i].name >> staffs[i].per.age >> staffs[i].per.function >> staffs[i].per.gender;
cout << "修改成功!" << endl;
return;
}
}
cout << "没有找到对应的员工信息" << endl;
}
void searchStaff() {
int number;
cout << "请输入要查询的员工编号:" << endl;
cin >> number;
for (int i = 0; i < staffCount; i++) {
if (staffs[i].number == number) {
cout << "员工编号:" << staffs[i].number << endl;
cout << "员工姓名:" << staffs[i].name << endl;
cout << "员工年龄:" << staffs[i].per.age << endl;
cout << "员工职位:" << staffs[i].per.function << endl;
cout << "员工性别:" << staffs[i].per.gender << endl;
return;
}
}
cout << "没有找到对应的员工信息" << endl;
}
void showStaff() {
if (staffCount == 0) {
cout << "员工信息为空" << endl;
return;
}
for (int i = 0; i < staffCount; i++) {
cout << "员工编号:" << staffs[i].number << endl;
cout << "员工姓名:" << staffs[i].name << endl;
cout << "员工年龄:" << staffs[i].per.age << endl;
cout << "员工职位:" << staffs[i].per.function << endl;
cout << "员工性别:" << staffs[i].per.gender << endl;
cout << endl;
}
}
int main() {
int choice;
while (true) {
cout << "员工信息管理系统" << endl;
cout << "1. 添加员工信息" << endl;
cout << "2. 删除员工信息" << endl;
cout << "3. 修改员工信息" << endl;
cout << "4. 查询员工信息" << endl;
cout << "5. 显示所有员工信息" << endl;
cout << "6. 退出系统" << endl;
cout << "请输入选项:" << endl;
cin >> choice;
switch (choice) {
case 1:
addStaff();
break;
case 2:
delStaff();
break;
case 3:
modifyStaff();
break;
case 4:
searchStaff();
break;
case 5:
showStaff();
break;
case 6:
return 0;
default:
cout << "输入选项无效,请重新输入" << endl;
break;
}
}
}
```
简化后的代码去除了一些不必要的部分,例如显示菜单时的清屏、输出菜单选项时的格式控制等,同时也对代码进行了一些优化和简化,例如函数返回值改为void、删除员工信息时使用数组末尾元素覆盖而非移动元素等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)