c语言顺序表员工绩效管理系统实现创建, 显示, 清空, 查询, 删除, 添加, 修改的功能
时间: 2023-12-13 11:04:08 浏览: 81
下面是一个基于顺序表实现员工绩效管理系统的C语言代码,包括创建、显示、清空、查询、删除、添加和修改功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEES 100
typedef struct {
char name[50];
int id;
int salary;
int performance;
} Employee;
typedef struct {
Employee employees[MAX_EMPLOYEES];
int n;
} EmployeeList;
void createList(EmployeeList *list) {
list->n = 0;
}
void display(EmployeeList *list) {
printf("ID\tName\tSalary\tPerformance\n");
for (int i = 0; i < list->n; i++) {
Employee employee = list->employees[i];
printf("%d\t%s\t%d\t%d\n", employee.id, employee.name, employee.salary, employee.performance);
}
}
void clearList(EmployeeList *list) {
list->n = 0;
}
int search(EmployeeList *list, int id) {
for (int i = 0; i < list->n; i++) {
if (list->employees[i].id == id) {
return i;
}
}
return -1;
}
void delete(EmployeeList *list, int id) {
int index = search(list, id);
if (index != -1) {
for (int i = index; i < list->n - 1; i++) {
list->employees[i] = list->employees[i + 1];
}
list->n--;
}
}
void add(EmployeeList *list, char name[], int id, int salary) {
Employee employee = {0};
strcpy(employee.name, name);
employee.id = id;
employee.salary = salary;
employee.performance = 0;
list->employees[list->n++] = employee;
}
void modify(EmployeeList *list, int id, int salary) {
int index = search(list, id);
if (index != -1) {
list->employees[index].salary = salary;
}
}
int main() {
EmployeeList list;
createList(&list);
add(&list, "Alice", 1, 5000);
add(&list, "Bob", 2, 6000);
add(&list, "Charlie", 3, 7000);
add(&list, "David", 4, 8000);
printf("Initial list:\n");
display(&list);
printf("\n");
printf("Searching for employee with ID 3:\n");
int index = search(&list, 3);
if (index != -1) {
Employee employee = list.employees[index];
printf("Found employee: %s, %d\n", employee.name, employee.salary);
} else {
printf("Employee not found.\n");
}
printf("\n");
printf("Deleting employee with ID 2:\n");
delete(&list, 2);
display(&list);
printf("\n");
printf("Adding new employee:\n");
add(&list, "Eve", 5, 9000);
display(&list);
printf("\n");
printf("Modifying employee with ID 4:\n");
modify(&list, 4, 8500);
display(&list);
printf("\n");
printf("Clearing the list:\n");
clearList(&list);
display(&list);
printf("\n");
return 0;
}
```
在这个代码中,我们定义了 `createList`、`display`、`clearList`、`search`、`delete`、`add` 和 `modify` 这些函数来实现各种功能。在主函数中,我们先创建了一个空列表,然后添加了四个员工。接着,我们对员工列表进行了各种操作,例如查找、删除、添加和修改员工信息等。最后,我们清空了列表并打印了结果。
阅读全文