-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist4802.cpp
78 lines (64 loc) · 1.36 KB
/
list4802.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
#include <iostream>
using namespace std;
template<class T>
class rational
{
public:
typedef T value_type;
rational() : rational{0} {}
rational(T num) : numerator_{num}, denominator_{1}{}
rational(T num, T den);
void assign(T num, T den);
template<class U>
U convert()
const
{
return static_cast<U>(numerator()) / static_cast<U>(denominator()) ;
}
T numerator() const { return numerator_;}
T denominator() const {return denominator_;}
private:
void reduce();
T numerator_;
T denominator_;
};
template<class T>
rational<T>::rational (T num, T den)
: numerator_{num}, denominator_{den}
{
rational<T>::reduce();
}
template<class T>
void rational<T>::assign(T num, T den)
{
numerator_ = num;
denominator_ = den;
rational<T>::reduce();
}
template<class T>
void rational<T>::reduce(){}
template<class T>
bool operator==(rational<T> const& a, rational<T> const& b)
{
return a.numerator() == b.numerator() and
a.denominator() == b.denominator();
}
template<class T>
inline bool operator!=(rational<T> const& a, rational<T> const& b)
{
return not (a==b);
}
int main()
{
rational<short> zero{};
rational<int> pi1{355,113}, x{1,2}, y{2,4};
rational<long> pi2 {80143857L, 25510582L};
if(x==y)
{
cout << "1/2 == 2/4" << endl;
}
else
{
cout << "1/2 != 2/4" << endl;
}
}