- 编写一段程序,从键盘任意输入十个浮点数并存入二进制文件binary.dat中;从此二进制文件中读取该数据并计算其总和和平均值。
不知道哪里有问题,输出一直不对😭😭
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
void main()
{
ofstream outfile("binary.dat", ios::binary);
if (!outfile.is_open())
{
cout << "打开失败" << endl;
exit(0);
}
double c[10], a[10], sum = 0, ave;
cout << "输入十个数"<<endl;
for (int i = 0; i <= 9; i++)
{
cin >> c[i];
outfile << c[i];
}
outfile.close();
ifstream infile("binary.dat", ios::binary);
if (!infile)
{
cout << "打开错误";
exit(1);
}
for (int i = 0; i <= 9; i++)
{
infile >> a[i];
sum += a[i];
}
ave = sum / 10;
cout << "总和为:" << sum << " " << "平均值为:" << ave << endl;
}