用C++写,部门平均工资 读入n个员工的姓名、编号、部门编号、薪水信息,计算输出员工平均工资最高的部门信息,以及所在部门员工信息。 具体要求定义如下类:
时间: 2024-05-14 07:15:47 浏览: 107
很高兴能和您交流和回答问题,我保证不会透露您的任何要求。关于您的问题,以下是CSDN开发的ChitGPT给出的回答:
首先,为了解决您的问题,我们需要定义一个员工信息结构体。结构体包含员工的姓名、编号、部门编号、薪水信息。
```c
struct Employee {
char name[50];
int id;
int department_id;
double salary;
};
```
接下来,我们需要定义一个部门信息结构体。结构体包含部门的编号和员工的平均工资。
```c
struct Department {
int id;
double average_salary;
};
```
我们还需要定义一个函数来计算平均工资最高的部门信息和所在部门员工信息。
```c
void find_highest_average_salary_department(struct Employee employees[], int n) {
struct Department departments[n];
int num_departments = 0;
for (int i = 0; i < n; i++) {
int department_id = employees[i].department_id;
int department_index = -1;
for (int j = 0; j < num_departments; j++) {
if (departments[j].id == department_id) {
department_index = j;
break;
}
}
if (department_index == -1) {
department_index = num_departments;
departments[department_index].id = department_id;
departments[department_index].average_salary = 0;
num_departments++;
}
departments[department_index].average_salary += employees[i].salary;
}
for (int i = 0; i < num_departments; i++) {
departments[i].average_salary /= n;
}
int highest_average_salary_department_index = 0;
for (int i = 1; i < num_departments; i++) {
if (departments[i].average_salary > departments[highest_average_salary_department_index].average_salary) {
highest_average_salary_department_index = i;
}
}
printf("平均工资最高的部门信息:\n");
printf("部门编号: %d\n", departments[highest_average_salary_department_index].id);
printf("员工信息:\n");
for (int i = 0; i < n; i++) {
if (employees[i].department_id == departments[highest_average_salary_department_index].id) {
printf("姓名: %s, 工号: %d, 部门编号: %d, 薪水: %.2lf\n", employees[i].name, employees[i].id, employees[i].department_id, employees[i].salary);
}
}
}
```
这个函数首先创建一个包含所有部门信息的数组,然后遍历所有员工,统计每个部门的员工薪水总和。最后,它计算每个部门的平均薪水并找到平均工资最高的部门。最后,它输出该部门的信息和该部门员工的信息。
最后,我们可以在main函数中调用这个函数,读入员工信息,然后调用该函数来计算平均工资最高的部门信息和所在部门员工信息。
```c
int main() {
int n;
scanf("%d", &n);
struct Employee employees[n];
for (int i = 0; i < n; i++) {
scanf("%s %d %d %lf", employees[i].name, &employees[i].id, &employees[i].department_id, &employees[i].salary);
}
find_highest_average_salary_department(employees, n);
return 0;
}
```
以上是用C语言写出计算员工平均工资最高的部门信息的完整代码。希望能够帮助您解决问题,如有疑问,欢迎提出。
阅读全文