-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrac.cpp
136 lines (107 loc) · 2.84 KB
/
frac.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
#include "frac.h"
#include <iostream>
using namespace std;
int gcd(int x, int y) {
// cout<< "\nThe greatest common divsor of "<<n<<" and "<<m<< " is ";
if (y == 0) {
// cout<< n<< "\n";
return x;
}else
return gcd(y, x%y);
}
int lcm(int n, int m) {
// For the gcd(x, y) to work, x >= y, y >= 0
int x = (n >= m) ? n: m;
int y = (n < m) ? n: m;
return (x*y)/gcd(x, y);
}
Fraction::Fraction() {
num = 0;
denom = 1;
}
Fraction::Fraction(int n, int d) {
num = n;
denom = d;
}
int Fraction::fracGCD(){
// For the gcd(x, y) to work, x >= y, y >= 0
int x = (num >= denom) ? num: denom;
int y = (num < denom) ? num: denom;
int result = gcd(x, y);
return result;
}
void Fraction::setNum(int n) {
num = n;
}
void Fraction::setDenom(int d) {
denom = d;
}
int Fraction::getNum()const {
return num;
}
int Fraction::getDenom()const {
return denom;
}
void Fraction::print(ostream & out)const {
out<< num<< "/"<< denom;
}
//--- Definition of output operator
ostream & operator<< (ostream & out, const Fraction & frac)
{
frac.print(out);
return out;
}
Fraction Fraction::add(const Fraction& F)const {
Fraction result = Fraction();
int localNum = this->num;
int otherNum = F.getNum();
int localDenom = this->denom;
int otherDenom = F.getDenom();
// Case 1: local and other denom are equal.
if (localDenom == otherDenom) { // the same if (this->denom == F.getDenom())
result.setNum(localNum+otherNum);
result.setDenom(localDenom);
} else if (localDenom != otherDenom) {
result.setNum((localNum*otherDenom)+(otherNum*localDenom));
result.setDenom(localDenom*otherDenom);
}
return result;
}
Fraction Fraction::sub(const Fraction& F)const {
Fraction result = Fraction();
int localNum = this->num;
int otherNum = F.getNum();
int localDenom = this->num;
int otherDenom = F.getDenom();
if (localDenom == otherDenom){
result.setNum(localNum-otherNum);
result.setDenom(localDenom);
} else if (localDenom != otherDenom){
result.setNum((localNum*otherDenom) - (otherNum*localDenom));
result.setDenom(localDenom*otherDenom);
}
return result;
}
Fraction Fraction::mult(const Fraction& F)const {
Fraction product = Fraction(this->num * F.getNum(), this->denom * F.getDenom());
return product;
}
Fraction Fraction::div(const Fraction& F)const {
Fraction result = Fraction(this->num * F.getDenom(), this->denom * F.getNum());
return result;
}
bool Fraction::gt(const Fraction& F)const {
int LCM = lcm(denom, F.getDenom());
return ((num * (LCM/denom)) > (F.getNum() * (LCM/F.getDenom())));
}
bool Fraction::lt(const Fraction& F)const {
int LCM = lcm(denom, F.getDenom());
return ((num * (LCM/denom)) < (F.getNum() * (LCM/F.getDenom())));
}
void Fraction::reduce() {
int commonDivisor = this->fracGCD();
if (commonDivisor > 1) {
this->num = num/commonDivisor;
this->denom = denom/commonDivisor;
}
}