-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFraction.cpp
More file actions
234 lines (188 loc) · 5.73 KB
/
Fraction.cpp
File metadata and controls
234 lines (188 loc) · 5.73 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "Fraction.hpp"
// TODO deal with negative parts correctly
/*******************************************************************
* constructors *
*******************************************************************/
Fraction::Fraction() :
numerator{0}, denominator{1}
{ }
Fraction::Fraction(int nn) :
numerator{nn}, denominator{1}
{ }
Fraction::Fraction(int nn, int dd) :
numerator{abs(nn)}, denominator{abs(dd)}
{
if(dd == 0)
throw invalid_argument("divide by zero");
if(nn < 0)
numerator *= -1;
if(dd < 0)
numerator *= -1;
reduce();
}
Fraction::Fraction(const Fraction& other) :
numerator{other.numerator}, denominator{other.denominator}
{ }
/*******************************************************************
* operators *
*******************************************************************/
Fraction& Fraction::operator+=(const Fraction& other) {
int common = lcm(denominator, other.denominator);
numerator *= common / denominator;
numerator += other.numerator * (common / other.denominator);
denominator = common;
reduce();
return *this;
}
Fraction& Fraction::operator-=(const Fraction& other) {
return operator+=(negative(other));
}
Fraction& Fraction::operator*=(const Fraction& other) {
numerator *= other.numerator;
denominator *= other.denominator;
reduce();
return *this;
}
Fraction& Fraction::operator/=(const Fraction& other) {
return operator*=(reciprocal(other));
}
Fraction Fraction::operator+(const Fraction& other) const {
Fraction ff1{*this};
ff1 += other;
return ff1;
}
Fraction Fraction::operator-(const Fraction& other) const {
return (*this) + negative(other);
}
Fraction Fraction::operator*(const Fraction& other) const {
Fraction ff1{*this};
ff1 *= other;
return ff1;
}
Fraction Fraction::operator/(const Fraction& other) const {
return (*this) * reciprocal(other);
}
bool Fraction::operator==(const Fraction& other) const {
return numerator == other.numerator && denominator == other.denominator;
}
bool Fraction::operator!=(const Fraction& other) const {
return !(operator==(other));
}
bool Fraction::operator>(const Fraction& other) const {
return((numerator * other.denominator) > (other.numerator * denominator));
}
bool Fraction::operator<(const Fraction& other) const {
return((numerator * other.denominator) < (other.numerator * denominator));
}
/*******************************************************************
* methods *
*******************************************************************/
int Fraction::get_numerator() const {
return numerator;
}
int Fraction::get_denominator() const {
return denominator;
}
void Fraction::set_numerator(int nn) {
numerator = nn;
reduce();
}
void Fraction::set_denominator(int dd){
if(dd == 0)
throw invalid_argument("divide by zero");
denominator = dd;
reduce();
}
void Fraction::reduce() {
int g = gcd(numerator, denominator);
while(g > 1) {
numerator /= g;
denominator /= g;
g = gcd(numerator, denominator);
}
}
/*******************************************************************
* helpers *
*******************************************************************/
Fraction reciprocal(const Fraction& fr) {
return Fraction{fr.get_denominator() ,fr.get_numerator()};
}
Fraction negative(const Fraction& fr) {
return Fraction{-fr.get_numerator() ,fr.get_denominator()};
}
/*******************************************************************
* i/o *
*******************************************************************/
// TODO try to return a representation as expanded with new denominator ndd
string expanded(const Fraction& fr, int ndd) {
ostringstream os;
int nn = fr.get_numerator();
int dd = fr.get_denominator();
char sign = '\0';
if(nn < 0) {
sign = '-';
nn = abs(nn);
}
if(dd == ndd)
os << fr;
else {
int common = lcm(dd, ndd);
nn *= common / dd;
if(nn % (common/ndd) == 0) {
nn /= (common/ndd);
dd = ndd;
os << sign << setw(2) << nn << "/" << setw(2) << dd;
}
else
os << fr;
}
return os.str();
}
// return a representation as a mixed number
string mixed(const Fraction& fr) {
ostringstream os;
int nn = fr.get_numerator();
int dd = fr.get_denominator();
int ww = 0;
char sign = '\0';
if(nn < 0) {
sign = '-';
nn = abs(nn);
}
if(nn < dd)
os << fr;
else {
ww = nn / dd;
nn = nn % dd;
os << sign << setw(2) << ww;
if(nn > 0)
os << ' ' << setw(2) << nn << "/" << setw(2) << dd;
}
return os.str();
}
// TODO implement mixed_and_expanded()
ostream& operator<<(ostream& os, const Fraction& fr) {
int nn = fr.get_numerator();
if(nn < 0) {
os << '-';
nn = abs(nn);
}
return os << setw(2) << nn << "/" << setw(2) << fr.get_denominator();
}
istream& operator>>(istream& is, Fraction& fr) {
int nn, dd;
char op;
is >> nn >> op >> dd;
if(!is || op != '/' || dd == 0) {
is.clear(ios_base::failbit);
throw invalid_argument("invalid fraction");
}
fr.set_numerator(nn);
fr.set_denominator(dd);
return is;
}