优化这段代码,用c语言
时间: 2024-03-08 14:47:52 浏览: 9
好的,以下是优化后的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_EMPLOYEE_COUNT 100
// 定义结构体,用于存储员工信息
typedef struct Employee {
int id;
int score;
} Employee;
int main() {
// 定义数组,用于存储员工信息
Employee employees[MAX_EMPLOYEE_COUNT];
int employee_count = 0;
// 循环输入员工工号和绩效考核分数
while (1) {
int employee_id, score;
printf("请输入员工工号(三位数,输入0结束):");
scanf("%d", &employee_id);
if (employee_id == 0) {
break;
}
printf("请输入该员工本月的绩效考核分数(百分制):");
scanf("%d", &score);
employees[employee_count].id = employee_id;
employees[employee_count].score = score;
employee_count++;
}
// 计算平均值、最大值、最小值和得60分以下的员工数和工号
float total_score = 0;
int max_score = 0;
int min_score = 100;
int below_60_count = 0;
int below_60_employee_ids[MAX_EMPLOYEE_COUNT];
for (int i = 0; i < employee_count; i++) {
total_score += employees[i].score;
if (employees[i].score > max_score) {
max_score = employees[i].score;
}
if (employees[i].score < min_score) {
min_score = employees[i].score;
}
if (employees[i].score < 60) {
below_60_count++;
below_60_employee_ids[below_60_count - 1] = employees[i].id;
}
}
float average_score = total_score / employee_count;
// 输出结果
printf("所有员工分数的平均值为:%.2f\n", average_score);
printf("其中的最大值为:%d\n", max_score);
printf("其中的最小值为:%d\n", min_score);
printf("得60分以下的有%d位员工,他们的工号分别为:", below_60_count);
for (int i = 0; i < below_60_count; i++) {
printf("%d ", below_60_employee_ids[i]);
}
printf("\n");
return 0;
}
```
这段代码的优化点在于使用了结构体来存储员工信息,使代码更加简洁和易读。同时,将数组的大小定义为宏常量,使代码具有更好的可维护性。