c语言员工管理系统
时间: 2023-07-03 20:06:11 浏览: 129
c语言员工管理系统.docx
员工管理系统可以包括以下功能:
1. 添加员工信息:包括姓名、性别、年龄、职位、工资等;
2. 修改员工信息:可以修改员工的职位、工资等信息;
3. 删除员工信息:可以根据员工的姓名或者编号删除员工信息;
4. 查询员工信息:可以根据员工的姓名或者编号查询员工信息;
5. 统计员工信息:可以统计公司员工的总人数、平均工资等信息;
6. 存储员工信息:将员工信息存储到文件中,方便下次使用时读取。
下面是一个简单的员工管理系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEES 100 // 最大员工数量
// 员工结构体
typedef struct {
char name[20]; // 姓名
char gender[10]; // 性别
int age; // 年龄
char position[20]; // 职位
float salary; // 工资
} Employee;
Employee employees[MAX_EMPLOYEES]; // 员工数组
int numEmployees = 0; // 员工数量
// 添加员工
void addEmployee() {
Employee employee;
printf("请输入员工姓名:");
scanf("%s", employee.name);
printf("请输入员工性别:");
scanf("%s", employee.gender);
printf("请输入员工年龄:");
scanf("%d", &employee.age);
printf("请输入员工职位:");
scanf("%s", employee.position);
printf("请输入员工工资:");
scanf("%f", &employee.salary);
employees[numEmployees++] = employee;
printf("%s 添加成功!\n", employee.name);
}
// 修改员工信息
void modifyEmployee() {
int index;
printf("请输入要修改的员工编号:");
scanf("%d", &index);
if (index >= 1 && index <= numEmployees) {
Employee *employee = &employees[index - 1];
printf("员工姓名:%s\n", employee->name);
printf("员工性别:%s\n", employee->gender);
printf("员工年龄:%d\n", employee->age);
printf("员工职位:%s\n", employee->position);
printf("员工工资:%g\n", employee->salary);
printf("请输入新的员工职位:");
scanf("%s", employee->position);
printf("请输入新的员工工资:");
scanf("%f", &employee->salary);
printf("%s 修改成功!\n", employee->name);
} else {
printf("无效的员工编号!\n");
}
}
// 删除员工
void deleteEmployee() {
int index;
printf("请输入要删除的员工编号:");
scanf("%d", &index);
if (index >= 1 && index <= numEmployees) {
Employee *employee = &employees[index - 1];
printf("确定要删除员工 %s 吗?(y/n)", employee->name);
char confirm[10];
scanf("%s", confirm);
if (strcmp(confirm, "y") == 0) {
for (int i = index; i < numEmployees; i++) {
employees[i - 1] = employees[i];
}
numEmployees--;
printf("%s 删除成功!\n", employee->name);
}
} else {
printf("无效的员工编号!\n");
}
}
// 查询员工信息
void queryEmployee() {
int index;
printf("请输入要查询的员工编号:");
scanf("%d", &index);
if (index >= 1 && index <= numEmployees) {
Employee *employee = &employees[index - 1];
printf("员工姓名:%s\n", employee->name);
printf("员工性别:%s\n", employee->gender);
printf("员工年龄:%d\n", employee->age);
printf("员工职位:%s\n", employee->position);
printf("员工工资:%g\n", employee->salary);
} else {
printf("无效的员工编号!\n");
}
}
// 统计员工信息
void statisticsEmployee() {
float totalSalary = 0;
for (int i = 0; i < numEmployees; i++) {
totalSalary += employees[i].salary;
}
printf("公司员工总人数:%d\n", numEmployees);
printf("公司员工平均工资:%g\n", numEmployees > 0 ? totalSalary / numEmployees : 0);
}
// 存储员工信息
void saveEmployees() {
FILE *file = fopen("employees.dat", "wb");
if (file != NULL) {
fwrite(&numEmployees, sizeof(int), 1, file);
fwrite(employees, sizeof(Employee), numEmployees, file);
fclose(file);
printf("员工信息保存成功!\n");
} else {
printf("员工信息保存失败!\n");
}
}
// 加载员工信息
void loadEmployees() {
FILE *file = fopen("employees.dat", "rb");
if (file != NULL) {
fread(&numEmployees, sizeof(int), 1, file);
fread(employees, sizeof(Employee), numEmployees, file);
fclose(file);
printf("员工信息加载成功!\n");
} else {
printf("员工信息加载失败!\n");
}
}
// 显示菜单
void showMenu() {
printf("========================================\n");
printf("1. 添加员工\n");
printf("2. 修改员工信息\n");
printf("3. 删除员工\n");
printf("4. 查询员工信息\n");
printf("5. 统计员工信息\n");
printf("6. 存储员工信息\n");
printf("7. 加载员工信息\n");
printf("0. 退出\n");
printf("========================================\n");
}
int main() {
int choice;
loadEmployees(); // 加载员工信息
do {
showMenu(); // 显示菜单
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee(); // 添加员工
break;
case 2:
modifyEmployee(); // 修改员工信息
break;
case 3:
deleteEmployee(); // 删除员工
break;
case 4:
queryEmployee(); // 查询员工信息
break;
case 5:
statisticsEmployee(); // 统计员工信息
break;
case 6:
saveEmployees(); // 存储员工信息
break;
case 7:
loadEmployees(); // 加载员工信息
break;
case 0:
printf("谢谢使用,再见!\n");
break;
default:
printf("无效的操作编号!\n");
break;
}
} while (choice != 0);
return 0;
}
```
阅读全文