一度配列のサイズを知らずにテキストファイルから読み込み、平均と分散を計算する必要があります。今、特定の配列を作成できません。その結果、平均と分散を計算できません。皆さんが助けてくれることを本当に願っています。
#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;
}