OK, I changed them this way:
Header:
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();
size_t Count() {return numCount;}
private:
vector<int>::iterator _b,_e;
size_t numCount;
void ReadFile(string &fileName);
string fileName;
vector <int> nums;
};
cpp:
Code:
#include "stdafx.h"
#include "Numbers.h"
Numbers::Numbers(string fileName)
{
ReadFile(fileName);
//So we won't need to call these functions next times
numCount = 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()
{
int mod = 0;
__w64 int buff, countN = 0;
for each(int val in nums)
{
buff = count(nums.begin(), nums.end(), val);
if(buff > countN){ countN = buff; mod = val;}
}
return mod;
}
And the runner:
Code:
#include "Numbers.h"
int main(int argc, char* argv[])
{
string fName;
if(argc>1) fName = argv[1];
else fName = "nume.txt";
Numbers nums(fName);
if(nums.Count()){
nums.PrintNums();
cout << "-------------------------------------------------------------------" << endl;
cout << "Count: " << nums.Count() << endl;
cout << "Sum: " << nums.Sum() << endl;
cout << "Average: " << cout.precision(10) << nums.Ave() << endl;
cout << "Mid: " << nums.Mid() << endl;
cout << "Mod: " << nums.Mod() << endl;
cout << "Max: " << nums.Max() << endl;
cout << "Min: " << nums.Min() << endl;
}
cout << "\nPress a key to exit..." << endl;
_getch();
return 0;
}