用C语言完成一个员工薪水计算程序
用 C语言完成:①设计一个员工的结构体,包含编号和薪水。②设计一个函数输入n个员工的信息。③设计一个函数,找到n个员工薪水的平均值和中位数,并打印出薪水介于平均值和中位数之间的员工信息。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
float salary;
} Employee;
void inputEmployees(Employee employees[], int n) {
for (int i = 0; i < n; i++) {
printf("请输入员工编号和薪水 (如:1 5000): ");
scanf("%d %f", &employees.id, &employees.salary);
}
}
float averageSalary(Employee employees[], int n) {
float sum = 0;
for (int i = 0; i < n; i++) {
sum += employees.salary;
}
return sum / n;
}
int compare(const void *a, const void *b) {
float diff = ((Employee *)a)->salary - ((Employee *)b)->salary;
return (diff > 0) - (diff < 0);
}
float medianSalary(Employee employees[], int n) {
qsort(employees, n, sizeof(Employee), compare);
return (n % 2 != 0) ? employees.salary : (employees.salary + employees.salary) / 2;
}
void printEmployeesBetweenAverageAndMedian(Employee employees[], int n) {
float average = averageSalary(employees, n);
float median = medianSalary(employees, n);
printf("平均薪水: %.2f\n", average);
printf("中位数薪水: %.2f\n", median);
printf("薪水介于平均值和中位数之间的员工信息:\n");
for (int i = 0; i < n; i++) {
if (employees.salary > average && employees.salary < median) {
printf("员工编号: %d, 薪水: %.2f\n", employees.id, employees.salary);
}
}
}
int main() {
int n;
printf("请输入员工数量: ");
scanf("%d", &n);
Employee employees;
inputEmployees(employees, n);
printEmployeesBetweenAverageAndMedian(employees, n);
return 0;
}
这个程序首先定义了一个员工结构体,包含员工编号和薪水。然后,它实现了几个函数:输入n个员工的信息、计算平均薪水、计算中位数薪水,以及打印薪水在平均值和中位数之间的员工信息。程序的主函数首先获取员工数量,然后调用这些函数完成任务。
页:
[1]