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

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

#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 

*/

我看了你的程序,给你改了两处(见注释),完整的程序如下:

#include<stdio.h>

#include<stdlib.h>

#define N 4/*学生人数*/

#define M 5/*课程数*/

struct student

{

int id

char name[20]

float mark[M]/*每门课的成绩*/

float sum[4]/*总分 平均成绩 最高分 最低分*/

}

typedef struct student STD

int main()

{

STD st[N]

int i,h,j,k

FILE *fp,*fp2

if((fp=fopen("E:\\11.txt","r"))==NULL)

{

printf("无法打开该文件!\n")

exit(0)

}

for(i=0i<Ni++)

{

fscanf(fp,"%d ",&st[i].id)

fscanf(fp,"%c",&st[i].name[0])

h=0

while (st[i].name[h]!=' ')

{

h++

fscanf(fp,"%c",&st[i].name[h])

}

st[i].name[++h]='\0'//这里加了一句,以添加字符串尾部。

for (j=0j<Mj++)

fscanf(fp,"%f",&st[i].mark[j])//这里把fscanf(fp,"%lf",&st[i].mark[j])改成了fscanf(fp,"%f",&st[i].mark[j])即%lf改成%f

}

for(i=0i<Ni++)

{

printf("%-3d%-5s",st[i].id,st[i].name)

for(j=0j<Mj++)

printf(" %.2f",st[i].mark[j])

printf("\n")

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/11844220.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存