c语言数据结构使用链表的应用编写一个人事管理系统,要输出相应界面:用链式存储方法实现。 每个职工的信息包括:工号、姓名、性别、出生年月、籍贯、部门,能实现输入: 按工号或者姓名或者性别或者部门查找: 插入: 按工号或者姓名或者性别或者部门删除,按工号或者出生年月进行升序或者降序,按工号进行修改职工的信息 ,分别统计30岁以下,30-50和50岁以上,三个年龄段的人数,这些操作都会有相应的print输出界面的代码
时间: 2024-02-01 07:14:20 浏览: 77
c语言实现链表的基本操作,链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
以下是一个简单的人事管理系统的代码,使用链表存储数据结构,并实现了查找、插入、删除、排序和统计等功能。请注意,这只是一个基础的实现,可能会存在一些漏洞,需要根据具体需求进行修改和完善。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义职工信息结构体
typedef struct employee {
int id; // 工号
char name[20]; // 姓名
char gender[5]; // 性别
char birth[20]; // 出生年月
char hometown[20]; // 籍贯
char department[20];// 部门
struct employee *next; // 指向下一个职工信息的指针
} Employee;
// 全局变量,指向链表头部
Employee *head = NULL;
// 显示菜单
void showMenu() {
printf("*************************\n");
printf("******* 人事管理系统 *******\n");
printf("*************************\n");
printf("1. 显示所有职工信息\n");
printf("2. 按工号查找\n");
printf("3. 按姓名查找\n");
printf("4. 按性别查找\n");
printf("5. 按部门查找\n");
printf("6. 插入职工信息\n");
printf("7. 按工号删除\n");
printf("8. 按出生年月排序\n");
printf("9. 按工号排序\n");
printf("10. 修改职工信息\n");
printf("11. 统计年龄\n");
printf("0. 退出系统\n");
printf("*************************\n");
}
// 显示一个职工信息
void showEmployee(Employee *e) {
printf("%d\t%s\t%s\t%s\t%s\t%s\n", e->id, e->name, e->gender, e->birth, e->hometown, e->department);
}
// 显示所有职工信息
void showAllEmployees() {
if (head == NULL) {
printf("当前没有职工信息!\n");
return;
}
printf("工号\t姓名\t性别\t出生年月\t籍贯\t部门\n");
Employee *current = head;
while (current != NULL) {
showEmployee(current);
current = current->next;
}
}
// 按工号查找职工信息
void findEmployeeById(int id) {
if (head == NULL) {
printf("当前没有职工信息!\n");
return;
}
Employee *current = head;
while (current != NULL) {
if (current->id == id) {
showEmployee(current);
return;
}
current = current->next;
}
printf("没有找到工号为%d的职工信息!\n", id);
}
// 按姓名查找职工信息
void findEmployeeByName(char *name) {
if (head == NULL) {
printf("当前没有职工信息!\n");
return;
}
Employee *current = head;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
showEmployee(current);
return;
}
current = current->next;
}
printf("没有找到姓名为%s的职工信息!\n", name);
}
// 按性别查找职工信息
void findEmployeeByGender(char *gender) {
if (head == NULL) {
printf("当前没有职工信息!\n");
return;
}
Employee *current = head;
while (current != NULL) {
if (strcmp(current->gender, gender) == 0) {
showEmployee(current);
}
current = current->next;
}
}
// 按部门查找职工信息
void findEmployeeByDepartment(char *department) {
if (head == NULL) {
printf("当前没有职工信息!\n");
return;
}
Employee *current = head;
while (current != NULL) {
if (strcmp(current->department, department) == 0) {
showEmployee(current);
}
current = current->next;
}
}
// 插入职工信息
void insertEmployee(Employee *e) {
if (head == NULL) {
head = e;
return;
}
Employee *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = e;
}
// 按工号删除职工信息
void deleteEmployeeById(int id) {
if (head == NULL) {
printf("当前没有职工信息!\n");
return;
}
Employee *current = head;
Employee *prev = NULL;
while (current != NULL) {
if (current->id == id) {
if (prev == NULL) {
head = current->next;
} else {
prev->next = current->next;
}
free(current);
printf("删除成功!\n");
return;
}
prev = current;
current = current->next;
}
printf("没有找到工号为%d的职工信息!\n", id);
}
// 按出生年月排序
void sortEmployeeByBirth(int order) {
if (head == NULL) {
printf("当前没有职工信息!\n");
return;
}
int count = 0;
Employee *current = head;
while (current != NULL) {
count++;
current = current->next;
}
Employee **array = (Employee **)malloc(count * sizeof(Employee *));
current = head;
for (int i = 0; i < count; i++) {
array[i] = current;
current = current->next;
}
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
char *birth1 = array[i]->birth;
char *birth2 = array[j]->birth;
if ((order > 0 && strcmp(birth1, birth2) > 0) || (order < 0 && strcmp(birth1, birth2) < 0)) {
Employee *tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
printf("工号\t姓名\t性别\t出生年月\t籍贯\t部门\n");
for (int i = 0; i < count; i++) {
showEmployee(array[i]);
}
free(array);
}
// 按工号排序
void sortEmployeeById(int order) {
if (head == NULL) {
printf("当前没有职工信息!\n");
return;
}
int count = 0;
Employee *current = head;
while (current != NULL) {
count++;
current = current->next;
}
Employee **array = (Employee **)malloc(count * sizeof(Employee *));
current = head;
for (int i = 0; i < count; i++) {
array[i] = current;
current = current->next;
}
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
int id1 = array[i]->id;
int id2 = array[j]->id;
if ((order > 0 && id1 > id2) || (order < 0 && id1 < id2)) {
Employee *tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
printf("工号\t姓名\t性别\t出生年月\t籍贯\t部门\n");
for (int i = 0; i < count; i++) {
showEmployee(array[i]);
}
free(array);
}
// 修改职工信息
void modifyEmployee(int id) {
if (head == NULL) {
printf("当前没有职工信息!\n");
return;
}
Employee *current = head;
while (current != NULL) {
if (current->id == id) {
printf("请输入新的姓名:");
scanf("%s", current->name);
printf("请输入新的性别:");
scanf("%s", current->gender);
printf("请输入新的出生年月:");
scanf("%s", current->birth);
printf("请输入新的籍贯:");
scanf("%s", current->hometown);
printf("请输入新的部门:");
scanf("%s", current->department);
printf("修改成功!\n");
return;
}
current = current->next;
}
printf("没有找到工号为%d的职工信息!\n", id);
}
// 统计年龄
void statisticsAge() {
if (head == NULL) {
printf("当前没有职工信息!\n");
return;
}
int age1 = 0, age2 = 0, age3 = 0;
Employee *current = head;
while (current != NULL) {
int birthYear = atoi(strtok(current->birth, "-"));
int nowYear = atoi(strtok(NULL, "-"));
int age = 2021 - nowYear;
if (age < 30) {
age1++;
} else if (age < 50) {
age2++;
} else {
age3++;
}
current = current->next;
}
printf("30岁以下的人数:%d\n", age1);
printf("30-50岁的人数:%d\n", age2);
printf("50岁以上的人数:%d\n", age3);
}
int main() {
int choice = -1;
while (choice != 0) {
showMenu();
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
showAllEmployees();
break;
case 2: {
int id;
printf("请输入工号:");
scanf("%d", &id);
findEmployeeById(id);
break;
}
case 3: {
char name[20];
printf("请输入姓名:");
scanf("%s", name);
findEmployeeByName(name);
break;
}
case 4: {
char gender[5];
printf("请输入性别:");
scanf("%s", gender);
findEmployeeByGender(gender);
break;
}
case 5: {
char department[20];
printf("请输入部门:");
scanf("%s", department);
findEmployeeByDepartment(department);
break;
}
case 6: {
Employee *e = (Employee *)malloc(sizeof(Employee));
printf("请输入工号:");
scanf("%d", &(e->id));
printf("请输入姓名:");
scanf("%s", e->name);
printf("请输入性别:");
scanf("%s", e->gender);
printf("请输入出生年月(格式为xxxx-xx-xx):");
scanf("%s", e->birth);
printf("请输入籍贯:");
scanf("%s", e->hometown);
printf("请输入部门:");
scanf("%s", e->department);
e->next = NULL;
insertEmployee(e);
printf("插入成功!\n");
break;
}
case 7: {
int id;
printf("请输入工号:");
scanf("%d", &id);
deleteEmployeeById(id);
break;
}
case 8: {
int order;
printf("请选择排序方式(1为升序,-1为降序):");
scanf("%d", &order);
sortEmployeeByBirth(order);
break;
}
case 9: {
int order;
printf("请选择排序方式(1为升序,-1为降序):");
scanf("%d", &order);
sortEmployeeById(order);
break;
}
case 10: {
int id;
printf("请输入工号:");
scanf("%d", &id);
modifyEmployee(id);
break;
}
case 11:
statisticsAge();
break;
case 0:
printf("谢谢使用,再见!\n");
break;
default:
printf("输入有误,请重新输入!\n");
}
}
return 0;
}
```
阅读全文