C语言题目,txt文件读取成绩,并求平均分

在一个txt文件里存储有20个同学的3科成绩,计算各科成绩的最高分和平均分。

#include <stdio.h>
int main(void)
{
    //把data.txt放在同一个文件夹里面
    freopen("data.txt", "r", stdin);
    int i, j;
    double score[3] = {0.0}; //三科最高分
    double avg[3] = {0.0}; //三科均分

    for (i = 0; i < 20; i++)
    {
        for (j = 0; j < 3; j++)
        {
            double index;
            scanf("%lf", &index);
            if (index > score[j])
                score[j] = index;
            avg[j] += index;
        }
    }
    for (j = 0; j < 3; j++)
        avg[j] /= 20;

    for (j = 0; j < 3; j++)
    {
        printf("第%d科, 最高分: %.2f\t 平均分: %.2f\n", j, score[j], avg[j]);
    }
    return 0;
}

//数据:
/*
22.00 97.00 15.00 
82.00 42.00 12.00 
80.00 35.00 25.00 
35.00 4.00 16.00 
17.00 26.00 64.00 
35.00 87.00 63.00 
9.00 18.00 86.00 
24.00 89.00 8.00 
47.00 41.00 4.00 
99.00 83.00 16.00 
57.00 5.00 65.00 
73.00 39.00 59.00 
37.00 20.00 47.00 
14.00 55.00 3.00 
83.00 24.00 29.00 
99.00 60.00 69.00 
62.00 21.00 87.00 
0.00 97.00 28.00 
9.00 44.00 69.00 
13.00 95.00 52.00 
*/

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-09
struct stu                                                      // 结构体定义变量 
{
char  num[20];
char name[10];
float score[3];

}Stu;
a[3]

void output(struct stu * pstu)
{
FILE *fp;
if ((fp = fopen("score.dat", "r")) == NULL)
{
printf("\n文件打开错误!\n");
return;
}
fread(pstu, sizeof(struct student), M, fp);
fclose(fp);
return;

for (i = 0; i

{
stu[i].score[3] = 0;
for (j = 0; j<3; j++)
stu[i].score[3] += stu[i].score[j];
stu[i].score[3 + 1] = stu[i].score[3] / 3;
}
for (j = 0; j<N + 2; j++)
{
s = 0;
for (i = 0; i<M; i++)
s += stu[i].score[j];
stu[M].score[j] = s / M;
}

第2个回答  2014-12-09
c语言读文件能做啊 我给你写
相似回答