?●编程要求:
??(1)从键盘输入三个学生的各项数据;
??(2)要求打印出每个学生的平均成绩,以及平均分最高的学生的各项数据(包括
?????学号、姓名、两门课成绩、平均分)。
???生的数据记录,每个记录包括num、name、score[3],在主函数中输入这些
???记录,用print()函数输出这些记录。
?●编程提示:先定义一个包含有三个成员项的结构体数组,在主函数中利用循
???环依次输入数据,并调用函数print()完成输出数据的功能。 ?
程序代码或实现过程: #include <stdio.h> #include <string.h> struct Student { ????char num[20]; ????char name[20]; ????float score[2]; }; void print(struct Student student[]) { ????int i, j; ????for (i = 0; i < 3; i++) { ????????printf("学号:%s\n", student[i].num); ????????printf("姓名:%s\n", student[i].name); ????????printf("成绩:"); ????????for (j = 0; j < 2; j++) { ????????????printf("%.2f ", student[i].score[j]); ????????} ????????printf("\n\n"); ????} } int main() { ????struct Student student[3]; ????float average[3] = {0}; ????float maxAverage; ????int maxIndex; ????int i, j; ????for (i = 0; i < 3; i++) { ????????printf("请输入第 %d 个学生的学号:", i + 1); ????????scanf("%s", student[i].num); ????????printf("请输入第 %d 个学生的姓名:", i + 1); ????????scanf("%s", student[i].name); ????????printf("请输入第 %d 个学生的两门课成绩:", i + 1); ????????for (j = 0; j < 2; j++) { ????????????scanf("%f", &student[i].score[j]); ????????????average[i] += student[i].score[j]; ????????} ????????average[i] /= 2; ????????printf("\n"); ????}? ????printf("每个学生的平均成绩:\n"); ????for (i = 0; i < 3; i++) { ????????printf("学号:%s, 平均成绩:%.2f\n", student[i].num, average[i]); ????} ????maxAverage = average[0]; ????maxIndex = 0; ????for (i = 1; i < 3; i++) { ????????if (average[i] > maxAverage) { ????????????maxAverage = average[i]; ????????????maxIndex = i; ????????} ????} ????printf("\n平均分最高的学生:\n"); ????printf("学号:%s\n", student[maxIndex].num); ????printf("姓名:%s\n", student[maxIndex].name); ????printf("成绩:"); ????for (j = 0; j < 2; j++) { ????????printf("%.2f ", student[maxIndex].score[j]); ????} ????printf("\n平均分:%.2f\n", maxAverage); ????return 0; } 2、#include <stdio.h> struct Student { ????int num; ????char name[20]; ????float score[3]; }; void print(struct Student student) { ????printf("学号:%d\n", student.num); ????printf("姓名:%s\n", student.name); ????printf("成绩:%.2f %.2f %.2f\n", student.score[0], student.score[1], student.score[2]); ????printf("\n"); } int main() { ????struct Student students[3]; ????int i, j; ????for (i = 0; i < 3; i++) { ????????printf("请输入第 %d 个学生的学号:", i + 1); ????????scanf("%d", &students[i].num); ????????printf("请输入第 %d 个学生的姓名:", i + 1); ????????scanf("%s", students[i].name); ????????printf("请输入第 %d 个学生的三门课成绩:", i + 1); ????????for (j = 0; j < 3; j++) { ????????????scanf("%f", &students[i].score[j]); ????????} ????????printf("\n"); ????} ????for (i = 0; i < 3; i++) { ????????print(students[i]); ????} ????return 0; } |