-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c++
75 lines (70 loc) · 2.24 KB
/
example.c++
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
#include <cstdio>
#include "make_comparable.h++"
int gcd ( int a, int b ) {
return b == 0 ? a : gcd (b, a % b);
}
class RationalStub {
private:
int p, q;
public:
RationalStub ( int _p, int _q) : p (_p), q (_q) {
int d = gcd (p, q);
p /= d, q /= d;
}
// operator '<' is mandatory, all other are made from it
bool operator < ( const RationalStub &r ) const {
fprintf (stderr, "RationalStub::operator <\n");
return p * r.q < r.p * q;
}
// but other you may comment/uncomment in any subset
/*
bool operator <= ( const RationalStub &r ) const {
fprintf (stderr, "RationalStub::operator <=\n");
return p * r.q <= r.p * q;
}
//*/
/*
bool operator > ( const RationalStub &r ) const {
fprintf (stderr, "RationalStub::operator >\n");
return p * r.q > r.p * q;
}
//*/
//*
bool operator >= ( const RationalStub &r ) const {
fprintf (stderr, "RationalStub::operator >=\n");
return p * r.q >= r.p * q;
}
//*/
/*
bool operator == ( const RationalStub &r ) const {
fprintf (stderr, "RationalStub::operator ==\n");
return p * r.q == r.p * q;
}
//*/
//*
bool operator != ( const RationalStub &r ) const {
fprintf (stderr, "RationalStub::operator !=\n");
return p * r.q != r.p * q;
}
//*/
};
// use the magic from make_comparable.h++
typedef make_comparable::MakeComparable <RationalStub> Rational;
int main () {
Rational x = Rational (2, 3);
Rational y = Rational (5, 7);
printf ("is 2/3 < 5/7? %s\n", x < y ? "yes" : "no");
printf ("is 2/3 <= 5/7? %s\n", x <= y ? "yes" : "no");
printf ("is 2/3 > 5/7? %s\n", x > y ? "yes" : "no");
printf ("is 2/3 >= 5/7? %s\n", x >= y ? "yes" : "no");
printf ("is 2/3 == 5/7? %s\n", x == y ? "yes" : "no");
printf ("is 2/3 != 5/7? %s\n", x != y ? "yes" : "no");
printf ("What about vice versa?\n");
printf ("is 5/7 < 2/3? %s\n", y < x ? "yes" : "no");
printf ("is 5/7 <= 2/3? %s\n", y <= x ? "yes" : "no");
printf ("is 5/7 > 2/3? %s\n", y > x ? "yes" : "no");
printf ("is 5/7 >= 2/3? %s\n", y >= x ? "yes" : "no");
printf ("is 5/7 == 2/3? %s\n", y == x ? "yes" : "no");
printf ("is 5/7 != 2/3? %s\n", y != x ? "yes" : "no");
return 0;
}