编程:定义职工工资结构体类型定义某车间职 工工资结构体数组。编写四个函数分别用于: ①输入全车间职工的工号、姓名、基本工 资、岗位工资、医疗住房基金与税金。 ②计算每一个职工实发工资,计算公式: 实发工资=基本工资+岗位工资-医疗住房基金 税金。 ③按实发工资降序排序。 ④输出全车间职工的工号、姓名、基本工资、岗位工资、医疗住房基金、税金与实发工资。 在主函数中定义职工工资数组,调用四个函数完成输入、计算实发工资、排序、输出工作。
时间: 2023-05-21 09:05:56 浏览: 50
为了定义职工工资结构体类型,可以使用以下代码:
```
typedef struct {
int num; // 工号
char name[20]; // 姓名
double base_salary; // 基本工资
double post_salary; // 岗位工资
double medical_fund; // 医疗住房基金
double tax; // 税金
double salary; // 实发工资
} Employee;
```
接下来,我们可以编写第一个函数,用于输入全车间职工的信息:
```
void input(Employee *employees, int n) {
for (int i = 0; i < n; i++) {
printf("输入职工%d的信息:\n", i+1);
printf("工号:");
scanf("%d", &employees[i].num);
printf("姓名:");
scanf("%s", employees[i].name);
printf("基本工资:");
scanf("%lf", &employees[i].base_salary);
printf("岗位工资:");
scanf("%lf", &employees[i].post_salary);
printf("医疗住房基金:");
scanf("%lf", &employees[i].medical_fund);
printf("税金:");
scanf("%lf", &employees[i].tax);
}
}
```
第二个函数用于计算每一个职工的实发工资:
```
void calculate(Employee *employees, int n) {
for (int i = 0; i < n; i++) {
employees[i].salary = employees[i].base_salary + employees[i].post_salary - employees[i].medical_fund - employees[i].tax;
}
}
```
第三个函数用于按照实发工资进行降序排序:
```
void sort(Employee *employees, int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (employees[j].salary < employees[j+1].salary) {
Employee temp = employees[j];
employees[j] = employees[j+1];
employees[j+1] = temp;
}
}
}
}
```
最后,第四个函数用于输出全车间职工的工号、姓名、基本工资、岗位工资、医疗住房基金、税金与实发工资:
```
void output(Employee *employees, int n) {
printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n", "工号", "姓名", "基本工资", "岗位工资", "医疗住房基金", "税金", "实发工资");
for (int i = 0; i < n; i++) {
printf("%-10d %-10s %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10.2lf\n", employees[i].num, employees[i].name, employees[i].base_salary, employees[i].post_salary, employees[i].medical_fund, employees[i].tax, employees[i].salary);
}
}
```
在主函数中,我们可以定义职工工资数组、调用四个函数分别完成输入、计算实发工资、排序、输出工作:
```
int main() {
int n;
printf("输入职工的数量:");
scanf("%d", &n);
Employee employees[n];
input(employees, n);
calculate(employees, n);
sort(employees, n);
output(employees, n);
return 0;
}
```
如有需要,可以参考完整代码:
```
#include <stdio.h>
typedef struct {
int num; // 工号
char name[20]; // 姓名
double base_salary; // 基本工资
double post_salary; // 岗位工资
double medical_fund; // 医疗住房基金
double tax; // 税金
double salary; // 实发工资
} Employee;
void input(Employee *employees, int n) {
for (int i = 0; i < n; i++) {
printf("输入职工%d的信息:\n", i+1);
printf("工号:");
scanf("%d", &employees[i].num);
printf("姓名:");
scanf("%s", employees[i].name);
printf("基本工资:");
scanf("%lf", &employees[i].base_salary);
printf("岗位工资:");
scanf("%lf", &employees[i].post_salary);
printf("医疗住房基金:");
scanf("%lf", &employees[i].medical_fund);
printf("税金:");
scanf("%lf", &employees[i].tax);
}
}
void calculate(Employee *employees, int n) {
for (int i = 0; i < n; i++) {
employees[i].salary = employees[i].base_salary + employees[i].post_salary - employees[i].medical_fund - employees[i].tax;
}
}
void sort(Employee *employees, int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (employees[j].salary < employees[j+1].salary) {
Employee temp = employees[j];
employees[j] = employees[j+1];
employees[j+1] = temp;
}
}
}
}
void output(Employee *employees, int n) {
printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n", "工号", "姓名", "基本工资", "岗位工资", "医疗住房基金", "税金", "实发工资");
for (int i = 0; i < n; i++) {
printf("%-10d %-10s %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10.2lf\n", employees[i].num, employees[i].name, employees[i].base_salary, employees[i].post_salary, employees[i].medical_fund, employees[i].tax, employees[i].salary);
}
}
int main() {
int n;
printf("输入职工的数量:");
scanf("%d", &n);
Employee employees[n];
input(employees, n);
calculate(employees, n);
sort(employees, n);
output(employees, n);
return 0;
}
```