定义职工工资结构体类型定义某车间职工工资结构体数组。编写四个函数分别用于: ①输入全车间职工的工号、姓名、基本工资、岗位工资、医疗住房基金与税金。 ②计算每一个职工实发工资,计算公式:实发工资=基本工资+岗位工资-医疗住房基金-税金。 ③按实发工资降序排序。 ④输出全车间职工的工号、姓名、基本工资、岗位工资、医疗住房基金、税金与实发工资。
时间: 2023-05-12 22:06:23 浏览: 83
以下是我为您准备的答案:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORKERS 100
typedef struct {
int id;
char name[20];
float base_salary;
float position_salary;
float medical_fund;
float housing_fund;
float tax;
float real_salary;
} Worker;
Worker workers[MAX_WORKERS];
int num_workers = 0;
void input_workers() {
printf("请输入职工信息:\n");
printf("工号 姓名 基本工资 岗位工资 医疗基金 住房基金 税金\n");
while (1) {
Worker worker;
scanf("%d", &worker.id);
if (worker.id == -1) {
break;
}
scanf("%s%f%f%f%f%f", worker.name, &worker.base_salary, &worker.position_salary, &worker.medical_fund, &worker.housing_fund, &worker.tax);
workers[num_workers++] = worker;
}
}
void calculate_real_salary() {
for (int i = 0; i < num_workers; i++) {
Worker *worker = &workers[i];
worker->real_salary = worker->base_salary + worker->position_salary - worker->medical_fund - worker->housing_fund - worker->tax;
}
}
int compare_workers(const void *a, const void *b) {
Worker *worker1 = (Worker *)a;
Worker *worker2 = (Worker *)b;
if (worker1->real_salary < worker2->real_salary) {
return 1;
} else if (worker1->real_salary > worker2->real_salary) {
return -1;
} else {
return 0;
}
}
void sort_workers() {
qsort(workers, num_workers, sizeof(Worker), compare_workers);
}
void output_workers() {
printf("工号\t姓名\t基本工资\t岗位工资\t医疗基金\t住房基金\t税金\t实发工资\n");
for (int i = 0; i < num_workers; i++) {
Worker *worker = &workers[i];
printf("%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", worker->id, worker->name, worker->base_salary, worker->position_salary, worker->medical_fund, worker->housing_fund, worker->tax, worker->real_salary);
}
}
int main() {
input_workers();
calculate_real_salary();
sort_workers();
output_workers();
return 0;
}
```
希望这个程序能够满足您的需求!
阅读全文