专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C语言

一道c语言题

发布时间:2011-06-28 16:17:45 文章来源:www.iduyao.cn 采编人员:星星草
求助一道c语言题
题目是,通过函数调用的方法实现:输入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);
}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: