C语言用结构体数组实现以下功能:有5个学生,每个学生的数据包括学号、姓名、数学课成绩、英语课成绩和计算机课成绩。从键盘输入5个学生数据,要求输出每门课的平均成绩,以及三门课总分最高的学生数据(包括学号、姓名、每门课的成绩、总分、平均分)
时间: 2024-01-06 08:05:28 浏览: 222
C语言利用结构体数组实现学生成绩管理系统
5星 · 资源好评率100%
以下是用C语言结构体数组实现该功能的代码示例:
```c
#include <stdio.h>
struct Student {
int number;
char name[20];
int mathScore;
int englishScore;
int computerScore;
int totalScore;
float averageScore;
};
int main() {
struct Student students[5];
int maxTotalScore = 0;
int maxTotalScoreIndex = 0;
// 输入学生数据
for (int i = 0; i < 5; i++) {
printf("请输入第%d个学生的学号:", i + 1);
scanf("%d", &students[i].number);
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", students[i].name);
printf("请输入第%d个学生的数学成绩:", i + 1);
scanf("%d", &students[i].mathScore);
printf("请输入第%d个学生的英语成绩:", i + 1);
scanf("%d", &students[i].englishScore);
printf("请输入第%d个学生的计算机成绩:", i + 1);
scanf("%d", &students[i].computerScore);
// 计算总分和平均分
students[i].totalScore = students[i].mathScore + students[i].englishScore + students[i].computerScore;
students[i].averageScore = students[i].totalScore / 3.0;
// 更新最高总分学生的索引
if (students[i].totalScore > maxTotalScore) {
maxTotalScore = students[i].totalScore;
maxTotalScoreIndex = i;
}
}
// 输出每门课的平均成绩
printf("每门课的平均成绩:\n");
float mathAverage = 0, englishAverage = 0, computerAverage = 0;
for (int i = 0; i < 5; i++) {
mathAverage += students[i].mathScore;
englishAverage += students[i].englishScore;
computerAverage += students[i].computerScore;
}
mathAverage /= 5;
englishAverage /= 5;
computerAverage /= 5;
printf("数学课平均成绩:%f\n", mathAverage);
printf("英语课平均成绩:%f\n", englishAverage);
printf("计算机课平均成绩:%f\n", computerAverage);
// 输出三门课总分最高的学生数据
printf("三门课总分最高的学生数据:\n");
printf("学号:%d\n", students[maxTotalScoreIndex].number);
printf("姓名:%s\n", students[maxTotalScoreIndex].name);
printf("数学成绩:%d\n", students[maxTotalScoreIndex].mathScore);
printf("英语成绩:%d\n", students[maxTotalScoreIndex].englishScore);
printf("计算机成绩:%d\n", students[maxTotalScoreIndex].computerScore);
printf("总分:%d\n", students[maxTotalScoreIndex].totalScore);
printf("平均分:%f\n", students[maxTotalScoreIndex].averageScore);
return 0;
}
```
阅读全文