#include <iostream>
#include <sstream>
#include <cassert>
#include <cstring>
#include <fstream>
using namespace std;
int main()
{
ifstream read;
string file="E:\\test.txt";//文件路径
read.open(file.data());
assert(read.is_open());//如果打开文件失败,则输出错误消息,并终止运行
read >> noskipws;//设置可以读取空白符
string str1,str2;
char c;
int cou=0;//计数
double sum=0, temp;
while(!read.eof())
{
read >> c;
if(c ==' '||c=='\n'||read.eof())
{
//字符串转换为数字
stringstream s;
s.str(str1);
s >> temp;
//求和
sum+=temp;
//设为0,防止文件末尾出现空行
temp = 0;
str1="";
}
else
{
cou++;//统计数字个数
//将字符转换为字符串
stringstream ss;
ss << c;
str2=ss.str();
str1+=str2;
}
}
cout<<"实数个数为:";
cout<<cou<<endl;
cout<<"平均值为:";
cout<<sum/cou<<endl;
read.close();
}