用C语言编制某单位的职工档案管理程序(职工不超过200人),每个职工档案的基本数据项包括职工号、姓名、性别、年龄、部门、住址、基本工资、文化程度等,其他项目可根据需要自行设定。 要求在程序中用不同的函数完成以下各功能要求: (1)从键盘输入这些数据,并按职工号从小到人的顺序保存到文件zgzldat中(格式自 定,但要包含以上各项信息)。 (2)可根据职工姓名查找并输出该职工的档案。 (3)可按部门打印出某个部门的职工工资表(包含职工号、姓名、基本工资等项),并查 找出全厂中基本工资最高的职工和基本工资最低的职工。 (4)打印全厂职工的年龄分布情况(包括<35岁、36~45岁、46~55岁、>55岁各年龄段的人数)。
时间: 2023-07-10 11:11:57 浏览: 119
C语言程序设计:职工信息管理系统
好的,以下是一个简单的实现,包含了上述要求的各个功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义结构体
typedef struct {
int id;
char name[20];
char gender[5];
int age;
char department[20];
char address[50];
int salary;
char education[20];
} Employee;
// 函数声明
void inputEmployees(Employee employees[], int num);
void sortEmployees(Employee employees[], int num);
void writeToFile(Employee employees[], int num);
void searchEmployeeByName(Employee employees[], int num);
void printDepartment(Employee employees[], int num);
void printAgeDistribution(Employee employees[], int num);
int main() {
Employee employees[200];
int num;
printf("请输入职工人数(不超过200人):");
scanf("%d", &num);
inputEmployees(employees, num);
sortEmployees(employees, num);
writeToFile(employees, num);
searchEmployeeByName(employees, num);
printDepartment(employees, num);
printAgeDistribution(employees, num);
return 0;
}
// 录入职工档案信息
void inputEmployees(Employee employees[], int num) {
for (int i = 0; i < num; i++) {
printf("请输入第%d个职工的信息:\n", i + 1);
printf("职工号:");
scanf("%d", &employees[i].id);
printf("姓名:");
scanf("%s", employees[i].name);
printf("性别:");
scanf("%s", employees[i].gender);
printf("年龄:");
scanf("%d", &employees[i].age);
printf("部门:");
scanf("%s", employees[i].department);
printf("住址:");
scanf("%s", employees[i].address);
printf("基本工资:");
scanf("%d", &employees[i].salary);
printf("文化程度:");
scanf("%s", employees[i].education);
}
}
// 按职工号从小到大排序
void sortEmployees(Employee employees[], int num) {
for (int i = 0; i < num - 1; i++) {
for (int j = 0; j < num - 1 - i; j++) {
if (employees[j].id > employees[j+1].id) {
Employee temp = employees[j];
employees[j] = employees[j+1];
employees[j+1] = temp;
}
}
}
}
// 将职工档案写入文件
void writeToFile(Employee employees[], int num) {
FILE *fp;
fp = fopen("zgzldat", "w");
if (fp == NULL) {
printf("打开文件失败!\n");
exit(1);
}
for (int i = 0; i < num; i++) {
fprintf(fp, "%d %s %s %d %s %s %d %s\n", employees[i].id, employees[i].name, employees[i].gender, employees[i].age, employees[i].department, employees[i].address, employees[i].salary, employees[i].education);
}
fclose(fp);
}
// 根据职工姓名查找并输出职工档案信息
void searchEmployeeByName(Employee employees[], int num) {
char name[20];
printf("请输入要查找的职工姓名:");
scanf("%s", name);
for (int i = 0; i < num; i++) {
if (strcmp(employees[i].name, name) == 0) {
printf("职工号:%d 姓名:%s 性别:%s 年龄:%d 部门:%s 住址:%s 基本工资:%d 文化程度:%s\n", employees[i].id, employees[i].name, employees[i].gender, employees[i].age, employees[i].department, employees[i].address, employees[i].salary, employees[i].education);
return;
}
}
printf("未找到该职工!\n");
}
// 按部门打印职工工资表,并查找基本工资最高和最低的职工
void printDepartment(Employee employees[], int num) {
char department[20];
int maxSalary = -1, minSalary = 100000; // 初始化最高工资和最低工资
Employee maxEmployee, minEmployee;
printf("请输入要打印工资表的部门名称:");
scanf("%s", department);
printf("职工号\t姓名\t基本工资\n");
for (int i = 0; i < num; i++) {
if (strcmp(employees[i].department, department) == 0) {
printf("%d\t%s\t%d\n", employees[i].id, employees[i].name, employees[i].salary);
if (employees[i].salary > maxSalary) {
maxSalary = employees[i].salary;
maxEmployee = employees[i];
}
if (employees[i].salary < minSalary) {
minSalary = employees[i].salary;
minEmployee = employees[i];
}
}
}
printf("基本工资最高的职工是:%s,工资为%d元\n", maxEmployee.name, maxEmployee.salary);
printf("基本工资最低的职工是:%s,工资为%d元\n", minEmployee.name, minEmployee.salary);
}
// 打印全厂职工的年龄分布情况
void printAgeDistribution(Employee employees[], int num) {
int age1 = 0, age2 = 0, age3 = 0, age4 = 0;
for (int i = 0; i < num; i++) {
if (employees[i].age < 35) {
age1++;
} else if (employees[i].age >= 35 && employees[i].age <= 45) {
age2++;
} else if (employees[i].age >= 46 && employees[i].age <= 55) {
age3++;
} else {
age4++;
}
}
printf("<35岁:%d人,36~45岁:%d人,46~55岁:%d人,>55岁:%d人\n", age1, age2, age3, age4);
}
```
以上代码仅供参考,实际应用中还需要进行完善和优化。
阅读全文