写一个员工管理系统c语言代码
时间: 2024-01-10 13:03:37 浏览: 61
用C编写员工管理系统
以下是一个简单的员工管理系统的C语言代码,包含了基本的员工信息录入、查询和修改功能:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NUM_EMPLOYEES 100
struct Employee {
char name[50];
int id;
float salary;
};
int num_employees = 0;
struct Employee employees[MAX_NUM_EMPLOYEES];
int add_employee() {
if (num_employees >= MAX_NUM_EMPLOYEES) {
printf("Error: Maximum number of employees exceeded.\n");
return 0;
}
struct Employee new_employee;
printf("Enter employee name: ");
scanf("%s", new_employee.name);
printf("Enter employee ID: ");
scanf("%d", &new_employee.id);
printf("Enter employee salary: ");
scanf("%f", &new_employee.salary);
employees[num_employees++] = new_employee;
printf("Employee added successfully.\n");
return 1;
}
int find_employee(int id) {
for (int i = 0; i < num_employees; i++) {
if (employees[i].id == id) {
return i;
}
}
return -1;
}
int search_employee() {
int id;
printf("Enter employee ID: ");
scanf("%d", &id);
int index = find_employee(id);
if (index == -1) {
printf("Error: Employee not found.\n");
return 0;
}
printf("Employee name: %s\n", employees[index].name);
printf("Employee ID: %d\n", employees[index].id);
printf("Employee salary: %.2f\n", employees[index].salary);
return 1;
}
int update_employee() {
int id;
printf("Enter employee ID: ");
scanf("%d", &id);
int index = find_employee(id);
if (index == -1) {
printf("Error: Employee not found.\n");
return 0;
}
struct Employee updated_employee;
printf("Enter new employee name: ");
scanf("%s", updated_employee.name);
printf("Enter new employee ID: ");
scanf("%d", &updated_employee.id);
printf("Enter new employee salary: ");
scanf("%f", &updated_employee.salary);
employees[index] = updated_employee;
printf("Employee updated successfully.\n");
return 1;
}
int main() {
int choice;
do {
printf("\nEmployee Management System\n");
printf("--------------------------\n");
printf("1. Add Employee\n");
printf("2. Search Employee\n");
printf("3. Update Employee\n");
printf("4. Exit\n");
printf("--------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_employee();
break;
case 2:
search_employee();
break;
case 3:
update_employee();
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 4);
return 0;
}
```
该代码定义了一个`Employee`结构体,其中包含了员工的姓名、ID和薪水信息。该程序支持添加员工、查询员工和更新员工信息的功能,并使用了一个简单的菜单来帮助用户进行操作。
阅读全文