C语言实现学生成绩管理:计算平均分和最高分学生
以下是一个用C语言实现的程序,可以实现输入6个学生的数据,并输出3门课的总平均成绩以及最高分的学生的数据。\n\nc\n#include <stdio.h>\n\n#define NUM_STUDENTS 6\n#define NUM_SUBJECTS 3\n\nstruct Student {\n int id;\n char name[20];\n int scores[NUM_SUBJECTS];\n};\n\nint main() {\n struct Student students[NUM_STUDENTS];\n int totalScores[NUM_SUBJECTS] = {0};\n float avgScores[NUM_SUBJECTS];\n struct Student maxScoreStudent;\n int maxScore = 0;\n \n // 输入学生数据\n for (int i = 0; i < NUM_STUDENTS; i++) {\n printf("请输入第%d个学生的学号:", i+1);\n scanf("%d", &students[i].id);\n printf("请输入第%d个学生的姓名:", i+1);\n scanf("%s", students[i].name);\n printf("请输入第%d个学生的语文成绩:", i+1);\n scanf("%d", &students[i].scores[0]);\n printf("请输入第%d个学生的数学成绩:", i+1);\n scanf("%d", &students[i].scores[1]);\n printf("请输入第%d个学生的外语成绩:", i+1);\n scanf("%d", &students[i].scores[2]);\n printf("\n");\n }\n \n // 计算总分和平均分\n for (int i = 0; i < NUM_STUDENTS; i++) {\n for (int j = 0; j < NUM_SUBJECTS; j++) {\n totalScores[j] += students[i].scores[j];\n }\n }\n \n for (int i = 0; i < NUM_SUBJECTS; i++) {\n avgScores[i] = (float)totalScores[i] / NUM_STUDENTS;\n }\n \n // 寻找最高分的学生\n for (int i = 0; i < NUM_STUDENTS; i++) {\n int curScore = students[i].scores[0] + students[i].scores[1] + students[i].scores[2];\n if (curScore > maxScore) {\n maxScore = curScore;\n maxScoreStudent = students[i];\n }\n }\n \n // 输出结果\n printf("3门课的总平均成绩:\n");\n printf("语文:%.2f\n", avgScores[0]);\n printf("数学:%.2f\n", avgScores[1]);\n printf("外语:%.2f\n", avgScores[2]);\n \n printf("\n最高分学生的数据:\n");\n printf("学号:%d\n", maxScoreStudent.id);\n printf("姓名:%s\n", maxScoreStudent.name);\n printf("语文成绩:%d\n", maxScoreStudent.scores[0]);\n printf("数学成绩:%d\n", maxScoreStudent.scores[1]);\n printf("外语成绩:%d\n", maxScoreStudent.scores[2]);\n \n return 0;\n}\n\n\n运行程序后,会依次提示输入每个学生的学号、姓名和3门课的成绩。最后会输出3门课的总平均成绩以及最高分学生的数据。
原文地址: https://www.cveoy.top/t/topic/qB1W 著作权归作者所有。请勿转载和采集!