用c语言,不是c++编写程序员工绩效有编号(int)、姓名(字符数组)、部门(字符数组)、绩效分数(float)等属性 使用结构体表示员工绩效 使用全局结构体数组存放绩效数据(最多1000个员工) 再定义一个全局结构体数组用来排序; 定义一个表示实际记录数量的全局变量(初值为0) 功能要求: 创建员工绩效记录 显示所有员工绩效记录 清空所有记录 创建员工绩效记录 显示所有员工绩效记录
时间: 2023-12-26 14:06:40 浏览: 82
C语言或C++通过IShellLinkA创建或解析lnk快捷方式(使用char字符数组)
以下是用C语言编写的员工绩效管理系统的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEE 1000
typedef struct {
int id;
char name[20];
char department[20];
float score;
} Employee;
Employee employees[MAX_EMPLOYEE];
Employee sortedEmployees[MAX_EMPLOYEE];
int numEmployees = 0;
void addEmployee() {
if (numEmployees >= MAX_EMPLOYEE) {
printf("Error: Maximum number of employees reached.\n");
return;
}
Employee newEmployee;
newEmployee.id = numEmployees + 1;
printf("Enter name: ");
scanf("%s", newEmployee.name);
printf("Enter department: ");
scanf("%s", newEmployee.department);
printf("Enter score: ");
scanf("%f", &newEmployee.score);
employees[numEmployees] = newEmployee;
sortedEmployees[numEmployees] = newEmployee;
numEmployees++;
printf("Employee added successfully.\n");
}
void displayEmployees(Employee *employeeList, int numEmployees) {
printf("%-5s %-20s %-20s %-10s\n", "ID", "Name", "Department", "Score");
for (int i = 0; i < numEmployees; i++) {
printf("%-5d %-20s %-20s %-10.2f\n", employeeList[i].id, employeeList[i].name, employeeList[i].department, employeeList[i].score);
}
}
void displayAllEmployees() {
displayEmployees(employees, numEmployees);
}
void clearEmployees() {
numEmployees = 0;
printf("All employee records cleared.\n");
}
int compareEmployees(const void *a, const void *b) {
Employee *employeeA = (Employee *) a;
Employee *employeeB = (Employee *) b;
if (employeeA->score < employeeB->score) {
return 1;
} else if (employeeA->score > employeeB->score) {
return -1;
} else {
return 0;
}
}
void sortEmployees() {
qsort(sortedEmployees, numEmployees, sizeof(Employee), compareEmployees);
}
void displayTopEmployees(int numTopEmployees) {
sortEmployees();
printf("Top %d employees:\n", numTopEmployees);
displayEmployees(sortedEmployees, numTopEmployees);
}
int main() {
int choice;
while (1) {
printf("\n");
printf("1. Add employee\n");
printf("2. Display all employees\n");
printf("3. Clear all employees\n");
printf("4. Display top employees\n");
printf("5. Exit\n");
printf("\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee();
break;
case 2:
displayAllEmployees();
break;
case 3:
clearEmployees();
break;
case 4:
int numTopEmployees;
printf("Enter number of top employees to display: ");
scanf("%d", &numTopEmployees);
displayTopEmployees(numTopEmployees);
break;
case 5:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
```
以上代码实现了以下功能:
1. 添加员工绩效记录
2. 显示所有员工绩效记录
3. 清空所有员工绩效记录
4. 显示绩效最高的员工
5. 退出程序
该程序使用了结构体来表示员工绩效,并使用全局结构体数组 `employees` 存放所有员工绩效数据。另外,还定义了一个全局结构体数组 `sortedEmployees` 用来排序,以及一个表示实际记录数量的全局变量 `numEmployees`。
阅读全文