Newer
Older
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
#include <iostream>
#include <cmath> // for std::pow
template<typename Data> struct sum{
Data operator()(const Data & a, const Data & b ){
return a+b;
}
};
template<typename Data> struct multiply{
Data operator()(const Data & a, const Data & b ){
return a*b;
}
};
template<typename Result> struct divide{
Result operator()(const Result & a, const Result & b ){
return a/b;
}
};
template<typename Result> struct nroot{
Result operator()(const Result & a, const Result & b ){
return std::pow(a,1.0/b);
}
};
template<typename Data, typename Result, typename IncrOp, typename ResOp, int InitPar=0 > class mean{
public:
// in C++98 typedef Data data_type; typedef Result result_type;
using data_type=Data;
using result_type=Result;
using incrop_type=IncrOp;
using resop_type=ResOp;
mean(): ncount_{}, rvalue_{static_cast<data_type>(InitPar)}, mean_{} {};
void increment(Data d){
ncount_++;
rvalue_=increment_(rvalue_,d);
result_type rtemp=static_cast<result_type>(rvalue_);
result_type ntemp=static_cast<result_type>(ncount_);
mean_=result_(rtemp,ntemp);
}
result_type get_ave() const { return mean_;}
private:
IncrOp increment_;
ResOp result_;
Data ncount_;
Data rvalue_;
Result mean_;
};
int main(){
// Since C++11
using data_type=int;
using result_type=double;
using sum_type=sum<data_type>;
using multiply_type=multiply<data_type>;
using divide_type=divide<result_type>;
using nroot_type=nroot<result_type>;
using arithmetic_type=mean<data_type,result_type,sum_type,divide_type>;
using geometrical_type=mean<data_type,result_type,multiply_type,nroot_type,1>;
arithmetic_type a;
geometrical_type b;
data_type indata{};
std::cout <<"Enter values [Ctrl-D to finish]: " <<std::endl;
while(std::cin>>indata){
a.increment(indata);
b.increment(indata);
}
std::cout << " Arithmetic mean: " << a.get_ave() << "\n Geometrical mean: " << b.get_ave() << std::endl;
return 0;
}