C语言写出以下系统的代码:系统要求能实现以下基本功能:录入员工信息、更新员工信息、查询员工信 息,排烈员工信息,统计员工信息和输出员工信息等 1. 录入员工信息:从键盘录入表示员工信息的结构体数组各项数据,并将结构 体数组的内容写入表示员工信息的文件中。 2. 更新员工信息:可以实现多次向数据文件追加员工记录的功能,添加完毕后 返回主菜单;能够实现根据员工工号删除文件中员工信息的操作,且可多次删除 记录:可以实现根据员工工号修改员工信息的功能,其中员工信息包括:工号、 姓名、年龄、工作、性别、地址、电话、入职时间,并将最终数据存储到文件中。 3. 查询员工信息:可分别根据姓名、年龄、工作对员工信息进行查询。 4. 排列员工信息:可根据年龄从小到大的顺序对员工信息进行排序。 5. 统计员工信息:能够根据员工的年龄、工作和性别统计显示满足要求的所有 员工信息。 6. 输出员工信息:将员工信息文件中的数据输出到显示器上。 7.实现输入数字0则退出管理系统。
时间: 2024-03-18 20:40:40 浏览: 185
下面是 C 语言实现员工信息管理系统的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEES 100
typedef struct {
char id[20];
char name[20];
int age;
char job[20];
char gender[10];
char address[50];
char phone[20];
char hire_date[20];
} Employee;
Employee employees[MAX_EMPLOYEES];
int num_employees = 0;
void add_employee() {
Employee e;
printf("Enter employee ID: ");
scanf("%s", e.id);
printf("Enter employee name: ");
scanf("%s", e.name);
printf("Enter employee age: ");
scanf("%d", &e.age);
printf("Enter employee job: ");
scanf("%s", e.job);
printf("Enter employee gender: ");
scanf("%s", e.gender);
printf("Enter employee address: ");
scanf("%s", e.address);
printf("Enter employee phone: ");
scanf("%s", e.phone);
printf("Enter employee hire date: ");
scanf("%s", e.hire_date);
employees[num_employees++] = e;
FILE *fp = fopen("employees.dat", "ab");
fwrite(&e, sizeof(Employee), 1, fp);
fclose(fp);
}
void update_employee() {
char id[20];
printf("Enter employee ID: ");
scanf("%s", id);
FILE *fp = fopen("employees.dat", "rb+");
Employee e;
while (fread(&e, sizeof(Employee), 1, fp) == 1) {
if (strcmp(e.id, id) == 0) {
printf("Enter employee name: ");
scanf("%s", e.name);
printf("Enter employee age: ");
scanf("%d", &e.age);
printf("Enter employee job: ");
scanf("%s", e.job);
printf("Enter employee gender: ");
scanf("%s", e.gender);
printf("Enter employee address: ");
scanf("%s", e.address);
printf("Enter employee phone: ");
scanf("%s", e.phone);
printf("Enter employee hire date: ");
scanf("%s", e.hire_date);
fseek(fp, -sizeof(Employee), SEEK_CUR);
fwrite(&e, sizeof(Employee), 1, fp);
break;
}
}
fclose(fp);
}
void delete_employee() {
char id[20];
printf("Enter employee ID: ");
scanf("%s", id);
FILE *fp = fopen("employees.dat", "rb+");
Employee e;
while (fread(&e, sizeof(Employee), 1, fp) == 1) {
if (strcmp(e.id, id) == 0) {
fseek(fp, -sizeof(Employee), SEEK_CUR);
Employee empty = {"", "", 0, "", "", "", "", ""};
fwrite(&empty, sizeof(Employee), 1, fp);
break;
}
}
fclose(fp);
}
void sort_employees() {
int i, j;
for (i = 0; i < num_employees - 1; i++) {
for (j = 0; j < num_employees - i - 1; j++) {
if (employees[j].age > employees[j+1].age) {
Employee temp = employees[j];
employees[j] = employees[j+1];
employees[j+1] = temp;
}
}
}
}
void search_employees() {
int option;
printf("Search employees by:\n");
printf("1. Name\n");
printf("2. Age\n");
printf("3. Job\n");
printf("Enter option: ");
scanf("%d", &option);
switch (option) {
case 1: {
char name[20];
printf("Enter employee name: ");
scanf("%s", name);
int i;
for (i = 0; i < num_employees; i++) {
if (strcmp(employees[i].name, name) == 0) {
printf("%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\n",
employees[i].id, employees[i].name, employees[i].age,
employees[i].job, employees[i].gender, employees[i].address,
employees[i].phone, employees[i].hire_date);
}
}
break;
}
case 2: {
int age;
printf("Enter employee age: ");
scanf("%d", &age);
int i;
for (i = 0; i < num_employees; i++) {
if (employees[i].age == age) {
printf("%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\n",
employees[i].id, employees[i].name, employees[i].age,
employees[i].job, employees[i].gender, employees[i].address,
employees[i].phone, employees[i].hire_date);
}
}
break;
}
case 3: {
char job[20];
printf("Enter employee job: ");
scanf("%s", job);
int i;
for (i = 0; i < num_employees; i++) {
if (strcmp(employees[i].job, job) == 0) {
printf("%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\n",
employees[i].id, employees[i].name, employees[i].age,
employees[i].job, employees[i].gender, employees[i].address,
employees[i].phone, employees[i].hire_date);
}
}
break;
}
default:
break;
}
}
void count_employees() {
char age[10], job[20], gender[10];
printf("Enter age: ");
scanf("%s", age);
printf("Enter job: ");
scanf("%s", job);
printf("Enter gender: ");
scanf("%s", gender);
int i, count = 0;
for (i = 0; i < num_employees; i++) {
if (strcmp(employees[i].age, age) == 0 &&
strcmp(employees[i].job, job) == 0 &&
strcmp(employees[i].gender, gender) == 0) {
count++;
}
}
printf("Number of employees: %d\n", count);
}
void display_employees() {
FILE *fp = fopen("employees.dat", "rb");
Employee e;
printf("ID\tName\tAge\tJob\tGender\tAddress\tPhone\tHire Date\n");
while (fread(&e, sizeof(Employee), 1, fp) == 1) {
if (strlen(e.id) > 0) {
printf("%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\n",
e.id, e.name, e.age, e.job, e.gender, e.address, e.phone, e.hire_date);
}
}
fclose(fp);
}
void menu() {
printf("Employee Management System\n");
printf("1. Add employee\n");
printf("2. Update employee\n");
printf("3. Delete employee\n");
printf("4. Search employee\n");
printf("5. Sort employees\n");
printf("6. Count employees\n");
printf("7. Display employees\n");
printf("0. Exit\n");
}
int main() {
int option;
FILE *fp = fopen("employees.dat", "rb");
if (fp == NULL) {
fp = fopen("employees.dat", "wb");
fclose(fp);
} else {
Employee e;
while (fread(&e, sizeof(Employee), 1, fp) == 1) {
if (strlen(e.id) > 0) {
employees[num_employees++] = e;
}
}
fclose(fp);
}
do {
menu();
printf("Enter option: ");
scanf("%d", &option);
switch (option) {
case 1:
add_employee();
break;
case 2:
update_employee();
break;
case 3:
delete_employee();
break;
case 4:
search_employees();
break;
case 5:
sort_employees();
break;
case 6:
count_employees();
break;
case 7:
display_employees();
break;
default:
break;
}
} while (option != 0);
return 0;
}
```
阅读全文