// (C) 2013-2014, Sergei Zaychenko, KNURE, Kharkiv, Ukraine
#include "arithmetic_progression.hpp"
#include <iostream>
//************** конс **************//
ArithmeticProgression::ArithmeticProgression()
:
m_step(1),
m_initialvalue(0)
{}
//************** 2й **************//
ArithmeticProgression::ArithmeticProgression(double step1, double initialvalue1)
:
m_step(step1),
m_initialvalue(initialvalue1)
{}
//************** 3й для шага ***********//
ArithmeticProgression::ArithmeticProgression(double step1)
:
m_step(step1)
{}
//************** получение элемента А **************//
double ArithmeticProgression::getIthElement(int _i)
{
if (_i < 0)
throw "Invalid index.";
double Element = m_initialvalue;
for (int i = 0; i < _i; i++)
{
Element = Element + m_step;
}
return Element;
}
//************** сумма всех элементов **************//
double ArithmeticProgression::partialSum(int _i, int _k)
{
checkRange(_i, _k);
int partialSum = 0;
for (int i = _i; i < _k; i++)
{
partialSum = partialSum + 1;
}
return partialSum;
}
//************** среднее арифмет **************//
double ArithmeticProgression::partialAverage(int _i, int _k)
{
double Averenge;
double psum = partialSum(_i, _k);
int count;
if (_k == 0)
return 0;
if (_i == 0)
count = _k - _i + 1; //?????
else
count = _k - _i;
Averenge = psum / count;
return Averenge;
}
//************** вывод **************//
void ArithmeticProgression::display(int _i, int _k, std::ostream& disp)
{
checkRange(_i, _k);
for (int i = _i; i <= _k; i++)
{
disp << " " << _i;
}
}
//************** сравнение != **************//
bool ArithmeticProgression::operator != (const ArithmeticProgression & _ArithmeticProgression) const
{
m_initialvalue != _ArithmeticProgression.m_initialvalue;
m_step != _ArithmeticProgression.m_step;
return true;
}
//************** сравенение == **************//
bool ArithmeticProgression::operator == (const ArithmeticProgression & _ArithmeticProgression) const
{
return (*this == _ArithmeticProgression);
}
//************** сравнивает масив чисел с элементами прогрессии до N **************//
bool ArithmeticProgression::matchesArray(const int* _pElements, int _N)
{
double Element = m_initialvalue;
for (int i = 0; i < _N; i++)
{
Element = Element + m_step;
if (Element != _pElements[i]) return false;
}
for (int i = 0; i < _N; i ++)
if (m_i == _pElements[0] && m_k == _pElements[i-1])
return true;
}
//************** инверсия значения шага **************//
ArithmeticProgression ArithmeticProgression::makeInverted()
{
ArithmeticProgression newProgretion = *this;
newProgretion.m_step = newProgretion.m_step * (-1);
return newProgretion;
}
//************** проверка индексов, должны быть + **************//
void checkRange(int _i, int _k)
{
if (_k < _i)
throw "Invalid index range.";
if (_i < 0 || _k < 0)
throw "Invalid index range.";
}
/*
int lol(double initialvalue, double step, double endvalue)
{
double sum1 = 0;
double sredarifm = 0;
int count = 0;
for (int i = initialvalue; i <= endvalue; i = i + step)
{
sum1 = sum1 + i;
count++;
sredarifm = sum1 / count;
std::cout << i << " " << sum1 << " " << sredarifm << std::endl;
}
return 0;
}
*/
/*
int main()
{
ArithmeticProgression Prog1;
double value;
double initialvalue = 0;
double step = 1;
double endvalue = 20;
double sum1 = 0;
double sredarifm = 0;
double count = 0;
std::cout << "enter iditial value!" << std::endl;
std::cin >> initialvalue;
std::cout << "enter iditial step!" << std::endl;
std::cin >> step;
std::cout << "enter iditial endvalue!" << std::endl;
std::cin >> endvalue;
//lol(initialvalue, step, endvalue);
//Prog1.lol(initialvalue, step, endvalue);
//Prog1.display(5, 11, std::cout);
std::ostringstream o1;
ArithmeticProgression p1;
p1.display(13, 15, o1);
std::cout << o1.str() <<std::endl;
std::cin >> value;
//delete Prog1;
//return 0;
};
*/