-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfraction.cpp
232 lines (194 loc) · 4.22 KB
/
fraction.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "fraction.h"
Fraction toFraction(int num)
{
Fraction frac(num);
return frac;
}
Fraction::Fraction(int numerator, int denominator)
{
if (denominator == 0)
{
throw "Error: Denominator can not be zero.";
}
this -> numerator = numerator;
this -> denominator = denominator;
}
Fraction::Fraction(int num)
{
this -> numerator = num;
this -> denominator = 1;
}
void Fraction::irregularFraction()
{
if ((this -> numerator) < 0 && (this -> denominator) < 0)
{
this -> numerator = -(this -> numerator);
this -> denominator = -(this -> denominator);
}
const int g = math::gcd(std::abs(this -> numerator),
std::abs(this -> denominator));
(this -> numerator) /= g;
(this -> denominator) /= g;
return ;
}
void Fraction::operator=(const Fraction & frac)
{
this -> numerator = frac.numerator;
this -> denominator = frac.denominator;
return ;
}
void Fraction::operator=(const int num)
{
this -> numerator = num;
this -> denominator = 1;
return ;
}
bool Fraction::operator==(const Fraction & frac)
{
this -> irregularFraction();
Fraction f = frac;
f.irregularFraction();
if ((this -> numerator) == f.numerator &&
(this -> denominator) == f.denominator)
{
return true;
}
return false;
}
bool Fraction::operator!=(const Fraction & frac)
{
return !((*this) == frac);
}
Fraction Fraction::operator-() const
{
Fraction result(-(this -> numerator), denominator);
return result;
}
Fraction Fraction::inverseFraction() const
{
if ((this -> numerator) == 0)
{
throw "Error: The numerator of this fraction is zero";
}
Fraction result(this -> denominator, this -> numerator);
return result;
}
Fraction Fraction::operator+(const Fraction & frac)
{
const int d = math::lcm(std::abs(this -> denominator), std::abs(frac.denominator));
const int n = (this -> numerator) * (d / (this -> denominator))
+ frac.numerator * (d / frac.denominator);
Fraction result(n, d);
return result;
}
Fraction Fraction::operator-(const Fraction & frac)
{
return (*this) + (-frac);
}
Fraction Fraction::operator*(const Fraction & frac)
{
Fraction result((this -> numerator) * frac.numerator,
(this -> denominator) * frac.denominator);
result.irregularFraction();
return result;
}
Fraction Fraction::operator/(const Fraction & frac)
{
if (frac.numerator == 0)
{
throw "Error: Can not divide by zero";
}
return (*this) * (frac.inverseFraction());
}
Fraction Fraction::operator+(const int num)
{
return (*this) + toFraction(num);
}
Fraction Fraction::operator-(const int num)
{
return (*this) - toFraction(num);
}
Fraction Fraction::operator*(const int num)
{
return (*this) * toFraction(num);
}
Fraction Fraction::operator/(const int num)
{
return (*this) / toFraction(num);
}
Fraction operator+(const int num, Fraction & frac)
{
return frac + num;
}
Fraction operator-(const int num, Fraction & frac)
{
return (-frac) + num;
}
Fraction operator*(const int num, Fraction & frac)
{
return frac * num;
}
Fraction operator/(const int num, Fraction & frac)
{
return frac.inverseFraction() * num;
}
int Fraction::getNumerator() const
{
return (this -> numerator);
}
int Fraction::getDenominator() const
{
return (this -> denominator);
}
bool Fraction::operator<(const Fraction & frac)
{
Fraction result = (*this) - frac;
return (result.getNumerator()
* result.getDenominator() < 0);
}
bool Fraction::operator>(const Fraction & frac)
{
Fraction result = (*this) - frac;
return (result.getNumerator()
* result.getDenominator() > 0);
}
bool Fraction::operator<=(const Fraction & frac)
{
return !((*this) > frac);
}
bool Fraction::operator>=(const Fraction & frac)
{
return !((*this) < frac);
}
bool Fraction::operator>(const int num)
{
return (*this) > toFraction(num);
}
bool Fraction::operator<(const int num)
{
return (*this) < toFraction(num);
}
bool Fraction::operator>=(const int num)
{
return !((*this) < num);
}
bool Fraction::operator<=(const int num)
{
return !((*this) > num);
}
bool operator>(const int num, Fraction & frac)
{
return toFraction(num) > frac;
}
bool operator<(const int num, Fraction & frac)
{
return toFraction(num) < frac;
}
bool operator>=(const int num, Fraction & frac)
{
return !(num < frac);
}
bool operator<=(const int num, Fraction & frac)
{
return !(num > frac);
}