-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRationalNumber.cpp
More file actions
388 lines (325 loc) · 11.2 KB
/
RationalNumber.cpp
File metadata and controls
388 lines (325 loc) · 11.2 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include<algorithm>
#include<string>
#include "RationalNumber.h" // class implemented
using namespace std;
// File scope starts here
/////////////////////////////// PUBLIC ///////////////////////////////////////
//============================= LIFECYCLE ====================================
// RationalNumber Default + Overloaded Constructor
RationalNumber::RationalNumber(int aNumerator, int aDenominator) {
this->SetRationalNumber(aNumerator, aDenominator);
}
// end RationalNumber constructor
// Float Conversion Constructor
RationalNumber::RationalNumber(float aFloatNumber) {
int count = std::to_string(aFloatNumber).substr(std::to_string(aFloatNumber).find('.') + 1).length();
this->SetRationalNumber(int(aFloatNumber * (float)pow(10, count)), pow(10, count));
this->ReducedForm();
}
// end RationalNumber constructor
//============================= OPERATORS ====================================
// Stream Insertion
ostream& operator <<(ostream& rOs, const RationalNumber& rFrom) {
if (rFrom.GetDenominator() == 1)
rOs << rFrom.GetNumerator();
else
rOs << rFrom.GetNumerator() << " / " << rFrom.GetDenominator();
return rOs;
}
// end stream insertion
// Stream Extraction
istream& operator >>(istream& rIs, RationalNumber& rTo) {
int aNumerator, aDenominator;
cout << "Enter value for numerator: ";
rIs >> aNumerator;
cout << "Enter value for denominator: ";
rIs >> aDenominator;
rTo.SetRationalNumber(aNumerator, aDenominator);
rTo.ReducedForm();
return rIs;
}
// end stream extraction
// Addition operator
RationalNumber RationalNumber::operator +(const RationalNumber& rhs) {
RationalNumber t = *this;
return t += rhs;
}
// end Addition operator
// Overloaded addition operator
RationalNumber RationalNumber::operator +(const int rhs) {
RationalNumber t = *this;
return t += rhs;
}
// end Addition operator
// Friend function to overloaded addition operator
RationalNumber operator +(const int lhs, const RationalNumber& rhs) {
RationalNumber t = rhs;
return t += lhs;
}
// end Addition operator
// Addition assignment operator
RationalNumber& RationalNumber::operator +=(const RationalNumber& rhs) {
this->SetNumerator((this->GetNumerator() * rhs.GetDenominator()) + (rhs.GetNumerator() * this->GetDenominator()));
this->SetDenominator(this->GetDenominator() * rhs.GetDenominator());
this->ReducedForm();
return *this;
}
// end addition assignment operator
// Overloaded Addition assignment operator
RationalNumber & RationalNumber::operator +=(const int rhs) {
this->SetNumerator(this->GetNumerator() + (rhs * this->GetDenominator()));
this->ReducedForm();
return *this;
}
// end addition assignment operator
// Subtraction operator
RationalNumber RationalNumber::operator -(const RationalNumber& rhs) {
RationalNumber t = *this;
return t -= rhs;
}
// end Subtraction operator
// Overloaded subtraction operator
RationalNumber RationalNumber::operator -(const int rhs) {
RationalNumber t = *this;
return t -= rhs;
}
// end subtraction operator
// Friend function to overloaded subtraction operator
RationalNumber operator -(const int lhs, const RationalNumber& rhs) {
RationalNumber t = rhs;
return t -= lhs;
}
// end subtraction operator
// Subtraction assignment operator
RationalNumber& RationalNumber::operator -=(const RationalNumber& rhs) {
this->SetNumerator((this->GetNumerator() * rhs.GetDenominator()) - (rhs.GetNumerator() * this->GetDenominator()));
this->SetDenominator(this->GetDenominator() * rhs.GetDenominator());
this->ReducedForm();
return *this;
}
// end subtraction assignment operator
// Overloaded subtraction assignment operator
RationalNumber & RationalNumber::operator -=(const int rhs) {
this->SetNumerator(this->GetNumerator() - (rhs * this->GetDenominator()));
this->ReducedForm();
return *this;
}
// end subtraction assignment operator
// Multiplication operator
RationalNumber RationalNumber::operator *(const RationalNumber& rhs) {
RationalNumber t = *this;
return t *= rhs;
}
// end multiplication operator
// Overloaded multiplication operator
RationalNumber RationalNumber::operator *(const int rhs) {
RationalNumber t = *this;
return t *= rhs;
}
// end multiplication operator
// Friend function to overloaded multiplication operator
RationalNumber operator *(const int lhs, const RationalNumber& rhs) {
RationalNumber t = rhs;
return t *= lhs;
}
// end multiplication operator
// Multiplication assignment operator
RationalNumber& RationalNumber::operator *=(const RationalNumber& rhs) {
this->SetNumerator(this->GetNumerator()*rhs.GetNumerator());
this->SetDenominator(this->GetDenominator()*rhs.GetDenominator());
this->ReducedForm();
return *this;
}
// end multiplication assignment operator
// Overloaded multiplication assignment operator
RationalNumber & RationalNumber::operator *=(const int rhs) {
this->SetNumerator(this->GetNumerator()*rhs);
this->ReducedForm();
return *this;
}
// end multiplication assignment operator
// Division operator
RationalNumber RationalNumber::operator /(const RationalNumber& rhs) {
RationalNumber t = *this;
return t /= rhs;
}
// end division operator
// Overloaded division operator
RationalNumber RationalNumber::operator /(const int rhs) {
RationalNumber t = *this;
return t /= rhs;
}
// end division operator
// Friend function to overloaded division operator
RationalNumber operator /(const int lhs, const RationalNumber& rhs) {
RationalNumber t = rhs;
return t /= lhs;
}
// end division operator
// Division assignment operator
RationalNumber& RationalNumber::operator /=(const RationalNumber& rhs) {
this->SetNumerator(this->GetNumerator()*rhs.GetDenominator());
this->SetDenominator(this->GetDenominator()*rhs.GetNumerator());
this->ReducedForm();
return *this;
}
// end division assignment operator
// Overloaded division assignment operator
RationalNumber & RationalNumber::operator /=(const int rhs) {
this->SetDenominator(this->GetDenominator()*rhs);
this->ReducedForm();
return *this;
}
// end division assignment operator
// Equality operator
bool RationalNumber::operator ==(const RationalNumber& rhs) {
if (((float)this->GetNumerator() / (float)this->GetDenominator()) == ((float)rhs.GetNumerator() / (float)rhs.GetDenominator()))
return true;
else
return false;
}
// end equality operator
// Non-Equality operator
bool RationalNumber::operator !=(const RationalNumber & rhs) {
if (((float)this->GetNumerator() / (float)this->GetDenominator()) != ((float)rhs.GetNumerator() / (float)rhs.GetDenominator()))
return true;
else
return false;
}
// end non-Equality operator
// Less than operator
bool RationalNumber::operator <(const RationalNumber & rhs) {
if (((float)this->GetNumerator() / (float)this->GetDenominator()) < ((float)rhs.GetNumerator() / (float)rhs.GetDenominator()))
return true;
else
return false;
}
// end less than operator
// Greater than operator
bool RationalNumber::operator >(const RationalNumber & rhs) {
if (((float)this->GetNumerator() / (float)this->GetDenominator()) > ((float)rhs.GetNumerator() / (float)rhs.GetDenominator()))
return true;
else
return false;
}
// end greater than operator
// Less than or equal to operator
bool RationalNumber::operator <=(const RationalNumber & rhs) {
if ((*this<rhs) || (*this == rhs))
return true;
else
return false;
}
// end less than or equal to operator.
// Greater than or equal to operator
bool RationalNumber::operator >=(const RationalNumber & rhs) {
if ((*this>rhs) || (*this == rhs))
return true;
else
return false;
}
// end greater than or equal to operator.
// Negation operator
RationalNumber RationalNumber::operator -() {
RationalNumber temp(-this->GetNumerator(), this->GetDenominator());
return temp;
}
//end negation operator
// pre-increment operator
RationalNumber& RationalNumber::operator ++(){
*this += 1;
return *this;
}
// end pre-increment operator
// post-increment operator
RationalNumber RationalNumber::operator ++(int){
RationalNumber t = *this;
*this += 1;
return t;
}
// end post-increment operator
// pre-decrement operator
RationalNumber& RationalNumber::operator --(){
*this -= 1;
return *this;
}
// end pre-decrement operator
// post-decrement operator
RationalNumber RationalNumber::operator --(int){
RationalNumber t = *this;
*this -= 1;
return t;
}
// end post-decrement operator
// Function operator to overload float
RationalNumber::operator float() {
return float(this->GetNumerator()) / float(this->GetDenominator());
}
// end function operator
//============================= OPERATIONS ===================================
// function that prints Rational number in fraction(a/b) form.
void RationalNumber::PrintFraction() const {
cout << this->GetNumerator() << " / " << this->GetDenominator();
}
// end function PrintFraction
// function that prints Rational number in floating-point(a.b) form.
void RationalNumber::PrintFloat() const {
cout << (float)this->GetNumerator() / (float)this->GetDenominator();
}
// end function PrintFloat
//============================= ACESS ===================================
// function that sets numerator of Rational Number
void RationalNumber::SetNumerator(int aNumerator) {
this->mNumerator = aNumerator;
}
// end function SetNumerator
// function that sets denominator of Rational Number
void RationalNumber::SetDenominator(int aDenominator) {
if (aDenominator == 0) {
cout << "Error. Denominator cannot be zero. Setting it to 1.";
this->mDenominator = 1;
}
else {
if (aDenominator < 0)
this->SetNumerator(-1 * this->GetNumerator());
this->mDenominator = abs(aDenominator);
}
}
// end function SetDenominator
// function that sets the RationalNumber
void RationalNumber::SetRationalNumber(int aNumerator, int aDenominator) {
this->SetNumerator(aNumerator);
this->SetDenominator(aDenominator);
}
// end function SetRationalNumber
// overloaded function that sets the RationalNumber
void RationalNumber::SetRationalNumber(const RationalNumber& rRationalNumber) {
this->SetRationalNumber(rRationalNumber.GetNumerator(), rRationalNumber.GetDenominator());
}
// end function SetRationalNumber
// function that gets numerator
int RationalNumber::GetNumerator()const {
return this->mNumerator;
}
// end function GetNumerator
// function that gets denominator
int RationalNumber::GetDenominator()const {
return this->mDenominator;
}
// end function GetDenominator
// function that gets Rational Number
const RationalNumber& RationalNumber::GetRationalNumber()const {
return *this;
}
// end function GetRationalNumber
/////////////////////////////// PRIVATE ///////////////////////////////////
// utility function that converts the rational into reduced form.
void RationalNumber::ReducedForm() {
int lDivisor = std::min(std::abs(this->GetNumerator()), std::abs(this->GetDenominator()));
for (int d = lDivisor; d > 1; --d) {
while ((this->GetNumerator() % d == 0) && (this->GetDenominator() % d == 0)) {
this->SetNumerator(this->GetNumerator() / d);
this->SetDenominator(this->GetDenominator() / d);
}
}
}