|
马上注册,发布并获取更多一手信息。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
用 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[i].id, &employees[i].salary);
- }
- }
- float averageSalary(Employee employees[], int n) {
- float sum = 0;
- for (int i = 0; i < n; i++) {
- sum += employees[i].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[n/2].salary : (employees[n/2 - 1].salary + employees[n/2].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[i].salary > average && employees[i].salary < median) {
- printf("员工编号: %d, 薪水: %.2f\n", employees[i].id, employees[i].salary);
- }
- }
- }
- int main() {
- int n;
- printf("请输入员工数量: ");
- scanf("%d", &n);
- Employee employees[n];
- inputEmployees(employees, n);
- printEmployeesBetweenAverageAndMedian(employees, n);
- return 0;
- }
复制代码 这个程序首先定义了一个员工结构体,包含员工编号和薪水。然后,它实现了几个函数:输入n个员工的信息、计算平均薪水、计算中位数薪水,以及打印薪水在平均值和中位数之间的员工信息。程序的主函数首先获取员工数量,然后调用这些函数完成任务。
|
|