课上作业
#include <stdio.h> struct student { int num; char name[20]; int score[3]; double aver; }; void input(struct student stu[]) { int i; for (i = 0; i < 5; i++) scanf("%d%s%d%d%d",&stu[i].num,&stu[i].name, &stu[i].score[0],& stu[i].score[1], &stu[i].score[2]); } void avera(struct student stu[]) { int i, aver1,aver2,aver3,sum1=0, sum2=0, sum3=0; for (i = 0; i < 5; i++) { sum1 += stu[i].score[0]; sum2 += stu[i].score[1]; sum3 += stu[i].score[2]; aver1 = sum1 / 5; aver2 = sum2 / 5; aver3 = sum3 / 5; } printf("aver1=%d aver2=%d aver3=%d\n", aver1, aver2, aver3); } void averb(struct student stu[]) { int j; double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0; for (j = 0; j < 3; j++){ sum1 += stu[0].score[j]; sum2 += stu[1].score[j]; sum3 += stu[2].score[j]; sum4 += stu[3].score[j]; sum5 += stu[4].score[j]; } stu[0].aver = sum1 / 3, stu[1].aver = sum2 / 3, stu[2].aver = sum3 / 3, stu[3].aver = sum4 / 3, stu[4].aver = sum5 / 3; printf("aver1=%f aver2=%f aver3=%f aver4=%f aver5=%f\n", stu[0].aver, stu[1].aver, stu[2].aver, stu[3].aver, stu[4].aver); } void max(struct student stu[]) { int i,m=0; for (i = 0; i < 5; i++) if (stu[i].aver>stu[m].aver) m=i; printf("%d %s %d %d %d\n", stu[m].num, stu[m].name, stu[m].score[0], stu[m].score[1], stu[m].score[2]); } int main() { struct student stu[5],*p; p = stu; input(p); avera(p); averb(p); max(p); return 0; } /*1 zhao 74 89 90 2 song 77 80 91 3 zhen 68 59 96 4 yang 78 85 90 5 chen 81 99 83 aver1=75 aver2=82 aver3=90 aver1=84.333333 aver2=82.666667 aver3=74.333333 aver4=84.333333 aver5=87.666667 5 chen 81 99 83 请按任意键继续. . .*/
码云截图: