-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_3_TMPRatio.cpp
More file actions
78 lines (62 loc) · 1.58 KB
/
10_3_TMPRatio.cpp
File metadata and controls
78 lines (62 loc) · 1.58 KB
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
#include <iostream>
template<int X, int Y>
struct GCD
{
static const int value = GCD<Y, X% Y>::value;
};
template<int X>
struct GCD<X, 0>
{
static const int value = X;
};
template<int N, int D = 1>
struct Ratio
{
private:
const static int _gcd = GCD<N, D>::value;
public:
//typedef Ratio<N / _gcd, D / _gcd> type;
using type = Ratio<N / _gcd, D / _gcd>;
static const int num = N / _gcd;
static const int den = D / _gcd;
};
template<class R1, class R2>
struct _Ratio_add
{
using type = Ratio<R1::num* R2::den + R2::num * R1::den, R1::den * R2::den>;
};
template<class R1, class R2>
struct Ratio_add : _Ratio_add<R1, R2>::type {};
template<class R1, class R2>
struct _Ratio_sub
{
using type = Ratio<R1::num* R2::den - R2::num * R1::den, R1::den * R2::den>;
};
template<class R1, class R2>
struct Ratio_sub : _Ratio_sub<R1, R2>::type {};
template<class R1, class R2>
struct _Ratio_mul
{
using type = Ratio<R1::num* R2::num, R1::den * R2::den>;
};
template<class R1, class R2>
struct Ratio_mul : _Ratio_mul<R1, R2>::type {};
template<class R1, class R2>
struct _Ratio_div
{
using type = Ratio<R1::num* R2::den, R1::den * R2::num>;
};
template<class R1, class R2>
struct Ratio_div : _Ratio_div<R1, R2>::type {};
int main(void)
{
/*typedef Ratio<2, 3> rat;
typedef Ratio<3, 2> rat2;
typedef Ratio_add<rat, rat2> rat3;*/
using rat = Ratio<2, 3>;
using rat2 = Ratio<3, 2>;
using rat3 = Ratio_add<rat, rat2>;
std::cout << "2/3 + 3/2 = " << rat3::num << "/" << rat3::den << "\n";
using rat4 = Ratio_mul<rat3, rat>;
std::cout << "13/6 * 2/3 = " << rat4::num << "/" << rat4::den << "\n";
}