编写c项目程序 员工绩效有编号(int)、姓名(字符数组)、部门(字符数组)、绩效分数(float)等属性 使用结构体表示员工绩效 使用全局结构体数组存放绩效数据(最多1000个员工) 再定义一个全局结构体数组用来排序; 定义一个表示实际记录数量的全局变量(初值为0) 功能要求: 创建员工绩效记录 显示所有员工绩效记录 清空所有记录 创建员工绩效记录 显示所有员工绩效记录 清空所有记录 保存记录到文件 从文件读取员工绩效记录 查询员工绩效记录 删除员工绩效记录 添加一个员工绩效记录 修改员工绩效记录 绩效排名 并解释代码含义
时间: 2024-01-17 17:04:18 浏览: 97
以下是一个简单的员工绩效管理程序的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEE 1000
typedef struct {
int id;
char name[30];
char department[30];
float score;
} Employee;
Employee employees[MAX_EMPLOYEE];
Employee sorted_employees[MAX_EMPLOYEE];
int num_employees = 0;
void create_employee() {
if (num_employees >= MAX_EMPLOYEE) {
printf("Cannot add more employees.\n");
return;
}
printf("Enter employee ID: ");
scanf("%d", &employees[num_employees].id);
printf("Enter employee name: ");
scanf("%s", employees[num_employees].name);
printf("Enter employee department: ");
scanf("%s", employees[num_employees].department);
printf("Enter employee score: ");
scanf("%f", &employees[num_employees].score);
num_employees++;
}
void display_employees() {
printf("ID\tName\tDepartment\tScore\n");
for (int i = 0; i < num_employees; i++) {
printf("%d\t%s\t%s\t\t%.2f\n", employees[i].id, employees[i].name, employees[i].department, employees[i].score);
}
}
void clear_employees() {
num_employees = 0;
}
void save_employees() {
FILE *fp;
fp = fopen("employees.dat", "wb");
fwrite(employees, sizeof(Employee), num_employees, fp);
fclose(fp);
}
void load_employees() {
FILE *fp;
fp = fopen("employees.dat", "rb");
if (fp) {
num_employees = fread(employees, sizeof(Employee), MAX_EMPLOYEE, fp);
fclose(fp);
}
}
void search_employee() {
int id;
printf("Enter employee ID: ");
scanf("%d", &id);
for (int i = 0; i < num_employees; i++) {
if (employees[i].id == id) {
printf("ID\tName\tDepartment\tScore\n");
printf("%d\t%s\t%s\t\t%.2f\n", employees[i].id, employees[i].name, employees[i].department, employees[i].score);
return;
}
}
printf("Employee not found.\n");
}
void delete_employee() {
int id;
printf("Enter employee ID: ");
scanf("%d", &id);
for (int i = 0; i < num_employees; i++) {
if (employees[i].id == id) {
for (int j = i; j < num_employees - 1; j++) {
employees[j] = employees[j+1];
}
num_employees--;
printf("Employee deleted.\n");
return;
}
}
printf("Employee not found.\n");
}
void add_employee() {
if (num_employees >= MAX_EMPLOYEE) {
printf("Cannot add more employees.\n");
return;
}
Employee new_employee;
printf("Enter employee ID: ");
scanf("%d", &new_employee.id);
printf("Enter employee name: ");
scanf("%s", new_employee.name);
printf("Enter employee department: ");
scanf("%s", new_employee.department);
printf("Enter employee score: ");
scanf("%f", &new_employee.score);
int i = num_employees;
while (i > 0 && new_employee.score > employees[i-1].score) {
employees[i] = employees[i-1];
i--;
}
employees[i] = new_employee;
num_employees++;
}
void sort_employees() {
memcpy(sorted_employees, employees, num_employees * sizeof(Employee));
for (int i = 0; i < num_employees - 1; i++) {
for (int j = i + 1; j < num_employees; j++) {
if (sorted_employees[j].score > sorted_employees[i].score) {
Employee temp = sorted_employees[i];
sorted_employees[i] = sorted_employees[j];
sorted_employees[j] = temp;
}
}
}
printf("ID\tName\tDepartment\tScore\n");
for (int i = 0; i < num_employees; i++) {
printf("%d\t%s\t%s\t\t%.2f\n", sorted_employees[i].id, sorted_employees[i].name, sorted_employees[i].department, sorted_employees[i].score);
}
}
int main() {
int choice;
do {
printf("\nEmployee Performance Management System\n");
printf("1. Create employee\n");
printf("2. Display all employees\n");
printf("3. Clear all employees\n");
printf("4. Save employees to file\n");
printf("5. Load employees from file\n");
printf("6. Search employee\n");
printf("7. Delete employee\n");
printf("8. Add employee\n");
printf("9. Sort employees by score\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
create_employee();
break;
case 2:
display_employees();
break;
case 3:
clear_employees();
printf("All employees cleared.\n");
break;
case 4:
save_employees();
printf("Employees saved to file.\n");
break;
case 5:
load_employees();
printf("Employees loaded from file.\n");
break;
case 6:
search_employee();
break;
case 7:
delete_employee();
break;
case 8:
add_employee();
break;
case 9:
sort_employees();
break;
case 0:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 0);
return 0;
}
```
这个程序使用了一个结构体类型 `Employee` 来表示员工绩效记录,包含了员工的编号、姓名、部门和绩效分数。这些记录保存在一个全局的结构体数组 `employees` 中,最多可以保存1000条记录。
程序的主菜单提供了多种操作,包括创建员工绩效记录、显示所有记录、清空所有记录、保存记录到文件、从文件读取记录、查询记录、删除记录、添加记录和排序记录。其中,添加记录和排序记录比较特殊,需要在插入新记录和排序后重新调整数组中的元素顺序。
注意,在实际应用中,可能需要增加更多的输入验证和错误处理,以保证程序的健壮性和安全性。
阅读全文