读入txt文件中的一列数,然后输入最大值、最小值、平均值
#include<stdio.h>
#define INF 10000000
int main()
{
FILE *fin, *fout;
fin = fopen("datain.txt","rb");
fout = fopen("dataout.txt","wb");
int x, n = 0, min = INF , max = -INF ,s = 0;
while(fscanf(fin , "%d", &x) == 1)
{
s += x;
if(x < min) min = x;
if(x > max) max = x;
n++;
}
fprintf(fout, "%d %d %.3f\n", min, max, (double)s/n);
fclose(fin);
fclose(fout);
return 0;
}
#include<stdio.h>
#define INF 10000000
int main()
{
FILE *fin, *fout;
fin = fopen("datain.txt","rb");
fout = fopen("dataout.txt","wb");
int x, n = 0, min = INF , max = -INF ,s = 0;
while(fscanf(fin , "%d", &x) == 1)
{
s += x;
if(x < min) min = x;
if(x > max) max = x;
n++;
}
fprintf(fout, "%d %d %.3f\n", min, max, (double)s/n);
fclose(fin);
fclose(fout);
return 0;
}