blob: ea0e3a1fe72f8d9c5ee28e55d079e5e7981a54df (
plain) (
blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#include <iostream>
#include "Rational.h"
bool Rational::gt(const Rational& other) const {
// x a x*b a*y
// - ? - <=> --- ? --- <=> x*b ? a*y
// y b y*b y*b
return numerator * other.denumerator > other.numerator * denumerator;
}
bool Rational::lt(const Rational& other) const {
// x a x*b a*y
// - ? - <=> --- ? --- <=> x*b ? a*y
// y b y*b y*b
return numerator * other.denumerator < other.numerator * denumerator;
}
int Rational::gcd() const {
int a = numerator, b = denumerator;
if (a < b) {
int temp = a;
a = b;
b = temp;
}
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
return a;
}
Rational::Rational() : Rational(1, 1)
{}
Rational::Rational(int numerator, int denumerator) {
if (denumerator == 0) numerator = denumerator = 1;
set_numerator(numerator);
set_denumerator(denumerator);
}
int Rational::get_numerator() const {
return numerator;
}
void Rational::set_numerator(int numerator) {
this->numerator = numerator;
}
int Rational::get_denumerator() const {
return denumerator;
}
void Rational::set_denumerator(int denumerator) {
if (denumerator == 0) denumerator = 1;
this->denumerator = denumerator;
}
void Rational::canonize() {
int gcdND = abs(gcd());
set_numerator(numerator / gcdND);
set_denumerator(denumerator / gcdND);
}
// Not needed, kept for fun
void Rational::canonizeRec() {
if (denumerator == 1)
return;
int whole = numerator / denumerator, leftover = numerator % denumerator;
if (leftover == 0) {
numerator = whole;
denumerator = 1;
return;
}
Rational temp(denumerator, leftover);
temp.canonize();
numerator = whole * temp.numerator + temp.denumerator;
denumerator = temp.numerator;
}
Rational Rational::operator+(const Rational& other) const {
// x a x*b + a*y
// - + - = ---------
// y b y*b
return Rational(numerator * other.denumerator + other.numerator * denumerator, denumerator * other.denumerator);
}
Rational Rational::operator-(const Rational& other) const {
// x a x*b - a*y
// - - - = ---------
// y b y*b
return Rational(numerator * other.denumerator - other.numerator * denumerator, denumerator * other.denumerator);
}
Rational Rational::operator*(const Rational& other) const {
// x a x*b
// - / - <=> ---
// y b y*a
return Rational(numerator * other.numerator, denumerator * other.denumerator);
}
Rational Rational::operator/(const Rational& other) const {
// x a x b
// - / - <=> - * -
// y b y a
return Rational(numerator * other.denumerator, denumerator * other.numerator);
}
int Rational::numcmp(const Rational& arg) const {
return gt(arg) - lt(arg);
}
void Rational::print() const {
std::cout << numerator << "/" << denumerator;
}
|