C++编程:定义职工工资结构体类型定义某车间职 工工资结构体数组。编写四个函数分别用于: ①输入全车间职工的工号、姓名、基本工 资、岗位工资、医疗住房基金与税金。 ②计算每一个职工实发工资,计算公式: 实发工资=基本工资+岗位工资-医疗住房基金 税金。 ③按实发工资降序排序。 ④输出全车间职工的工号、姓名、基本工资、岗位工资、医疗住房基金、税金与实发工资。 在主函数中定义职工工资数组,调用四个函数完成输入、计算实发工资、排序、输出工作。
时间: 2023-05-21 13:06:26 浏览: 195
C++职工工资管理
以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORKERS 100
// 定义职工工资结构体类型
typedef struct {
char id[20]; // 工号
char name[20]; // 姓名
double base_salary; // 基本工资
double position_salary; // 岗位工资
double welfare_fund; // 医疗住房基金
double tax; // 税金
double real_salary; // 实发工资
} Worker;
Worker workers[MAX_WORKERS]; // 职工工资结构体数组
int worker_count = 0; // 职工数量
// 输入职工信息
void input_workers_info()
{
printf("请输入职工信息:(输入end结束)\n");
while (worker_count < MAX_WORKERS) {
printf("职工 %d:\n", worker_count + 1);
Worker* worker = &workers[worker_count];
printf("工号:");
scanf("%s", worker->id);
if (strcmp(worker->id, "end") == 0) {
break;
}
printf("姓名:");
scanf("%s", worker->name);
printf("基本工资:");
scanf("%lf", &worker->base_salary);
printf("岗位工资:");
scanf("%lf", &worker->position_salary);
printf("医疗住房基金:");
scanf("%lf", &worker->welfare_fund);
printf("税金:");
scanf("%lf", &worker->tax);
worker_count++;
}
}
// 计算实发工资
void calculate_real_salary()
{
for (int i = 0; i < worker_count; i++) {
Worker* worker = &workers[i];
worker->real_salary = worker->base_salary + worker->position_salary - worker->welfare_fund - worker->tax;
}
}
// 实发工资降序排序
void sort_by_real_salary_desc()
{
for (int i = 0; i < worker_count; i++) {
for (int j = i + 1; j < worker_count; j++) {
if (workers[i].real_salary < workers[j].real_salary) {
Worker temp = workers[i];
workers[i] = workers[j];
workers[j] = temp;
}
}
}
}
// 输出职工信息
void output_workers_info()
{
printf("全车间职工的工资:\n");
printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n", "工号", "姓名", "基本工资", "岗位工资", "医疗住房基金", "税金", "实发工资");
for (int i = 0; i < worker_count; i++) {
printf("%-10s%-10s%-10.2lf%-10.2lf%-10.2lf%-10.2lf%-10.2lf\n", workers[i].id, workers[i].name, workers[i].base_salary, workers[i].position_salary, workers[i].welfare_fund, workers[i].tax, workers[i].real_salary);
}
}
int main()
{
input_workers_info();
calculate_real_salary();
sort_by_real_salary_desc();
output_workers_info();
return 0;
}
```
阅读全文