4.编制职工档案管理程序 用C语言编制某单位的职工档案管理程序(职工不超过200人),每个职工档案的基本数据项包括职工号、姓名、性别、年龄、部门、住址、基本工资、文化程度等,其他项目可根据需要自行设定。 要求在程序中用不同的函数完成以下各功能要求: (1)从键盘输入这些数据,并按职工号从小到大的顺序保存到文件 zgzl.dat 中(格式自定,但要包含以上各项信息)。 (2)可根据职工姓名查找并输出该职工的档案。 (3)可按部门打印出某个部门的职工工资表(包含职工号、姓名、基本工资等项),并查找出全厂中基本工资最高的职工和基本工资最低的职工。 (4)打印全厂职工的年龄分布情况(包括<35岁、36~45岁、46~55岁、>55岁各年龄段的人数)。
时间: 2023-11-27 20:54:07 浏览: 126
学院教职工工资管理系统C语言程序设计.doc
以下是实现以上功能要求的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEE 200
#define FILENAME "zgzl.dat"
// 定义职工档案结构体
typedef struct {
int id;
char name[20];
char gender[5];
int age;
char department[20];
char address[50];
float salary;
char education[20];
} Employee;
// 函数声明
int menu();
void input(Employee *employees, int *count);
void save(Employee *employees, int count);
void searchByName(Employee *employees, int count);
void printByDepartment(Employee *employees, int count);
void printSalary(Employee *employees, int count);
void printAgeDistribution(Employee *employees, int count);
int main() {
Employee employees[MAX_EMPLOYEE];
int count = 0;
int choice;
while ((choice = menu()) != 0) {
switch (choice) {
case 1:
input(employees, &count);
save(employees, count);
break;
case 2:
searchByName(employees, count);
break;
case 3:
printByDepartment(employees, count);
break;
case 4:
printSalary(employees, count);
break;
case 5:
printAgeDistribution(employees, count);
break;
default:
printf("无效的选项,请重新输入\n");
break;
}
}
return 0;
}
// 显示菜单并获取用户选择
int menu() {
int choice;
printf("\n");
printf("=== 职工档案管理系统 ===\n");
printf("1. 输入职工档案\n");
printf("2. 按姓名查找职工档案\n");
printf("3. 按部门打印职工工资表\n");
printf("4. 打印全厂职工基本工资最高和最低的职工\n");
printf("5. 打印全厂职工年龄分布情况\n");
printf("0. 退出系统\n");
printf("========================\n");
printf("请选择:");
scanf("%d", &choice);
return choice;
}
// 输入职工档案信息
void input(Employee *employees, int *count) {
int n;
printf("请输入要输入的职工数目(不超过200):");
scanf("%d", &n);
if (*count + n > MAX_EMPLOYEE) {
printf("职工数目超过最大限制,无法输入\n");
return;
}
for (int i = 0; i < n; i++) {
Employee employee;
printf("请输入第%d个职工的信息:\n", i+1);
printf("职工号:");
scanf("%d", &employee.id);
printf("姓名:");
scanf("%s", employee.name);
printf("性别:");
scanf("%s", employee.gender);
printf("年龄:");
scanf("%d", &employee.age);
printf("部门:");
scanf("%s", employee.department);
printf("住址:");
scanf("%s", employee.address);
printf("基本工资:");
scanf("%f", &employee.salary);
printf("文化程度:");
scanf("%s", employee.education);
employees[*count + i] = employee;
}
*count += n;
}
// 将职工档案信息保存到文件中
void save(Employee *employees, int count) {
FILE *fp;
fp = fopen(FILENAME, "wb");
if (fp == NULL) {
printf("无法打开文件 %s\n", FILENAME);
return;
}
fwrite(employees, sizeof(Employee), count, fp);
fclose(fp);
printf("职工档案已保存到文件 %s 中\n", FILENAME);
}
// 根据姓名查找并输出职工档案信息
void searchByName(Employee *employees, int count) {
char name[20];
int found = 0;
printf("请输入要查找的职工姓名:");
scanf("%s", name);
for (int i = 0; i < count; i++) {
if (strcmp(name, employees[i].name) == 0) {
printf("职工号:%d\n", employees[i].id);
printf("姓名:%s\n", employees[i].name);
printf("性别:%s\n", employees[i].gender);
printf("年龄:%d\n", employees[i].age);
printf("部门:%s\n", employees[i].department);
printf("住址:%s\n", employees[i].address);
printf("基本工资:%f\n", employees[i].salary);
printf("文化程度:%s\n", employees[i].education);
found = 1;
break;
}
}
if (!found) {
printf("未找到姓名为 %s 的职工档案\n", name);
}
}
// 按部门打印职工工资表,并查找全厂中基本工资最高和最低的职工
void printByDepartment(Employee *employees, int count) {
char department[20];
float maxSalary = 0, minSalary = 999999;
int maxIndex = -1, minIndex = -1;
printf("请输入要打印的部门名称:");
scanf("%s", department);
printf("职工号\t姓名\t基本工资\n");
for (int i = 0; i < count; i++) {
if (strcmp(department, employees[i].department) == 0) {
printf("%d\t%s\t%f\n", employees[i].id, employees[i].name, employees[i].salary);
if (employees[i].salary > maxSalary) {
maxSalary = employees[i].salary;
maxIndex = i;
}
if (employees[i].salary < minSalary) {
minSalary = employees[i].salary;
minIndex = i;
}
}
}
if (maxIndex != -1) {
printf("全厂基本工资最高的职工为 %s(工号:%d),基本工资为 %f\n", employees[maxIndex].name, employees[maxIndex].id, employees[maxIndex].salary);
}
if (minIndex != -1) {
printf("全厂基本工资最低的职工为 %s(工号:%d),基本工资为 %f\n", employees[minIndex].name, employees[minIndex].id, employees[minIndex].salary);
}
}
// 打印全厂职工基本工资最高和最低的职工
void printSalary(Employee *employees, int count) {
float maxSalary = 0, minSalary = 999999;
int maxIndex = -1, minIndex = -1;
for (int i = 0; i < count; i++) {
if (employees[i].salary > maxSalary) {
maxSalary = employees[i].salary;
maxIndex = i;
}
if (employees[i].salary < minSalary) {
minSalary = employees[i].salary;
minIndex = i;
}
}
if (maxIndex != -1) {
printf("全厂基本工资最高的职工为 %s(工号:%d),基本工资为 %f\n", employees[maxIndex].name, employees[maxIndex].id, employees[maxIndex].salary);
}
if (minIndex != -1) {
printf("全厂基本工资最低的职工为 %s(工号:%d),基本工资为 %f\n", employees[minIndex].name, employees[minIndex].id, employees[minIndex].salary);
}
}
// 打印全厂职工年龄分布情况
void printAgeDistribution(Employee *employees, int count) {
int ageCount[4] = {0}; // 分别存储各年龄段的人数
for (int i = 0; i < count; i++) {
if (employees[i].age < 35) {
ageCount[0]++;
} else if (employees[i].age < 46) {
ageCount[1]++;
} else if (employees[i].age < 56) {
ageCount[2]++;
} else {
ageCount[3]++;
}
}
printf("全厂职工年龄分布情况:\n");
printf("<35岁:%d人\n", ageCount[0]);
printf("36~45岁:%d人\n", ageCount[1]);
printf("46~55岁:%d人\n", ageCount[2]);
printf(">55岁:%d人\n", ageCount[3]);
}
```
注意:以上代码中使用了二进制文件来保存职工档案信息,这样可以方便地读写整个职工档案数组,但是也存在风险,如果数据被损坏或篡改,整个职工档案信息可能会受到影响。因此,在实际应用中,可能需要进行更严格的数据安全措施。
阅读全文