p281

源程序:

#include <iostream>

using namespace std;

int main()

{

int x, count, sum = 0;

freopen("c:\\input.txt", "r", stdin);

for (count = 0; count<10; count++)

{

cin >> x;

if (cin.eof())

break;

else sum += x;

}

cout << "前" << count << "个整数的平均值=" << 1.0*sum / count << endl;

system("pause");

return 0;

}

运行结果:

在input.txt中输入10个整数,读10个整数读入文件中,依次相加,直到文件结束停止读入,再求平均值

posted @ 2020-01-27 12:44  bobo哥  阅读(98)  评论(0编辑  收藏  举报