用C语言写一个人事管理系统
时间: 2023-11-11 09:05:49 浏览: 135
人事管理系统是一个比较复杂的应用,需要考虑到很多方面,比如员工信息的录入、修改和查询、工资管理、考勤管理、人员调配等等。下面是一个简单的人事管理系统的示例代码,供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义员工信息结构体
typedef struct Employee {
char id[10]; // 员工编号
char name[20]; // 姓名
int age; // 年龄
char gender; // 性别
char dept[20]; // 部门
int salary; // 工资
int attendance; // 出勤天数
} Employee;
// 定义员工链表节点结构体
typedef struct EmployeeNode {
Employee data; // 员工信息
struct EmployeeNode *next; // 指向下一个节点的指针
} EmployeeNode;
// 定义员工链表结构体
typedef struct EmployeeList {
EmployeeNode *head; // 头节点指针
int count; // 员工数量
} EmployeeList;
// 初始化员工链表
void initEmployeeList(EmployeeList *list) {
list->head = NULL;
list->count = 0;
}
// 添加员工信息
void addEmployee(EmployeeList *list) {
EmployeeNode *node = (EmployeeNode *) malloc(sizeof(EmployeeNode));
printf("请输入员工编号:");
scanf("%s", node->data.id);
printf("请输入员工姓名:");
scanf("%s", node->data.name);
printf("请输入员工年龄:");
scanf("%d", &node->data.age);
printf("请输入员工性别(m/f):");
scanf(" %c", &node->data.gender);
printf("请输入员工部门:");
scanf("%s", node->data.dept);
printf("请输入员工工资:");
scanf("%d", &node->data.salary);
printf("请输入员工出勤天数:");
scanf("%d", &node->data.attendance);
node->next = NULL;
if (list->head == NULL) {
list->head = node;
} else {
EmployeeNode *p = list->head;
while (p->next != NULL) {
p = p->next;
}
p->next = node;
}
list->count++;
printf("添加成功!\n");
}
// 根据员工编号查找员工信息
void findEmployeeById(EmployeeList *list) {
char id[10];
printf("请输入要查找的员工编号:");
scanf("%s", id);
EmployeeNode *p = list->head;
while (p != NULL) {
if (strcmp(p->data.id, id) == 0) {
printf("编号\t姓名\t年龄\t性别\t部门\t工资\t出勤天数\n");
printf("%s\t%s\t%d\t%c\t%s\t%d\t%d\n", p->data.id, p->data.name, p->data.age, p->data.gender, p->data.dept, p->data.salary, p->data.attendance);
return;
}
p = p->next;
}
printf("未找到该员工!\n");
}
// 显示所有员工信息
void showAllEmployees(EmployeeList *list) {
if (list->count == 0) {
printf("暂无员工信息!\n");
return;
}
printf("编号\t姓名\t年龄\t性别\t部门\t工资\t出勤天数\n");
EmployeeNode *p = list->head;
while (p != NULL) {
printf("%s\t%s\t%d\t%c\t%s\t%d\t%d\n", p->data.id, p->data.name, p->data.age, p->data.gender, p->data.dept, p->data.salary, p->data.attendance);
p = p->next;
}
}
// 修改员工信息
void modifyEmployee(EmployeeList *list) {
char id[10];
printf("请输入要修改的员工编号:");
scanf("%s", id);
EmployeeNode *p = list->head;
while (p != NULL) {
if (strcmp(p->data.id, id) == 0) {
printf("请输入员工姓名(原值:%s):", p->data.name);
scanf("%s", p->data.name);
printf("请输入员工年龄(原值:%d):", p->data.age);
scanf("%d", &p->data.age);
printf("请输入员工性别(原值:%c):", p->data.gender);
scanf(" %c", &p->data.gender);
printf("请输入员工部门(原值:%s):", p->data.dept);
scanf("%s", p->data.dept);
printf("请输入员工工资(原值:%d):", p->data.salary);
scanf("%d", &p->data.salary);
printf("请输入员工出勤天数(原值:%d):", p->data.attendance);
scanf("%d", &p->data.attendance);
printf("修改成功!\n");
return;
}
p = p->next;
}
printf("未找到该员工!\n");
}
// 删除员工信息
void deleteEmployee(EmployeeList *list) {
char id[10];
printf("请输入要删除的员工编号:");
scanf("%s", id);
EmployeeNode *p = list->head;
EmployeeNode *prev = NULL;
while (p != NULL) {
if (strcmp(p->data.id, id) == 0) {
if (prev == NULL) {
list->head = p->next;
} else {
prev->next = p->next;
}
free(p);
list->count--;
printf("删除成功!\n");
return;
}
prev = p;
p = p->next;
}
printf("未找到该员工!\n");
}
// 计算员工工资总和
int getSalarySum(EmployeeList *list) {
int sum = 0;
EmployeeNode *p = list->head;
while (p != NULL) {
sum += p->data.salary;
p = p->next;
}
return sum;
}
// 计算员工平均工资
float getAverageSalary(EmployeeList *list) {
if (list->count == 0) {
return 0;
}
return (float) getSalarySum(list) / list->count;
}
// 主函数
int main() {
EmployeeList list;
initEmployeeList(&list);
int choice;
do {
printf("请选择操作:\n");
printf("1. 添加员工信息\n");
printf("2. 根据员工编号查找员工信息\n");
printf("3. 显示所有员工信息\n");
printf("4. 修改员工信息\n");
printf("5. 删除员工信息\n");
printf("6. 计算员工平均工资\n");
printf("7. 退出程序\n");
printf("请选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee(&list);
break;
case 2:
findEmployeeById(&list);
break;
case 3:
showAllEmployees(&list);
break;
case 4:
modifyEmployee(&list);
break;
case 5:
deleteEmployee(&list);
break;
case 6:
printf("员工平均工资为:%.2f\n", getAverageSalary(&list));
break;
case 7:
printf("程序已退出!\n");
break;
default:
printf("无效的选择!\n");
break;
}
} while (choice != 7);
return 0;
}
```
以上示例代码实现了基本的员工信息录入、修改、查询和删除功能,以及计算员工平均工资的功能。实际应用中,还需要根据需要添加更多的功能,比如考勤管理、部门管理等。
阅读全文