从练习2中的data.txt文件读取各个整数,打印出最大值和最小值,以及平均值
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
int max,min,sum=0;
int num;
int n = 0;
ifstream stream;
stream.open("data.txt");
if (!stream.is_open())
{
cout << "文件打开失败" << endl;
exit(1);
}
stream >> num;
max = num;
min = num;
sum += num;
n++;
while (1)
{
stream >> num;
if (stream.eof())
{
break;
}
n++;
sum += num;
if (num>max)
{
max = num;
}
else if(num<min)
{
min = num;
}
}
cout << "max" << max << endl;
cout << "min" << min << endl;
cout << "average" << sum / n << endl;
stream.close();
system("pause");
return 0;
}