-
Notifications
You must be signed in to change notification settings - Fork 0
/
FractionClass.cpp
219 lines (169 loc) · 4.06 KB
/
FractionClass.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
#include "Fraction.h"
Fraction::Fraction() : num(0), den(1) {}
Fraction::Fraction(int num) : num(num), den(1) {}
Fraction::Fraction(int num, int den) : num(num), den(den)
{
if (den == 0) {
std::cerr << "Error!!! Denominator will never be zero." << std::endl;
exit(EXIT_FAILURE);
}
normalize();
}
Fraction::Fraction(const Fraction& other)
{
num = other.num;
den = other.den;
}
Fraction& Fraction::operator=(const Fraction& other)
{
if (this != &other) {
num = other.num;
den = other.den;
}
return *this;
}
std::ostream& operator<<(std::ostream& os, const Fraction& f)
{
os << f.num << "/" << f.den;
return os;
}
bool operator==(const Fraction& fra1, const Fraction& fra2)
{
if (fra1.num == fra2.num && fra1.den == fra2.den)
{
return true;
}
else
{
return false;
}
}
bool operator<(const Fraction& fra1, const Fraction& fra2) {
return fra1.num * fra2.den < fra2.num * fra1.den;
}
bool operator!=(const Fraction& fra1, const Fraction& fra2)
{
if (fra1.num == fra2.num && fra1.den == fra2.den)
{
return false;
}
else
{
return true;
}
}
bool operator<=(const Fraction& fra1, const Fraction& fra2) {
if (fra2 < fra1)
{
return false;
}
else {
return true;
}
}
bool operator>(const Fraction& fra1, const Fraction& fra2)
{
return fra2 < fra1;
}
bool operator>=(const Fraction& fra1, const Fraction& fra2) {
if (fra1 < fra2)
{
return false;
}
else
{
return true;
}
}
Fraction operator+(const Fraction& fra1, const Fraction& fra2)
{
//Fraction result(numer, denom);
return Fraction(fra1.num * fra2.den + fra2.num * fra1.den, fra1.den * fra2.den);
}
Fraction operator-(const Fraction& fra1, const Fraction& fra2)
{
Fraction result(fra1.num * fra2.den - fra2.num * fra1.den , fra1.den * fra2.den);
result.normalize();
return result;
}
Fraction operator*(const Fraction& fra1, const Fraction& fra2)
{
return Fraction(fra1.num * fra2.num, fra1.den * fra2.den);
}
Fraction operator/(const Fraction& fra1, const Fraction& fra2) {
if (fra2.num == 0) {
throw std::invalid_argument("Cannot divide by zero.");
}
Fraction result(fra1.num * fra2.den, fra1.den * fra2.num);
result.normalize();
return result;
}
Fraction Fraction::operator-() const {
Fraction result;
result.num= -num;
result.den= den;
return result;
}
Fraction& Fraction::operator+=(const Fraction& other) {
//Fraction result;
num = num * other.den + other.num * den;
den = den * other.den;
normalize();
return *this;
}
Fraction& Fraction::operator-=(const Fraction& other) {
num = num * other.den - other.num * den;
den *= other.den;
normalize();
return *this;
}
Fraction& Fraction::operator*=(const Fraction& other) {
num = num * other.num;
den= den * other.den;
normalize();
return *this;
}
Fraction& Fraction::operator/=(const Fraction& other) {
if (other.num == 0) {
std::cerr << "Error!!! Cannot divide by zero." << std::endl;
exit(EXIT_FAILURE);
}
num *= other.den;
den *= other.num;
Fraction& result = *this;
result.normalize();
return result;
}
Fraction& Fraction::operator++() {
this->num += this->den;
this->normalize();
return *this;
}
Fraction Fraction::operator++(int) {
Fraction temp(*this);
*this += 1;
return temp;
}
int Fraction::getNumerator() const {
return num;
}
int Fraction::getDenominator() const {
return den;
}
void Fraction::display() const {
std::cout << num << "/" << den<< std::endl;
}
void Fraction::normalize() {
den == 0 ? throw std::invalid_argument("Denominator cannot be zero.") : 0;
(den < 0) ? (num = -num, den = -den) : 0;
int divisor = gcd(std::abs(num), den);
(divisor > 1) ? (num /= divisor, den /= divisor) : 0;
}
int Fraction::gcd(int a, int b) const {
do {
int temp = b;
b = a % b;
a = temp;
} while (b != 0);
return a;
}