- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
int main()
{
//выводит на экран среднее арифметическое чисел 1, 3, 5
std::cout << Mean + 1 + 3 + 5 << std::endl;
//выводит на экран среднее геометрическое чисел 2, 4, 8
std::cout << Mean * 2 * 4 * 8 << std::endl;
}
//реализация
class CMean
{
mutable double out;
mutable size_t cnt;
mutable size_t type;
public:
CMean(): out(0), cnt(0), type(-1) {}
CMean& operator + (double n)
{
return type = 0, out+= n, ++cnt, *this;
}
CMean& operator * (double n)
{
return (type == (size_t)-1 ? type = 1, out = 1 : 0), out*= n, ++cnt, *this;
}
size_t reset() const {return type = -1, out = cnt = 0;};
friend std::ostream& operator << (std::ostream&, const CMean&);
} Mean;
std::ostream& operator << (std::ostream& _os, const CMean& _arith)
{
return _os << (!_arith.type ? _arith.out / _arith.cnt : std::pow(_arith.out, 1.0 / _arith.cnt)) + _arith.reset();
}