一道c语言题
求助一道c语言题
题目是,通过函数调用的方法实现:输入50组数据,数据包括姓名和成绩,并输出最高分,最低分,前三名成绩,平均分及超过平均分人数,并把以上输入到文本中。。。
------解决方案--------------------
题目是,通过函数调用的方法实现:输入50组数据,数据包括姓名和成绩,并输出最高分,最低分,前三名成绩,平均分及超过平均分人数,并把以上输入到文本中。。。
------解决方案--------------------
- C/C++ code
#include <stdio.h> FILE *stream; #define MAXNUM 5 struct student { char name[32]; float score; }; void main() { struct student st[MAXNUM]; int i = 0, count = 0; float ave, max=0, min=0, sum=0, top[3]={0}; stream = fopen( "data.txt", "w+" ); if( stream == NULL ) printf( "The file fscanf.out was not opened\n" ); else { for(i = 0; i < MAXNUM; i++) { printf("请输入第%d位学生的信息\n姓名:", i+1); scanf("%s", st[i].name); printf("成绩:"); scanf("%f", &st[i].score); fprintf(stream,"%s %f\n",st[i].name,st[i].score); sum += st[i].score; if (st[i].score>max) { max = st[i].score; } if (st[i].score<min) { min = st[i].score; } if (st[i].score>top[2]) { top[2] = st[i].score; if (st[i].score>top[1]) { top[2] = top[1]; top[2] = st[i].score; if (st[i].score>top[0]) { top[1] = top[0]; top[0] = st[i].score; } } } } ave=sum/MAXNUM; for (i = 0; i < MAXNUM; i++) { if(ave<st[i].score) count++; } fprintf(stream,"最高分:%f;最低分:%f;平均分:%f;\n", max, min, ave); fprintf(stream,"第一名:%f;第二名:%f;第三名:%f;\n", top[0], top[1], top[2]); fprintf(stream,"超过平均分的人数:%d\n", count); printf("最高分:%d;最低分:%d;平均分:%d;\n", max, min, ave); } fclose(stream); }