用 C 语言编制某单位的职工档案管理程序(职工不超过 200 人),每个职工档案的基本数据项包括职工号,姓名,性别,年龄,部门,住址,基本工资,文化程度等,其他项目可根据需要自行设定。 要求在程序中用不同函数完成以下各功能要求:1.从键盘输入这些数据,并按职工号从小到大的顺序保存到 word 文件 zgzl.dat 中 2.可根据职工姓名查找并输出该职工档案 3.可按部门打印出某个部门的职工工资表(包含职工号、姓名、基本工资等项),并查找出全厂中基本工资最高的职工和基本工资最低的职工 4.打印全厂职工的年龄分布情况(包括<35 岁、36~45 岁、46~55 岁、>55 岁各年龄段的人数)
时间: 2023-06-25 07:07:18 浏览: 330
以下是用 C 语言编写的职工档案管理程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEE 200
struct Employee {
char id[10];
char name[20];
char gender[10];
int age;
char department[20];
char address[50];
float salary;
char education[20];
};
struct Employee employees[MAX_EMPLOYEE];
int numEmployees = 0;
void addEmployee() {
if (numEmployees >= MAX_EMPLOYEE) {
printf("The employee list is full.\n");
return;
}
struct Employee employee;
printf("Enter the employee's information:\n");
printf("ID: ");
scanf("%s", employee.id);
printf("Name: ");
scanf("%s", employee.name);
printf("Gender: ");
scanf("%s", employee.gender);
printf("Age: ");
scanf("%d", &employee.age);
printf("Department: ");
scanf("%s", employee.department);
printf("Address: ");
scanf("%s", employee.address);
printf("Salary: ");
scanf("%f", &employee.salary);
printf("Education: ");
scanf("%s", employee.education);
employees[numEmployees++] = employee;
}
void saveEmployees() {
FILE *file = fopen("zgzl.dat", "wb");
if (file == NULL) {
printf("Cannot open file.\n");
return;
}
fwrite(&numEmployees, sizeof(int), 1, file);
fwrite(employees, sizeof(struct Employee), numEmployees, file);
fclose(file);
printf("Employee list saved to zgzl.dat.\n");
}
void loadEmployees() {
FILE *file = fopen("zgzl.dat", "rb");
if (file == NULL) {
printf("Cannot open file.\n");
return;
}
fread(&numEmployees, sizeof(int), 1, file);
fread(employees, sizeof(struct Employee), numEmployees, file);
fclose(file);
}
void searchEmployee() {
char name[20];
printf("Enter the name of the employee you want to search: ");
scanf("%s", name);
for (int i = 0; i < numEmployees; i++) {
if (strcmp(employees[i].name, name) == 0) {
printf("ID: %s\n", employees[i].id);
printf("Name: %s\n", employees[i].name);
printf("Gender: %s\n", employees[i].gender);
printf("Age: %d\n", employees[i].age);
printf("Department: %s\n", employees[i].department);
printf("Address: %s\n", employees[i].address);
printf("Salary: %.2f\n", employees[i].salary);
printf("Education: %s\n", employees[i].education);
return;
}
}
printf("Employee not found.\n");
}
void printSalaryTable() {
char department[20];
printf("Enter the department you want to print: ");
scanf("%s", department);
float maxSalary = -1, minSalary = 1e9;
int numEmployeesInDepartment = 0;
printf("ID\tName\tSalary\n");
for (int i = 0; i < numEmployees; i++) {
if (strcmp(employees[i].department, department) == 0) {
printf("%s\t%s\t%.2f\n", employees[i].id, employees[i].name, employees[i].salary);
numEmployeesInDepartment++;
if (employees[i].salary > maxSalary) {
maxSalary = employees[i].salary;
}
if (employees[i].salary < minSalary) {
minSalary = employees[i].salary;
}
}
}
if (numEmployeesInDepartment == 0) {
printf("No employee found in the department.\n");
} else {
printf("Department: %s\n", department);
printf("Number of employees: %d\n", numEmployeesInDepartment);
printf("Highest salary: %.2f\n", maxSalary);
printf("Lowest salary: %.2f\n", minSalary);
}
}
void printAgeDistribution() {
int numUnder35 = 0, num35To45 = 0, num46To55 = 0, numOver55 = 0;
for (int i = 0; i < numEmployees; i++) {
if (employees[i].age < 35) {
numUnder35++;
} else if (employees[i].age >= 35 && employees[i].age <= 45) {
num35To45++;
} else if (employees[i].age >= 46 && employees[i].age <= 55) {
num46To55++;
} else {
numOver55++;
}
}
printf("<35: %d\n", numUnder35);
printf("36-45: %d\n", num35To45);
printf("46-55: %d\n", num46To55);
printf(">55: %d\n", numOver55);
}
int main() {
loadEmployees();
while (1) {
printf("1. Add employee\n");
printf("2. Save employee list\n");
printf("3. Search employee\n");
printf("4. Print salary table\n");
printf("5. Print age distribution\n");
printf("6. Exit\n");
int choice;
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee();
break;
case 2:
saveEmployees();
break;
case 3:
searchEmployee();
break;
case 4:
printSalaryTable();
break;
case 5:
printAgeDistribution();
break;
case 6:
return 0;
default:
printf("Invalid choice.\n");
}
printf("\n");
}
}
```
程序首先定义了一个 `Employee` 结构体,表示职工档案的基本数据项。然后定义了一个 `employees` 数组来保存所有职工档案,以及一个 `numEmployees` 变量来记录当前职工档案的数量。
程序中包含以下函数:
- `addEmployee()`:从键盘输入职工信息,并将其保存到 `employees` 数组中。
- `saveEmployees()`:将 `employees` 数组中的职工信息保存到文件 `zgzl.dat` 中。
- `loadEmployees()`:从文件 `zgzl.dat` 中加载职工信息到 `employees` 数组中。
- `searchEmployee()`:根据职工姓名查找并输出该职工档案。
- `printSalaryTable()`:根据部门打印出该部门的职工工资表,并查找出全厂中基本工资最高的职工和基本工资最低的职工。
- `printAgeDistribution()`:打印全厂职工的年龄分布情况。
主函数中使用一个无限循环来接受用户的输入,并根据用户的选择调用相应的函数来执行相应的操作。程序在每次操作后会输出一个空行,以及下一次操作的提示。
阅读全文