用C语言编写工资管理系统软件,职工数据包括:职工号,姓名,基本补贴金额,奖励金额,扣除金额、实发工资等),注: 其中实发工资=基本工资+补贴金额 + 奖励金额-扣除金额,要求职工数据保存到一个文件中。完成以下功能: (1)输入职工数据 (2)能够查询某位职工的相关数据 (3)能够对职工的各项信息进行修改 (4)能够统计出所有职工的平均工资 (5) 退出程序。
时间: 2024-01-22 20:18:13 浏览: 33
以下是用C语言编写的工资管理系统软件,包括职工数据的输入、查询、修改、统计和退出功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee {
int id; // 职工号
char name[20]; // 姓名
float basic_salary; // 基本工资
float subsidy; // 补贴金额
float bonus; // 奖励金额
float deduction; // 扣除金额
float actual_salary; // 实发工资
};
// 声明函数
void input_employee_data(struct Employee *employee);
void output_employee_data(struct Employee employee);
void modify_employee_data(struct Employee *employee);
void calculate_average_salary(struct Employee *employees, int size);
int find_employee_by_id(struct Employee *employees, int size, int id);
int main() {
struct Employee employees[100]; // 最多100个职工
int employee_count = 0;
int choice;
// 从文件中读取职工数据
FILE *file = fopen("employees.dat", "rb");
if (file != NULL) {
fread(&employee_count, sizeof(int), 1, file);
fread(employees, sizeof(struct Employee), employee_count, file);
fclose(file);
}
while (1) {
printf("\n");
printf("1. 输入职工数据\n");
printf("2. 查询职工数据\n");
printf("3. 修改职工数据\n");
printf("4. 统计平均工资\n");
printf("5. 退出程序\n");
printf("请选择操作: ");
scanf("%d", &choice);
printf("\n");
switch (choice) {
case 1:
input_employee_data(&employees[employee_count]);
employee_count++;
break;
case 2: {
int id;
printf("请输入职工号: ");
scanf("%d", &id);
int index = find_employee_by_id(employees, employee_count, id);
if (index == -1) {
printf("没有找到职工号为%d的职工\n", id);
} else {
output_employee_data(employees[index]);
}
break;
}
case 3: {
int id;
printf("请输入职工号: ");
scanf("%d", &id);
int index = find_employee_by_id(employees, employee_count, id);
if (index == -1) {
printf("没有找到职工号为%d的职工\n", id);
} else {
modify_employee_data(&employees[index]);
}
break;
}
case 4:
calculate_average_salary(employees, employee_count);
break;
case 5:
// 保存职工数据到文件中
file = fopen("employees.dat", "wb");
if (file != NULL) {
fwrite(&employee_count, sizeof(int), 1, file);
fwrite(employees, sizeof(struct Employee), employee_count, file);
fclose(file);
}
exit(0);
break;
default:
printf("无效的操作,请重新输入\n");
break;
}
}
return 0;
}
// 输入职工数据
void input_employee_data(struct Employee *employee) {
printf("请输入职工号: ");
scanf("%d", &employee->id);
printf("请输入姓名: ");
scanf("%s", employee->name);
printf("请输入基本工资: ");
scanf("%f", &employee->basic_salary);
printf("请输入补贴金额: ");
scanf("%f", &employee->subsidy);
printf("请输入奖励金额: ");
scanf("%f", &employee->bonus);
printf("请输入扣除金额: ");
scanf("%f", &employee->deduction);
employee->actual_salary = employee->basic_salary + employee->subsidy + employee->bonus - employee->deduction;
}
// 输出职工数据
void output_employee_data(struct Employee employee) {
printf("职工号: %d\n", employee.id);
printf("姓名: %s\n", employee.name);
printf("基本工资: %.2f\n", employee.basic_salary);
printf("补贴金额: %.2f\n", employee.subsidy);
printf("奖励金额: %.2f\n", employee.bonus);
printf("扣除金额: %.2f\n", employee.deduction);
printf("实发工资: %.2f\n", employee.actual_salary);
}
// 修改职工数据
void modify_employee_data(struct Employee *employee) {
int choice;
printf("1. 修改姓名\n");
printf("2. 修改基本工资\n");
printf("3. 修改补贴金额\n");
printf("4. 修改奖励金额\n");
printf("5. 修改扣除金额\n");
printf("请选择要修改的项目: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入新的姓名: ");
scanf("%s", employee->name);
break;
case 2:
printf("请输入新的基本工资: ");
scanf("%f", &employee->basic_salary);
employee->actual_salary = employee->basic_salary + employee->subsidy + employee->bonus - employee->deduction;
break;
case 3:
printf("请输入新的补贴金额: ");
scanf("%f", &employee->subsidy);
employee->actual_salary = employee->basic_salary + employee->subsidy + employee->bonus - employee->deduction;
break;
case 4:
printf("请输入新的奖励金额: ");
scanf("%f", &employee->bonus);
employee->actual_salary = employee->basic_salary + employee->subsidy + employee->bonus - employee->deduction;
break;
case 5:
printf("请输入新的扣除金额: ");
scanf("%f", &employee->deduction);
employee->actual_salary = employee->basic_salary + employee->subsidy + employee->bonus - employee->deduction;
break;
default:
printf("无效的操作,请重新输入\n");
break;
}
}
// 统计平均工资
void calculate_average_salary(struct Employee *employees, int size) {
float total_salary = 0;
for (int i = 0; i < size; i++) {
total_salary += employees[i].actual_salary;
}
float average_salary = total_salary / size;
printf("所有职工的平均工资是: %.2f\n", average_salary);
}
// 根据职工号查找职工,返回索引,如果没找到则返回-1
int find_employee_by_id(struct Employee *employees, int size, int id) {
for (int i = 0; i < size; i++) {
if (employees[i].id == id) {
return i;
}
}
return -1;
}
```
职工数据会保存到文件`employees.dat`中,每次运行程序时会从文件中读取职工数据,程序退出时会将职工数据保存到文件中。每个职工的数据包括职工号、姓名、基本工资、补贴金额、奖励金额、扣除金额和实发工资。程序提供了输入职工数据、查询职工数据、修改职工数据、统计平均工资和退出程序等功能。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)