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

时间:2013-10-04 16:56:51

标签: c++ arrays overflow sstream

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

#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;


}

1 个答案:

答案 0 :(得分:1)

我担心这看起来像家庭作业,我觉得不得不提供一个方向的想法,而不是一个完整的解决方案来做你要求的一切。我希望这足以让你开始:

#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计算平均值的每个平方差值。