-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfraction.cpp
362 lines (309 loc) · 9.94 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
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
/*---------------------------------------------------------------------------*\
* fraction.cpp *
* Implementation of the Fraction class *
* *
* Created by: Colin Hamilton, Tufts University *
* Last Modified: May 8, 2014 *
\*---------------------------------------------------------------------------*/
#include<iostream>
#include<math.h>
#include "fraction.h"
using namespace std;
/* Default constructor sets fraction to zero. *
*/
Fraction::Fraction()
{
negative = false;
numerator = 0;
denominator = 1;
}
/* Sets the fraction to be the given number. *
*/
Fraction::Fraction(long long number)
{
negative = (number < 0);
numerator = negative ? (-number) : number;
denominator = 1;
}
/* Sets the fraction to be (numerator/denominator), properly reduced *
*/
Fraction::Fraction(long long numerator, long long denominator)
{
/* Equivalent to logical XOR; negative iff exactly one is negative */
negative = ((numerator < 0) != (denominator < 0));
this->numerator = (numerator < 0) ? (-numerator) : numerator;
this->denominator = (denominator < 0) ? (-denominator) : denominator;
reduce(&(this->numerator), &(this->denominator));
}
/*-------------NEEDS TESTING----------------*/
/* Sets the fraction based on the given mixed number. *
* If the whole number is negative, it signifies that the entire fraction is*
* negative. If this is not the desired behavior, make use of arithmetic *
* instead. *
*/
Fraction::Fraction(long long number, unsigned long long numerator,
unsigned long long denominator)
{
reduce(&numerator, &denominator);
negative = (number < 0);
if (negative) number = -number;
long long total_numerator = (number * denominator) + numerator;
this->numerator = total_numerator;
this->denominator = denominator;
}
unsigned long long Fraction::getNumerator()
{
return numerator;
}
unsigned long long Fraction::getDenominator()
{
return denominator;
}
bool Fraction::isNegative()
{
/* Accounts for nan; nan is not negative. */
return (negative && numerator != 0);
}
Fraction Fraction::operator=(long long rhs)
{
if (rhs < 0) {
negative = true;
rhs = -rhs;
}
numerator = rhs;
denominator = 1;
return *this;
}
Fraction Fraction::operator-()
{
negative = !negative;
return *this;
}
/* All arithmetic operations reduce the given numbers as quickly as possible,*
* to avoid overflow where possible. *
*/
Fraction Fraction::operator*=(long long rhs)
{
if (rhs < 0) {
negative = !negative;
rhs = -rhs;
}
reduce(&(this->denominator), (long long unsigned *) &rhs);
numerator *= rhs;
return *this;
}
Fraction Fraction::operator*=(Fraction rhs)
{
unsigned long long num = rhs.getNumerator();
unsigned long long den = rhs.getDenominator();
reduce(&numerator, &den);
reduce(&num, &denominator);
numerator *= num;
denominator *= den;
if (rhs.negative) negative = !negative;
return *this;
}
Fraction Fraction::operator/=(long long rhs)
{
if (rhs < 0) {
negative = !negative;
rhs = -rhs;
}
reduce(&(this->numerator), (long long unsigned *) &rhs);
denominator *= rhs;
return *this;
}
Fraction Fraction::operator/=(Fraction rhs)
{
unsigned long long num = rhs.getNumerator();
unsigned long long den = rhs.getDenominator();
reduce(&numerator, &num);
reduce(&den, &denominator);
numerator *= den;
denominator *= num;
if (rhs.isNegative()) negative = !negative;
return *this;
}
/* Must account for three cases: *
* -A positive fraction becoming negative, by adding a negative number of *
* greater magnitude *
* -A negative fraction becoming positive, by adding a positive number of *
* greater magnitude *
* -A fraction maintaining its sign *
*/
Fraction Fraction::operator+=(long long rhs)
{
if ((!negative && rhs < 0) &&
((unsigned long long)(-rhs * denominator) > numerator)) {
negative = true;
numerator = (-rhs * denominator) - numerator;
} else if ((negative && rhs > 0) &&
((unsigned long long)(rhs * denominator) > numerator)) {
negative = false;
numerator = (rhs * denominator) - numerator;
} else {
numerator += (rhs * denominator);
}
reduce(&numerator, &denominator);
return *this;
}
/* Must account for three cases: *
* -Adding a fraction of the same sign *
* -Adding a fraction of a different sign, greater magnitude *
* -Adding a fraction of a different sign, lesser magnitude *
*/
Fraction Fraction::operator+=(Fraction rhs)
{
unsigned long long lcm = LCM(denominator, rhs.denominator);
numerator *= (lcm / denominator);
rhs.numerator *= (lcm / rhs.denominator);
if (rhs.negative == negative) {
numerator += rhs.numerator;
} else if (rhs.numerator > numerator) {
numerator = rhs.numerator - numerator;
negative = !negative;
} else {
numerator -= rhs.numerator;
}
denominator = lcm;
reduce(&numerator, &denominator);
return *this;
}
Fraction Fraction::operator-=(long long rhs)
{
return operator+=(-rhs);
}
Fraction Fraction::operator-=(Fraction rhs)
{
return operator+=(-rhs);
}
bool Fraction::operator==(Fraction rhs)
{
return ((negative == rhs.negative) &&
(denominator == rhs.denominator) &&
(numerator == rhs.numerator));
}
/* Must account for four cases: *
* -Zero (denominator may be anything but zero, which signals nan) *
* -Non-integer (cannot equal an integer) *
* -Positive and negative numbers *
*/
bool Fraction::operator==(long long rhs)
{
if (rhs == 0) return numerator == 0 && denominator != 0;
if (denominator > 1) return false;
if (rhs < 0) return negative && numerator == (unsigned long long)-rhs;
return numerator == (unsigned long long)rhs;
}
bool Fraction::operator<(long long rhs)
{
return (*this - rhs).isNegative();
}
bool Fraction::operator<(Fraction rhs)
{
return (*this - rhs).isNegative();
}
/* Returns the result of raising the current fraction to the given integer *
* exponent. *
* Does not modify the existing fraction. *
*/
Fraction Fraction::power(int exp)
{
Fraction result(1);
Fraction base = *this;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}
/* Returns the reciprocal without modifying the existing fraction. *
*/
Fraction Fraction::reciprocal()
{
return Fraction((negative ? -denominator : denominator), numerator);
}
/* Returns the sqare root (or nan) without modifying the existing fraction. *
*/
Fraction Fraction::sqroot()
{
if(!negative)
return Fraction(sqrt((double)numerator), sqrt((double)denominator));
return Fraction(1, 0);
}
/* Determines the length, in characters, of the existing fraction. *
*/
unsigned Fraction::length()
{
if (numerator == 0) return 1; /* Fraction is 0 */
if (denominator == 0) return 3; /* Fraction is nan */
unsigned len = 0;
if (negative) len++; /* Account for the '-' */
len += length(numerator);
if (denominator == 1) return len; /* Fraction is an integer */
return len + 1 + length(denominator); /* Account for the '/' */
}
/* Returns the length of an integer. *
*/
unsigned Fraction::length(unsigned long long x)
{
if (x == 0) return 1;
unsigned len = 0;
while (x > 0) {
len++;
x /= 10;
}
return len;
}
/* Prints the fraction, accounting for nan, negative numbers, and integers.*
*/
void Fraction::print(ostream &stream)
{
if (numerator == 0) {
stream << 0;
} else if (denominator == 1) {
stream << (negative? "-" : "") << numerator;
} else if (denominator == 0) {
stream << "nan";
} else {
stream << (negative ? "-" : "") << numerator << "/" << denominator;
}
}
/* Reduces the given numbers by dividing out all multiples. *
* Takes the numbers by reference, modifying both (if necessary). *
*/
void Fraction::reduce(unsigned long long *num1, unsigned long long *num2)
{
int n1 = *num1; /* Use local variables to avoid frequent dereferencing */
int n2 = *num2;
for (int i = 2; i <= n1 && i <= n2; i++) {
if (n1 % i == 0 && n2 % i == 0) {
n1 /= i;
n2 /= i;
}
}
*num1 = n1;
*num2 = n2;
}
/* Returns the Least Common Multiple of the given two numbers. *
* Takes advantage of the fact LCM(a,b) == |a*b|/GCD(a,b) *
* Takes unsigned numbers, so no need to worry about absolute value. *
* Order of caluclations is to make overflow less likely. *
*/
unsigned long long Fraction::LCM(unsigned long long num1,
unsigned long long num2)
{
return (num1 / GCD(num1, num2)) * num2;
}
/* Returns the Greatest Common Divisor of the given two numbers. *
* Utilizes the Euclidean algorithm. *
* The first base case is merely for speed considerations, and to avoid *
* stack overflow when one of the numbers has become very small. *
* NOTE: Change to loop.
*/
unsigned long long Fraction::GCD(unsigned long long num1,
unsigned long long num2)
{
if (num1 == num2) return num1;
if (num1 > num2) return GCD(num2, num1 - num2);
return GCD(num1, num2 - num1);
}