职工工资管理系统的设计与实现(顺序表实现) (1)职工信息包括:职工号(char num[10])、职工名(char name[10])、职工级别(char type[10])、工资(float money) (2)除了总的模块要求外,还需计算①职工的平均工资; ②按工资由低到高排序
时间: 2024-03-19 08:46:32 浏览: 50
好的,这是一个关于职工工资管理系统的设计与实现的问题,需要使用顺序表来实现。下面是我的回答:
首先,我们需要定义一个结构体来存储职工的信息:
```c++
struct Worker {
char num[10];
char name[10];
char type[10];
float money;
};
```
然后,我们可以使用一个动态顺序表来存储所有职工的信息:
```c++
vector<Worker> workers;
```
接下来,我们需要实现以下几个功能:
1. 添加职工信息
```c++
void addWorker() {
Worker worker;
cout << "请输入职工号:";
cin >> worker.num;
cout << "请输入职工名:";
cin >> worker.name;
cout << "请输入职工级别:";
cin >> worker.type;
cout << "请输入职工工资:";
cin >> worker.money;
workers.push_back(worker);
}
```
2. 删除职工信息
```c++
void deleteWorker() {
string num;
cout << "请输入要删除的职工号:";
cin >> num;
for (int i = 0; i < workers.size(); i++) {
if (strcmp(workers[i].num, num.c_str()) == 0) {
workers.erase(workers.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "没有找到该职工号!" << endl;
}
```
3. 修改职工信息
```c++
void modifyWorker() {
string num;
cout << "请输入要修改的职工号:";
cin >> num;
for (int i = 0; i < workers.size(); i++) {
if (strcmp(workers[i].num, num.c_str()) == 0) {
cout << "请输入职工名:";
cin >> workers[i].name;
cout << "请输入职工级别:";
cin >> workers[i].type;
cout << "请输入职工工资:";
cin >> workers[i].money;
cout << "修改成功!" << endl;
return;
}
}
cout << "没有找到该职工号!" << endl;
}
```
4. 显示所有职工信息
```c++
void showWorkers() {
cout << "职工号\t职工名\t职工级别\t职工工资" << endl;
for (int i = 0; i < workers.size(); i++) {
cout << workers[i].num << "\t" << workers[i].name << "\t" << workers[i].type << "\t" << workers[i].money << endl;
}
}
```
5. 计算职工平均工资
```c++
void calculateAverageMoney() {
float sum = 0;
for (int i = 0; i < workers.size(); i++) {
sum += workers[i].money;
}
float average = sum / workers.size();
cout << "平均工资为:" << average << endl;
}
```
6. 按工资由低到高排序
```c++
bool compare(const Worker& worker1, const Worker& worker2) {
return worker1.money < worker2.money;
}
void sortWorkers() {
sort(workers.begin(), workers.end(), compare);
cout << "排序成功!" << endl;
}
```
最后,我们可以使用一个菜单来调用上述函数:
```c++
int main() {
while (true) {
cout << "---------------------" << endl;
cout << "1. 添加职工信息" << endl;
cout << "2. 删除职工信息" << endl;
cout << "3. 修改职工信息" << endl;
cout << "4. 显示所有职工信息" << endl;
cout << "5. 计算职工平均工资" << endl;
cout << "6. 按工资由低到高排序" << endl;
cout << "0. 退出程序" << endl;
cout << "---------------------" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addWorker();
break;
case 2:
deleteWorker();
break;
case 3:
modifyWorker();
break;
case 4:
showWorkers();
break;
case 5:
calculateAverageMoney();
break;
case 6:
sortWorkers();
break;
case 0:
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
这样,我们就完成了职工工资管理系统的设计与实现。
阅读全文