以下是一个可以实现上述功能的 C 程序示例:
``` #include <stdio.h> #include <stdlib.h> #include <string.h>
#define NUM_STUDENTS 20 #define NUM_SUBJECTS 5 #define NAME_LEN 50 #define ID_LEN 10
// 定义学生结构体 typedef struct { char id[ID_LEN]; char name[NAME_LEN]; int scores[NUM_SUBJECTS]; int total; char award[20]; } Student;
// 从键盘读入学生信息 void input_students(Student *students) { for (int i = 0; i < NUM_STUDENTS; i++) { printf("请输入第 %d 个学生的信息\n", i + 1);
printf("请输入学号:");
scanf("%s", students[i].id);
printf("请输入姓名:");
scanf("%s", students[i].name);
printf("请输入 ABCDE 五门课的成绩:");
int total = 0;
for (int j = 0; j < NUM_SUBJECTS; j++) {
scanf("%d", &students[i].scores[j]);
total += students[i].scores[j];
}
students[i].total = total;
}
}
// 按总成绩排序 void sort_by_total(Student *students) { for (int i = 0; i < NUM_STUDENTS; i++) { for (int j = i + 1; j < NUM_STUDENTS; j++) { if (students[i].total < students[j].total) { Student temp = students[i]; students[i] = students[j]; students[j] = temp; } } } }
// 设置获奖情况 void set_award(Student *students) { int num_first = NUM_STUDENTS * 0.05; int num_second = NUM_STUDENTS * 0.1; int num_third = NUM_STUDENTS * 0.15;
for (int i = 0; i < NUM_STUDENTS; i++) {
if (i < num_first) {
strcpy(students[i].award, "一等奖");
} else if (i < num_first + num_second) {
strcpy(students[i].award, "二等奖");
} else if (i < num_first + num_