帮我用c++写一个职工管理系统的代码
时间: 2024-05-01 12:18:18 浏览: 12
好的,以下是一个简单的职工管理系统的 C 代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEES 100
struct Employee {
int id;
char name[50];
int age;
char gender[10];
char department[50];
char position[50];
double salary;
};
// 声明全局变量
struct Employee employees[MAX_EMPLOYEES];
int numEmployees = 0;
// 声明函数原型
void addEmployee();
void removeEmployee();
void displayAllEmployees();
void displayEmployee(int id);
void saveToFile();
void readFromFile();
int main() {
int choice;
// 从文件读取职工信息
readFromFile();
do {
printf("\n");
printf("职工管理系统菜单\n");
printf("1. 添加职工\n");
printf("2. 删除职工\n");
printf("3. 显示所有职工\n");
printf("4. 查找职工\n");
printf("5. 保存到文件\n");
printf("0. 退出\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee();
break;
case 2:
removeEmployee();
break;
case 3:
displayAllEmployees();
break;
case 4:
printf("请输入要查找的职工 ID:");
int id;
scanf("%d", &id);
displayEmployee(id);
break;
case 5:
saveToFile();
break;
case 0:
break;
default:
printf("无效的选择,请重新输入!\n");
}
} while (choice != 0);
// 将职工信息保存到文件
saveToFile();
printf("程序已退出,谢谢使用!\n");
return 0;
}
void addEmployee() {
printf("添加职工\n");
if (numEmployees >= MAX_EMPLOYEES) {
// 如果已超过最大职工人数,则无法添加新职工
printf("已达到最大职工人数,无法添加新职工!\n");
return;
}
struct Employee newEmployee;
newEmployee.id = numEmployees + 1;
printf("请输入职工姓名:");
scanf("%s", newEmployee.name);
printf("请输入职工年龄:");
scanf("%d", &newEmployee.age);
printf("请输入职工性别:");
scanf("%s", newEmployee.gender);
printf("请输入所在部门:");
scanf("%s", newEmployee.department);
printf("请输入职位:");
scanf("%s", newEmployee.position);
printf("请输入工资:");
scanf("%lf", &newEmployee.salary);
employees[numEmployees] = newEmployee;
numEmployees++;
printf("职工信息已添加!\n");
}
void removeEmployee() {
printf("删除职工\n");
printf("请输入要删除的职工 ID:");
int id;
scanf("%d", &id);
// 找到要删除的职工索引
int index = -1;
for (int i = 0; i < numEmployees; i++) {
if (employees[i].id == id) {
index = i;
break;
}
}
if (index >= 0) {
// 找到了要删除的职工,将其从数组中删除
for (int i = index; i < numEmployees - 1; i++) {
employees[i] = employees[i + 1];
}
numEmployees--;
printf("职工信息已删除!\n");
} else {
printf("未找到要删除的职工!\n");
}
}
void displayAllEmployees() {
printf("所有职工信息\n");
if (numEmployees == 0) {
printf("当前没有任何职工信息!\n");
return;
}
printf("| %-5s | %-10s | %-5s | %-10s | %-20s | %-20s | %-10s |\n", "ID", "姓名", "年龄", "性别", "部门", "职位", "工资");
printf("--------------------------------------------------------------------------\n");
for (int i = 0; i < numEmployees; i++) {
struct Employee e = employees[i];
printf("| %-5d | %-10s | %-5d | %-10s | %-20s | %-20s | %-10.2lf |\n", e.id, e.name, e.age, e.gender, e.department, e.position, e.salary);
}
}
void displayEmployee(int id) {
printf("查找职工\n");
// 找到要查找的职工索引
int index = -1;
for (int i = 0; i < numEmployees; i++) {
if (employees[i].id == id) {
index = i;
break;
}
}
if (index >= 0) {
// 找到了要查找的职工,显示其信息
struct Employee e = employees[index];
printf("| %-5s | %-10s | %-5s | %-10s | %-20s | %-20s | %-10s |\n", "ID", "姓名", "年龄", "性别", "部门", "职位", "工资");
printf("--------------------------------------------------------------------------\n");
printf("| %-5d | %-10s | %-5d | %-10s | %-20s | %-20s | %-10.2lf |\n", e.id, e.name, e.age, e.gender, e.department, e.position, e.salary);
} else {
printf("未找到要查找的职工!\n");
}
}
void saveToFile() {
FILE* file = fopen("employees.txt", "w");
if (file == NULL) {
printf("无法打开保存文件!\n");
return;
}
for (int i = 0; i < numEmployees; i++) {
struct Employee e = employees[i];
fprintf(file, "%d,%s,%d,%s,%s,%s,%.2lf\n", e.id, e.name, e.age, e.gender, e.department, e.position, e.salary);
}
fclose(file);
printf("职工信息已保存到文件!\n");
}
void readFromFile() {
FILE* file = fopen("employees.txt", "r");
if (file == NULL) {
printf("无法打开读取文件,可能是文件还不存在!\n");
return;
}
char line[256];
while (fgets(line, sizeof(line), file)) {
// 逐行读取职工信息并解析
struct Employee e;
sscanf(line, "%d,%[^,],%d,%[^,],%[^,],%[^,],%lf", &e.id, e.name, &e.age, e.gender, e.department, e.position, &e.salary);
employees[numEmployees] = e;
numEmployees++;
}
fclose(file);
printf("职工信息已从文件读取!\n");
}
```
该代码实现了添加、删除、查找和显示所有职工信息的基本功能,并可以将职工信息保存到文件中,以便下次启动程序时从文件中读取。注意,在实际使用时应该对输入进行一些错误处理,例如对输入的年龄和工资进行范围检查等等。