员工信息管理系统c语言代码
时间: 2023-07-26 15:03:28 浏览: 196
以下是一个简单的员工信息管理系统的 C 语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEES 100 // 最大员工数
struct employee {
int id;
char name[50];
int age;
float salary;
};
struct employee employees[MAX_EMPLOYEES]; // 定义员工数组
int num_employees = 0; // 当前员工数
void add_employee() {
if (num_employees >= MAX_EMPLOYEES) {
printf("员工数已达到上限,无法添加更多员工。\n");
return;
}
printf("请输入新员工的信息:\n");
printf("编号:");
scanf("%d", &employees[num_employees].id);
printf("姓名:");
scanf("%s", employees[num_employees].name);
printf("年龄:");
scanf("%d", &employees[num_employees].age);
printf("工资:");
scanf("%f", &employees[num_employees].salary);
num_employees++;
printf("员工添加成功。\n");
}
void delete_employee() {
int id;
printf("请输入要删除的员工编号:");
scanf("%d", &id);
int i;
for (i = 0; i < num_employees; i++) {
if (employees[i].id == id) {
// 找到要删除的员工,将其后面的员工依次前移
int j;
for (j = i; j < num_employees - 1; j++) {
employees[j] = employees[j+1];
}
num_employees--;
printf("员工删除成功。\n");
return;
}
}
printf("未找到该员工。\n");
}
void list_employees() {
if (num_employees == 0) {
printf("目前没有员工信息。\n");
return;
}
int i;
for (i = 0; i < num_employees; i++) {
printf("编号:%d\t姓名:%s\t年龄:%d\t工资:%f\n",
employees[i].id, employees[i].name, employees[i].age, employees[i].salary);
}
}
int main() {
int choice;
while (1) {
printf("请选择要执行的操作:\n");
printf("1. 添加员工\n");
printf("2. 删除员工\n");
printf("3. 列出所有员工\n");
printf("4. 退出程序\n");
printf("请选择(输入对应数字):");
scanf("%d", &choice);
switch (choice) {
case 1:
add_employee();
break;
case 2:
delete_employee();
break;
case 3:
list_employees();
break;
case 4:
printf("程序已退出。\n");
exit(0);
break;
default:
printf("无效的选择,请重新输入。\n");
}
printf("\n");
}
return 0;
}
```
该代码中定义了一个 `employee` 结构体,表示一个员工的信息,包括编号、姓名、年龄和工资。员工信息以数组的形式存储,可以进行添加、删除和列出所有员工的操作。可以通过菜单选择要执行的操作,直到选择退出程序为止。
阅读全文
相关推荐
















