-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction.cpp
More file actions
411 lines (323 loc) · 7.43 KB
/
fraction.cpp
File metadata and controls
411 lines (323 loc) · 7.43 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "fraction.h"
fraction::fraction(long long int n, long long int d ) //constructor with parameters
{
num = n;
denom = d;
reduce();
}
fraction::fraction(const fraction &other) //copy constructor
{
copy(other);
}
fraction::~fraction() //destructor
{
num = 0;
denom = 0;
}
//Assignment operators
fraction& fraction::operator=(const fraction &other)//assignment operator
{
if(this != &other)
copy(other);
return *this;
}
fraction& fraction::operator+=(const fraction &other)
{
*this = *this + other;
return *this;
}
fraction& fraction::operator-=(const fraction &other)
{
*this = *this - other;
return *this;
}
fraction& fraction::operator*=(const fraction &other)
{
*this = *this * other;
return *this;
}
fraction& fraction::operator/=(const fraction &other)
{
*this = *this / other;
return *this;
}
fraction& fraction::operator+=(const long long int &other)
{
*this = *this + other;
return *this;
}
fraction& fraction::operator-=(const long long int &other)
{
*this = *this - other;
return *this;
}
fraction& fraction::operator*=(const long long int &other)
{
*this = *this * other;
return *this;
}
fraction& fraction::operator/=(const long long int &other)
{
*this = *this / other;
return *this;
}
//Accessor functions
long long int fraction::getNum() const
{
return num;
}
long long int fraction::getDenom() const
{
return denom;
}
//Mutator functions
void fraction::setNum(long long int n)
{
num = n;
reduce();
}
void fraction::setDenom(long long int d)
{
if(d == 0)
throw ILLEGAL_FRACTION;
else
{
denom = d;
reduce();
}
}
//protected:
long long int& fraction::theNum()
{
return num;
}
long long int& fraction::theDenom()
{
return denom;
}
void fraction::setFraction(long long int x, long long int y)
{
if(y == 0)
throw ILLEGAL_FRACTION;
num = x;
denom = y;
reduce();
}
//private:
void fraction::copy(const fraction &other)
{
num = other.num;
denom = other.denom;
reduce();
}
void fraction::reduce()
{
if(denom < 0)
{
num *= -1;
denom *= -1;
}
long long int divisor = gcd(abs(num), abs(denom));
num /= divisor;
denom /= divisor;
}
long long int fraction::gcd(long long int p , long long int q)
{
return q == 0 ? p : gcd(q, p%q);
}
//Friends that can take two fractions and create a third fraction
fraction operator*(const fraction &x, const fraction &y)
{
return fraction(x.num*y.num, x.denom * y.denom);
}
fraction operator/(const fraction &x, const fraction &y)
{
if(y == 0)
throw ILLEGAL_FRACTION;
else
return fraction(x.num*y.denom, x.denom * y.num);
}
fraction operator-(const fraction &x, const fraction &y)
{
return fraction(x.num*y.denom-x.denom*y.num, x.denom*y.denom);
}
fraction operator+(const fraction &x, const fraction &y)
{
return fraction(x.num*y.denom+x.denom*y.num, x.denom*y.denom);
}
fraction operator^(const fraction &x, const fraction &y)
{
if(x < 0 && y.getDenom()%2 == 0 && y<1 && y > -1)
throw ILLEGAL_FRACTION;
else
{
long double num, denom, decimo;
long long int appoxNum, appoxDenom;
num = y.num;
denom = y.denom;
decimo = num/denom;
if(x.num < 0 && y.num%2 == 1)
num = -pow(-x.num, decimo);
else if (x.num < 0 && y.num%2 == 0)
num = pow(-x.num, decimo);
else
num = pow(x.num, decimo);
denom = pow(x.denom, decimo);
appoxNum = num;
appoxDenom = denom;
if(appoxNum != num || appoxDenom != denom)
{
appoxNum = num * 1000000;
appoxDenom = denom * 1000000;
}
return fraction(appoxNum, appoxDenom);
}
}
//s that take an long long integer and a mixed number
fraction operator*(const fraction &x, const long long int &y)
{
return fraction(x.num*y,x.denom);
}
fraction operator/(const fraction &x, const long long int &y)
{
if(y == 0)
throw ILLEGAL_FRACTION;
else
return fraction(x.num,x.denom*y);
}
fraction operator-(const fraction &x, const long long int &y)
{
return fraction(x.num - y*x.denom ,x.denom);
}
fraction operator+(const fraction &x, const long long int &y)
{
return fraction(x.num + y*x.denom ,x.denom);
}
fraction operator^(const fraction &x, const long long int &y)
{
if(pow(x.num, y) == HUGE_VAL || pow(x.denom, y) == HUGE_VAL)
return fraction(HUGE_VAL, 1);
else
return fraction(pow(x.num, y),pow(x.denom, y));
}
fraction operator*(const long long int &x, const fraction &y)
{
return fraction(x *y.num, y.denom);
}
fraction operator/(const long long int &x, const fraction &y)
{
if(y == 0)
throw ILLEGAL_FRACTION;
else
return fraction(x *y.denom, y.num);
}
fraction operator-(const long long int &x, const fraction &y)
{
return fraction(x*y.denom - y.num, y.denom);
}
fraction operator+(const long long int &x, const fraction &y)
{
return fraction(x*y.denom + y.num, y.denom);
}
fraction operator^(const long long int &x, const fraction &y)
{
if(pow(x, y.num/y.denom) == HUGE_VAL)
return fraction(HUGE_VAL, 1);
else
{
double num, denom, decimo;
long long int appox;
num = y.num;
denom = y.denom;
decimo = num/denom;
decimo = pow(x, decimo);
appox = decimo * 10000;
return fraction(appox, 100000);
}
}
//input / output friemds
ostream& operator<<(ostream &out, const fraction &x)
{
out<<x.num<<"/"<<x.denom;
return out;
}
istream& operator>>(istream &in, fraction &x)
{
char junk;
in>>x.num>>junk>>x.denom;
return in;
}
//Comparision operator s
bool operator==(const fraction &x, const fraction &y)
{
return x.num*y.denom == x.denom*y.num;
}
bool operator!=(const fraction &x, const fraction &y)
{
return !(x == y);
}
bool operator>=(const fraction &x, const fraction &y)
{
return x.num*y.denom >= x.denom*y.num;
}
bool operator<=(const fraction &x, const fraction &y)
{
return x.num*y.denom <= x.denom*y.num;
}
bool operator>(const fraction &x, const fraction &y)
{
return x.num*y.denom > x.denom*y.num;
}
bool operator<(const fraction &x, const fraction &y)
{
return x.num*y.denom < x.denom*y.num;
}
//Comparision operators for fractions and long long integers
bool operator==(const fraction &x, const long long int &y)
{
return x.num == y*x.denom;
}
bool operator!=(const fraction &x, const long long int &y)
{
return !(x == y);
}
bool operator>=(const fraction &x, const long long int &y)
{
return x.num >= y*x.denom;
}
bool operator<=(const fraction &x, const long long int &y)
{
return x.num <= y*x.denom;
}
bool operator>(const fraction &x, const long long int &y)
{
return x.num > y*x.denom;
}
bool operator<(const fraction &x, const long long int &y)
{
return x.num < y*x.denom;
}
bool operator==(const long long int &x, const fraction &y)
{
return y == x;
}
bool operator!=(const long long int &x, const fraction &y)
{
return !(y == x);
}
bool operator>=(const long long int &x, const fraction &y)
{
return x*y.denom >= y.num;
}
bool operator<=(const long long int &x, const fraction &y)
{
return x*y.denom <= y.num;
}
bool operator>(const long long int &x, const fraction &y)
{
return x*y.denom > y.num;
}
bool operator<(const long long int &x, const fraction &y)
{
return x*y.denom < y.num;
}