:定义职工工资结构体类型定义某车间职工工资结构体数组。编写四个函数分别用于: ①输入全车间职工的工号、姓名、基本工资、岗位工资、医疗住房基金与税金。 ②计算每一个职工实发工资,计算公式:实发工资=
时间: 2023-05-29 10:05:55 浏览: 130
基本工资+岗位工资-医疗住房基金-税金。③按照实发工资从高到低排序。④输出全车间职工的工号、姓名、实发工资。
```c
#include <stdio.h>
#include <string.h>
#define MAX_WORKERS 100
typedef struct {
int id; // 工号
char name[20]; // 姓名
double base_salary; // 基本工资
double post_salary; // 岗位工资
double medical_fund; // 医疗住房基金
double tax; // 税金
double real_salary; // 实发工资
} Worker;
Worker workers[MAX_WORKERS];
int num_workers = 0;
void input_workers()
{
while (1) {
if (num_workers >= MAX_WORKERS) {
printf("超过最大职工数%d,无法继续添加\n", MAX_WORKERS);
break;
}
printf("请输入第%d个职工信息:\n", num_workers + 1);
Worker *w = &workers[num_workers++];
printf("工号:");
scanf("%d", &w->id);
printf("姓名:");
scanf("%s", w->name);
printf("基本工资:");
scanf("%lf", &w->base_salary);
printf("岗位工资:");
scanf("%lf", &w->post_salary);
printf("医疗住房基金:");
scanf("%lf", &w->medical_fund);
printf("税金:");
scanf("%lf", &w->tax);
printf("\n");
if (getchar() == '\n') {
break;
}
}
}
void calculate_salary()
{
for (int i = 0; i < num_workers; i++) {
Worker *w = &workers[i];
w->real_salary = w->base_salary + w->post_salary - w->medical_fund - w->tax;
}
}
int compare_worker(const void *p1, const void *p2)
{
const Worker *w1 = (const Worker *)p1;
const Worker *w2 = (const Worker *)p2;
if (w1->real_salary > w2->real_salary) {
return -1;
} else if (w1->real_salary < w2->real_salary) {
return 1;
}
return 0;
}
void sort_workers()
{
qsort(workers, num_workers, sizeof(Worker), compare_worker);
}
void output_workers()
{
printf("工号\t姓名\t实发工资\n");
for (int i = 0; i < num_workers; i++) {
Worker *w = &workers[i];
printf("%d\t%s\t%.2lf\n", w->id, w->name, w->real_salary);
}
}
int main()
{
input_workers();
calculate_salary();
sort_workers();
output_workers();
return 0;
}
```
阅读全文