微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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;
} 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐