编程:定义职工工资结构体类型定义某车间职工工资结构体数组。编写四个函数分别用于: ①输入全车间职工的工号、姓名、基本工资、岗位工资、医疗住房基金与税金。 ②计算每一个职工实发工资,计算公式:实发工资=基本工资+岗位工资-医疗住房基金-税金。 ③按实发工资降序排序。 ④输出全车间职工的工号、姓名、基本工资、岗位工资、医疗住房基金、税金与实发工资。 在主函数中定义职工工资数组,调用四个函数完成输入、计算实发工资、排序、输出工作。
时间: 2023-05-27 19:05:48 浏览: 28
定义结构体类型变量的方法-谭浩强 c++ 教材 PPT
#include <stdio.h>
#include <string.h>
#define MAX_EMPLOYEES 100
// 定义职工工资结构体
typedef struct {
int id; // 工号
char name[20]; // 姓名
float base_salary; // 基本工资
float position_salary; // 岗位工资
float fund; // 医疗住房基金
float tax; // 税金
float real_salary; // 实发工资
} Employee;
// 输入职工信息
void input(Employee employees[], int size) {
printf("请输入职工信息:\n");
for (int i = 0; i < size; i++) {
printf("请输入第%d个职工的工号:", i + 1);
scanf("%d", &employees[i].id);
printf("请输入第%d个职工的姓名:", i + 1);
scanf("%s", employees[i].name);
printf("请输入第%d个职工的基本工资:", i + 1);
scanf("%f", &employees[i].base_salary);
printf("请输入第%d个职工的岗位工资:", i + 1);
scanf("%f", &employees[i].position_salary);
printf("请输入第%d个职工的医疗住房基金:", i + 1);
scanf("%f", &employees[i].fund);
printf("请输入第%d个职工的税金:", i + 1);
scanf("%f", &employees[i].tax);
}
}
// 计算实发工资
void calculate(Employee employees[], int size) {
for (int i = 0; i < size; i++) {
employees[i].real_salary = employees[i].base_salary + employees[i].position_salary - employees[i].fund - employees[i].tax;
}
}
// 按实发工资降序排序
void sort(Employee employees[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (employees[j].real_salary < employees[j + 1].real_salary) {
Employee temp = employees[j];
employees[j] = employees[j + 1];
employees[j + 1] = temp;
}
}
}
}
// 输出职工信息
void output(Employee employees[], int size) {
printf("工号\t姓名\t基本工资\t岗位工资\t医疗住房基金\t税金\t实发工资\n");
for (int i = 0; i < size; i++) {
printf("%d\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\t%.2f\n", employees[i].id, employees[i].name, employees[i].base_salary, employees[i].position_salary, employees[i].fund, employees[i].tax, employees[i].real_salary);
}
}
int main() {
Employee employees[MAX_EMPLOYEES];
int n;
printf("请输入职工人数:");
scanf("%d", &n);
input(employees, n);
calculate(employees, n);
sort(employees, n);
output(employees, n);
return 0;
}
阅读全文