首页 > 编程语言 > 详细

c语言中输出文件的行数、每一列的总和、每一列的平均值

时间:2021-06-07 00:42:24      阅读:25      评论:0      收藏:0      [点我收藏+]

1、

#include <stdio.h>

int main(void)
{
    FILE *fp;
    
    int lines = 0;
    double c1, c2, c3;
    double c1sum = 0, c2sum = 0, c3sum = 0;
    
    if((fp = fopen("a.txt","r")) == NULL)
        printf("\afile does not exist!\n");
    else
    {
        while(fscanf(fp, "%lf%lf%lf", &c1, &c2, &c3) == 3)
        {
            printf("%8.2f%8.2f%8.2f\n", c1, c2, c3);
            lines++;
            c1sum += c1;
            c2sum += c2;
            c3sum += c3;
        }
        puts("\n======================\n");
        printf("the total lines: %d\n", lines);
        puts("the sum of every column are as below.");
        printf("%8.2f%8.2f%8.2f\n\n", c1sum, c2sum, c3sum);
        puts("the average are as below.");
        printf("%8.2f%8.2f%8.2f\n", c1sum/lines, c2sum/lines, c3sum/lines); 
        fclose(fp);
    }
    return 0;
} 

技术分享图片

 

c语言中输出文件的行数、每一列的总和、每一列的平均值

原文:https://www.cnblogs.com/liujiaxin2018/p/14856705.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!