标签:average-计算文件中double类型数字的平均数
Average-计算文件中double类型数字的平均数:
//Average-计算文件中double类型数字的平均数 #include<iostream> #include<fstream> #include<cstdlib> int main() { using namespace std; ifstream fin; ofstream fout; double tem,sum,aver; int count = 0; fin.open("numbers.dat"); if(fin.fail()) { cout<<"Input file opening failed.\n"; exit(1); } fin>>tem; cout<<"The numbers in the file is "; while(!fin.eof()) { cout<<tem<<" "; sum += tem; count++; fin>>tem; } cout<<endl; //cout<<count<<endl; //cout<<sum<<endl; aver = 1.0*sum/count; cout<<"The average of the numbers is "<<aver<<endl; fin.close(); return 0; }
文件数据:
1.5 2.5 3.5 4.5 5.5
结果:
The numbers in the file is 1.5 2.5 3.5 4.5 5.5 The average of the numbers is 3.5
标签:average-计算文件中double类型数字的平均数
原文地址:http://9320314.blog.51cto.com/9310314/1547708