-
Notifications
You must be signed in to change notification settings - Fork 0
/
Averager.h
55 lines (49 loc) · 1.22 KB
/
Averager.h
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
/*
*
* Averager - усреднитель
*
*/
#pragma once
template<uint16_t total>
class Averager
{
public:
// Constructors
Averager() {}
// Copy constructor
Averager( const Averager& ) = delete;
// Move constructor
Averager ( Averager && ) = delete;
// Добавляет новый элемент
void add(uint16_t val)
{
if(total == _count) {
// Когда массив заполнен полностью
// Сдвигаем все влево
for(uint8_t i = 0; i < total-1; i++) {
_arr[i] = _arr[i+1];
}
_arr[total-1] = val;
} else {
// На стадии заполнения
_arr[_count] = val;
_count++;
}
}
// Возвращает среднее
uint16_t get() const
{
if (0 == _count) {
return 0;
}
uint32_t sum = 0;
for(uint8_t i = 0; i < _count; i++) {
sum +=_arr[i];
}
// Возвращаем округленное до целого среднее
return (uint16_t)(0.5 + 1.0*sum/_count);
}
private:
uint8_t _count =0;
uint16_t _arr[total];
};