#include <stdio.h>
// 定义学生结构体
typedef struct Student {
int id;
char name[20];
float score1;
float score2;
float score3;
} Student;
// 输入学生数据
void input(Student *students, int n) {
for (int i = 0; i < n; i++) {
printf(“请输入第%d个学生的学号:”, i + 1);
scanf("%d", &(students[i].id));
printf(“请输入第%d个学生的姓名:”, i + 1);
scanf("%s", students[i].name);
printf(“请输入第%d个学生的3门课的成绩:”, i + 1);
scanf("%f%f%f", &(students[i].score1), &(students[i].score2), &(students[i].score3));
}
}
// 计算总平均分
float average(Student *students, int n) {
float sum = 0;
for (int i = 0; i < n; i++) {
sum += students[i].score1 + students[i].score2 + students[i].score3;
}
return sum / (n * 3);
}
// 找出最高分的学生数据
Student max(Student *students, int n) {
int maxIndex = 0;
float maxScore = students[0].score1 + students[0].score2 + students[0].score3;
for (int i = 1; i < n; i++) {
float totalScore = students[i].score1 + students[i].score2 + students[i].score3;
if (totalScore > maxScore) {
maxIndex = i;
maxScore = totalScore;
}
}
return students[maxIndex];
}
int main() {
Student students[10];
// 输入学生数据
input(students, 10);
// 计算总平均分
float avgScore = average(students, 10);
printf(“3门课的总平均成绩为:%.2f\n”, avgScore);
// 找出最高分的学生数据
Student maxStudent = max(students, 10);
printf(“最高分的学生信息:\n”);
printf(“学号:%d\n”, maxStudent.id);
printf(“姓名:%s\n”, maxStudent.name);
printf(“成绩:%.2f %.2f %.2f\n”, maxStudent.score1, maxStudent.score2, maxStudent.score3);
printf(“平均分数:%.2f\n”, (maxStudent.score1 + maxStudent.score2 + maxStudent.score3) / 3);
return 0;
}