/*本次作业对于我还是很有难度的,经过不断失败、尝试,总算通过了。由于自己的不算规范,以下标出自己的环境: Linux 3.13.0-29-generic #53-Ubuntu SMP Wed Jun 4 21:00:20 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2 Copyright (C) 2013 Free Software Foundation, Inc. stustruct.c: In function ‘student_input’: stustruct.c:57:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat=] scanf("%s", &pStudent->name); ^ stustruct.c: In function ‘student_get_score’: stustruct.c:82:2: warning: writing into constant object (argument 2) [-Wformat=] scanf("%d", &pStudent->score[index]); ^ K.Weng 5 5 5 T.Dixon 4 3 2 V.Chu 3 4 5 L.Tson 4 3 4 L.Lee 3 4 3 I.Young 4 2 5 K.Hiro 4 2 1 G.Ping 4 4 4 H.Gu 2 3 4 J.Jon 5 4 3 数据保存为studata文件一次输入。 */ #include <stdio.h> typedef struct { char name[20]; int score[3]; } Student; // 输入一个学生的数据 Student* student_input(Student *pStudent); // 输出一个学生的数据,包括平均成绩 void student_print(const Student *pStudent); // 计算一个学生的平均成绩 double student_average(const Student *pStudent); // 获得学生的一个成绩 int student_get_score(const Student *pStudent, int index); int main(int argc, char const *argv[]) { double avgs[3],sum[3]; int min[3], max[3]; int i,j; Student St[10]; for(i=0;i<10;i++){ //printf("Input a Student data:"); student_input(&St[i]); for(j=0;j<3;j++){ St[i].score[j]=student_get_score(&St[i], j); } } printf("no\tname\t\tscore1\tscore2\tscore3\taverage\n"); for(i=0;i<10;i++){ printf("%d\t", i+1); student_print(&St[i]); } for(j=0;j<3;j++){ sum[j]=min[j]=max[j]=St[0].score[j]; for(i=1;i<10;i++){ if(St[i].score[j]<min[j]){ min[j]=St[i].score[j]; } else if(St[i].score[j]>max[j]){ max[j]=St[i].score[j]; } sum[j] +=St[i].score[j]; } avgs[j]=sum[j]/10; } printf(" \taverage\t\t%.1f\t%.1f\t%.1f\n", avgs[0], avgs[1], avgs[2]); printf(" \tmin\t\t%d\t%d\t%d\n", min[0], min[1], min[2]); printf(" \tmax\t\t%d\t%d\t%d\n", max[0], max[1], max[2]); return 0; } Student* student_input(Student *pStudent) { scanf("%s", &pStudent->name); return pStudent; } void student_print(const Student *pStudent) { int j; double avg; printf("%s\t", pStudent->name); for(j=0;j<3;j++){ printf("\t%d", pStudent->score[j]); avg=student_average(pStudent); } printf("\t%.5lg\n", avg); } double student_average(const Student *pStudent) { double sum=0; int i; for(i=0;i<3;i++){ sum += pStudent->score[i]; } return sum/3; } int student_get_score(const Student *pStudent, int index) { scanf("%d", &pStudent->score[index]); return pStudent->score[index]; } /* ./stustruct <studata no name score1 score2 score3 average 1 K.Weng 5 5 5 5 2 T.Dixon 4 3 2 3 3 V.Chu 3 4 5 4 4 L.Tson 4 3 4 3.6667 5 L.Lee 3 4 3 3.3333 6 I.Young 4 2 5 3.6667 7 K.Hiro 4 2 1 2.3333 8 G.Ping 4 4 4 4 9 H.Gu 2 3 4 3 10 J.Jon 5 4 3 4 average 3.8 3.4 3.6 min 2 2 1 max 5 5 5 */
时间: 2024-11-08 23:07:59