员工信息管理系统
在日常的企业管理中,员工信息的管理显得尤为重要。为了提高员工信息管理的效率,我们设计并实现了一个简单的员工信息管理系统。该系统主要使用C语言编写,具备输入、显示、查询、更新(增加、删除、修改)、读取文件和保存文件等功能。
系统功能
1. 输入员工信息
该功能允许用户输入多名员工的信息,包括姓名、工号、性别、联系电话、学历、职位和薪资。输入信息时需按特定格式一次性输入多项内容,并用空格分隔。输入的信息会被存储在一个全局的员工数组中。
c
void input_info() {
int numToAdd;
printf("请输入要添加的员工数量:");
scanf("%d", &numToAdd);
clearBuffer();
if (numToAdd <= 0 || numToAdd > 100 - numEmployees) {
printf("输入数量无效或超出可添加的最大数量。\n");
return;
}
printf("请依次输入每位员工的姓名 工号 性别 联系电话 学历 职位 薪资,用空格分隔:\n");
for (int i = 0; i < numToAdd; ++i) {
struct Employee newEmployee;
if (scanf("%s %d %s %s %s %s %f",
newEmployee.name, &newEmployee.emp_id, newEmployee.gender,
newEmployee.phone, newEmployee.education, newEmployee.position,
&newEmployee.salary) != 7) {
printf("输入格式错误,请重新输入。\n");
clearBuffer();
return;
}
clearBuffer();
employees[numEmployees++] = newEmployee;
}
printf("员工信息输入成功!\n");
}
2. 显示员工信息
该功能按照分页的形式显示所有员工的信息,每页显示5名员工。如果员工数量较多,可以通过按Enter键翻页查看下一页的员工信息。
c
void display_info() {
if (numEmployees == 0) {
printf("没有员工信息可以显示。\n");
return;
}
int page = 1;
int itemsPerPage = 5;
int totalPages = (numEmployees + itemsPerPage - 1) / itemsPerPage;
while (page <= totalPages) {
printf("第%d页,共%d页\n", page, totalPages);
int start = (page - 1) * itemsPerPage;
int end = start + itemsPerPage;
if (end > numEmployees) {
end = numEmployees;
}
for (int i = start; i < end; ++i) {
printf("姓名: %s, 工号: %d, 性别: %s, 联系电话: %s, 学历: %s, 职位: %s, 薪资: %.2f\n",
employees[i].name, employees[i].emp_id, employees[i].gender,
employees[i].phone, employees[i].education, employees[i].position,
employees[i].salary);
}
if (page < totalPages) {
printf("按 Enter 键查看下一页...\n");
getchar();
}
++page;
}
}
3. 查询员工信息
该功能支持按工号、姓名和薪资三种方式查询员工信息。用户可以根据自己的需求选择相应的查询方式,并输入对应的查询条件。系统会显示符合条件的员工信息。
c
void search_info() {
if (numEmployees == 0) {
printf("没有员工信息可以查询。\n");
return;
}
int searchOption;
printf("请选择查询方式:1.按工号 2.按姓名 3.按薪资\n");
scanf("%d", &searchOption);
clearBuffer();
if (searchOption == 1) {
int empId;
printf("请输入要查询的员工工号: ");
scanf("%d", &empId);
clearBuffer();
int found = 0;
for (int i = 0; i < numEmployees; ++i) {
if (employees[i].emp_id == empId) {
found = 1;
printf("员工信息如下:\n");
printf("姓名: %s, 工号: %d, 性别: %s, 联系电话: %s, 学历: %s, 职位: %s, 薪资: %.2f\n",
employees[i].name, employees[i].emp_id, employees[i].gender,
employees[i].phone, employees[i].education, employees[i].position,
employees[i].salary);
break;
}
}
if (!found) {
printf("未找到工号为%d的员工。\n", empId);
}
} else if (searchOption == 2) {
char name[50];
printf("请输入要查询的员工姓名: ");
scanf("%s", name);
clearBuffer();
int found = 0;
for (int i = 0; i < numEmployees; ++i) {
if (strcmp(employees[i].name, name) == 0) {
found = 1;
printf("员工信息如下:\n");
printf("姓名: %s, 工号: %d, 性别: %s, 联系电话: %s, 学历: %s, 职位: %s, 薪资: %.2f\n",
employees[i].name, employees[i].emp_id, employees[i].gender,
employees[i].phone, employees[i].education, employees[i].position,
employees[i].salary);
}
}
if (!found) {
printf("未找到姓名为%s的员工。\n", name);
}
} else if (searchOption == 3) {
float salary;
printf("请输入要查询的员工薪资: ");
scanf("%f", &salary);
clearBuffer();
int found = 0;
for (int i = 0; i < numEmployees; ++i) {
if (employees[i].salary == salary) {
found = 1;
printf("员工信息如下:\n");
printf("姓名: %s, 工号: %d, 性别: %s, 联系电话: %s, 学历: %s, 职位: %s, 薪资: %.2f\n",
employees[i].name, employees[i].emp_id, employees[i].gender,
employees[i].phone, employees[i].education, employees[i].position,
employees[i].salary);
}
}
if (!found) {
printf("未找到薪资为%.2f的员工。\n", salary);
}
} else {
printf("无效的查询选项。\n");
}
}
4. 更新员工信息
更新员工信息功能包括增加、删除和修改三种操作。用户可以根据提示选择对应的操作,并输入相应的信息进行更新。
c
void update_info() {
int updateOption;
printf("请选择更新方式:1.增加 2.删除 3.修改\n");
scanf("%d", &updateOption);
clearBuffer();
switch (updateOption) {
case 1:
add_info();
break;
case 2:
delete_info();
break;
case 3:
modify_info();
break;
default:
printf("无效的更新选项。\n");
break;
}
}
5. 读取文件信息
该功能用于从文件中读取员工信息,并将读取到的信息存储到全局员工数组中。读取成功后,会显示相应提示信息。
c
void read_info() {
FILE* file = fopen("employee_data.txt", "r");
if (file == NULL) {
printf("无法打开文件或文件不存在。\n");
return;
}
numEmployees = 0;
while (fscanf(file, "%s %d %s %s %s %s %f",
employees[numEmployees].name, &employees[numEmployees].emp_id,
employees[numEmployees].gender, employees[numEmployees].phone,
employees[numEmployees].education, employees[numEmployees].position,
&employees[numEmployees].salary) == 7) {
numEmployees++;
if (numEmployees >= 100) {
printf("警告:员工信息数量超过程序允许的最大值。\n");
break;
}
}
fclose(file);
printf("员工信息从文件中读取成功!\n");
}
6. 保存文件信息
该功能用于将当前存储的员工信息保存到文件中。保存成功后,会显示相应提示信息。
c
void save_info() {
FILE* file = fopen("employee_data.txt", "w");
if (file == NULL) {
printf("无法打开文件来保存员工信息。\n");
return;
}
for (int i = 0; i < numEmployees; ++i) {
fprintf(file, "%s %d %s %s %s %s %.2f\n",
employees[i].name, employees[i].emp_id, employees[i].gender,
employees
[i].phone, employees[i].education, employees[i].position,
employees[i].salary);
}
fclose(file);
printf("员工信息成功保存到文件中!\n");
}
7. 清空缓冲区
该功能用于清空输入缓冲区,避免因缓冲区中的多余字符影响后续的输入操作。
c
void clearBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
系统主界面
系统通过一个主菜单界面来引导用户进行操作。用户可以根据提示输入相应的操作序号来执行对应的功能。主菜单界面如下所示:
c
int main() {
int a = 0;
printf("\n\n*********************【员工信息管理系统】***************\n");
do {
printf("********** 1:员工信息输入 **********\n");
printf("********** 2:员工信息显示 **********\n");
printf("********** 3:员工信息查询 **********\n");
printf("********** 4:员工信息更新-增/改/删 **********\n");
printf("********** 5:读取文件信息 **********\n");
printf("********** 0:保存并退出系统 **********\n");
printf("请输入需要进行的操作序号:");
scanf("%d", &a);
clearBuffer();
switch (a) {
case 1:
input_info();
break;
case 2:
display_info();
break;
case 3:
search_info();
break;
case 4:
update_info();
break;
case 5:
read_info();
break;
case 0:
save_info();
printf("保存并退出系统。\n");
break;
default:
printf("无效的操作序号,请重新输入。\n");
break;
}
} while (a != 0);
return 0;
}
通过上述功能和代码,我们实现了一个简单而实用的员工信息管理系统。该系统不仅可以有效地管理员工信息,还具备良好的用户交互体验。未来可以根据实际需求进行功能扩展和优化。
如有课程设计需求 请私聊作者