arithmetic_progression.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// (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;
};
*/