首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一道c语言题

2012-03-15 
求助一道c语言题题目是,通过函数调用的方法实现:输入50组数据,数据包括姓名和成绩,并输出最高分,最低分,前

求助一道c语言题
题目是,通过函数调用的方法实现:输入50组数据,数据包括姓名和成绩,并输出最高分,最低分,前三名成绩,平均分及超过平均分人数,并把以上输入到文本中。。。

[解决办法]

C/C++ code
#include <stdio.h>FILE *stream;#define MAXNUM  5struct 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);} 

热点排行