gpt4 book ai didi

c++ - 关于sstream,使用变量作为数组大小和计算溢出

转载 作者:行者123 更新时间:2023-11-28 07:22:54 25 4
gpt4 key购买 nike

需要在不知道数组大小的情况下从文本文件中读取一次,然后计算均值和方差。现在我无法创建特定的数组。结果,我无法计算均值和方差。我真的希望你们能提供帮助。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <math.h>
#include "fraction.h"

using namespace std;

void ascending(fraction fractionarr[]);
fraction mean(fraction fractionarr[]);
fraction variance(fraction fractionarr[]);
static const int countlinenumber(ifstream& data);


void ascending(fraction fractionarr[])
{
ifstream data("data1.txt");
fraction temp;
fraction test(0, 1);
int n = 0;
int linenumber = countlinenumber(data);

for (int i = 0; i < 100; i++)
{
if(fractionarr[i] != test)
{
n++;
}
}
for (int i = 0; i < linenumber; i++)
{
for(int j = 0; j < linenumber-1; j++)
{
if(fractionarr[i+1] != test)
{
if(fractionarr[j] > fractionarr[j+1])
{
temp = fractionarr[j];
fractionarr[j] = fractionarr[j+1];
fractionarr[j+1] = temp;
}
}
}
}
}

fraction mean(fraction fractionarr[])
{
ifstream data("data1.txt");
fraction sum = fractionarr[0];
fraction average;
fraction n(1,1);
fraction test(0,1);
int linenumber = countlinenumber(data);

for (int i = 1; i < linenumber; i++)
{
if(fractionarr[i] != test)
{
sum = sum + fractionarr[i];
n = n + fraction(1,1);
}
}

average = sum / n;
return average;
}

fraction variance(fraction fractionarr[])
{
ifstream data("data1.txt");
fraction average = mean(fractionarr);
fraction* semi = new fraction[100];
fraction total;
fraction t(1, 1);
fraction variance;
fraction test(0,1);
int linenumber = countlinenumber(data);

for (int i = 0; i < linenumber; i++)
{
if(fractionarr[i] != test)
{
semi[i] = (fractionarr[i]-average) * (fractionarr[i]-average);
t = t + fraction(1, 1);
}
}
total = semi[0];
for (int i = 1; i < 100; i++)
{
if(semi[i] != test)
{
total = total + semi[i];
}
}
variance = total / t;
return variance;
}

static const int countlinenumber(ifstream& data)
{
string temp;
int linenumber = 0;

while(getline(data, temp) && !data.eof())
{
linenumber++;
}
return linenumber;
}

int main()
{
ifstream data("data1.txt");
ofstream data_out("data1_out.txt");
stringstream ss;

string temp;
char op;
fraction testing(0,1);
fraction average;
static const int linenumber = countlinenumber(data);

int* numerator = new int[linenumber];
int* denominator = new int[linenumber];
fraction* fractionarr = new fraction[linenumber];

for(int i = 0; i < linenumber; i++)
{
numerator[i] = 0;
denominator[i] = 0;
}

for(int i = 0; i < linenumber; i++)
{
if(!data.eof())
{
getline(data, temp, '\n');
ss << temp;
ss >> numerator[i] >> op >> denominator[i];
ss.clear();
}
}
delete [] numerator;
delete [] denominator;

ascending(fractionarr);
for (int i = 0; i < linenumber; i++)
{
if(fractionarr[i] != testing)
data_out << fractionarr[i] << endl;
}
data_out.close();

cout << mean(fractionarr) << endl;
average = mean(fractionarr);
cout << variance(fractionarr) << endl;

system("pause");
return 0;


}

最佳答案

恐怕这看起来很像家庭作业,我觉得有必要只提供一个方向的想法,而不是一个完整的解决方案来完成你所要求的一切。不过,我希望这足以让您入门:

#include <sstream>
#include <vector>
#include <iterator>
#include <numeric>

int main(){
std::istringstream input("1 200 50 9.5 300.1");

std::vector<double> numbers{std::istream_iterator<double>(input),
std::istream_iterator<double>()};

double sum = std::accumulate(numbers.begin(), numbers.end(), 0.0);

std::cout << "Sum: " << sum << "\n";
std::cout << "Count: " << numbers.size() << "\n";
}

这不会计算均值或方差,但根据此处的内容,我认为添加均值计算应该是微不足道的,而方差只是稍微困难一点(注意:对于方差,您需要std::accumulate 的版本,它使用 lambda 计算与平均值的每个平方差)。

关于c++ - 关于sstream,使用变量作为数组大小和计算溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19186800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com