I rewrote the code for it. Please tell me how it is:
Numbers.h:Numbers.cpp:Code:#include <fstream>
#include <vector>
#include <iostream>
#include <numeric>
#include <algorithm>
#include <string>
using namespace std;
class Numbers{
public:
Numbers(string); //Definitions are placed here to be inlined
__int64 Sum() {return accumulate(_b, _e, 0);}
double Ave() {return static_cast<double> (Sum()/numCount);}
int Mid() {return (nums[numCount/2]);}
void PrintNums() {for each (int val in nums) cout << val << endl;}
int Min(){return *min_element(_b, _e);}
int Max(){return *max_element(_b, _e);}
int Mod();
int Count() {return numCount;}
private:
vector<int>::iterator _b,_e;
unsigned int numCount;
void ReadFile(string &fileName);
string fileName;
vector <int> nums;
};
Code:
#include "Numbers.h"
Numbers::Numbers(string fileName)
{
ReadFile(fileName);
//So we won't need to call these functions next times
numCount = static_cast <unsigned int> (nums.size()); _b = nums.begin(); _e = nums.end();
sort(_b, _e); //We need to sort to find the middle (Am I a poet?)
}
void Numbers::ReadFile(string &fileName)
{
cout << "Openning \""<< fileName <<"\"." << endl;
ifstream file(fileName.c_str(), ios::in);
if(file.is_open())
{
int buf;
cout << "Reading numbers." << endl;
while(file >> buf) nums.push_back(buf);
if(!file.eof()) cerr << "WARNING: File was not read to end!" << endl;
file.close();
}
else cerr << "ERROR: File could not be opened!" << endl;
}
int Numbers::Mod()
{
unsigned int countN = 0;
int mod = 0;
unsigned int buff;
for each(int val in nums)
{
buff = static_cast<unsigned int> (count(nums.begin(), nums.end(), val));
if(buff > countN){ countN = buff; mod = val;}
}
return mod;
}