-
Notifications
You must be signed in to change notification settings - Fork 2
/
fraction.cpp
298 lines (235 loc) · 8.2 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
#include<iostream>
#include<stdexcept>
using namespace std;
// FRACTION BEGINS
// Returns the GCD of numerator and denominator
template<typename T>
T GCD( const T& numerator, const T& denominator )
{
if ( numerator % denominator == 0 )
{
return denominator;
}
return GCD( denominator, numerator % denominator );
}
// Class Forward Declaration
template<typename T> class Fraction;
// Routines without any attempt to prevent overflow
template<typename T> Fraction<T> operator +( const Fraction<T>& first, const Fraction<T>& second );
template<typename T> Fraction<T> operator -( const Fraction<T>& first, const Fraction<T>& second );
template<typename T> Fraction<T> operator *( const Fraction<T>& first, const Fraction<T>& second );
template<typename T> Fraction<T> operator /( const Fraction<T>& first, const Fraction<T>& second );
// Routines with an attempt to prevent overflow
// Assuming "first" and "second" are already in their Reduced form
template<typename T> Fraction<T> Add( Fraction<T> first, Fraction<T> second );
template<typename T> Fraction<T> Subtract( Fraction<T> first, Fraction<T> second );
template<typename T> Fraction<T> Multiply( Fraction<T> first, Fraction<T> second );
template<typename T> Fraction<T> Divide( Fraction<T> first, Fraction<T> second );
// Routines of a Fraction with an integer
template<typename T> Fraction<T> operator +( const Fraction<T>& first, const T& second );
template<typename T> Fraction<T> operator -( const Fraction<T>& first, const T& second );
template<typename T> Fraction<T> operator *( const Fraction<T>& first, const T& second );
template<typename T> Fraction<T> operator /( const Fraction<T>& first, const T& second );
// I/O operators for Fraction class
template<typename T> ostream& operator <<( ostream& out, const Fraction<T>& a );
template<typename T> istream& operator >>( istream& in, Fraction<T>& a );
// Class Definition begins
template<typename T>
class Fraction
{
// Private Data Members
T numerator, denominator;
public:
// Constructor
Fraction<T>( const T& numerator = 1, const T& denominator = 1 );
// Reduce the Fraction to the lowest terms
Fraction<T>& Reduce();
// Returns the numerical value of the Fraction as a double
double value() const;
// Getters
T GetNumerator() const;
T GetDenominator() const;
// Friend Functions necessary for successful compilation
friend Fraction operator + <>( const Fraction& first, const Fraction& second );
friend Fraction operator - <>( const Fraction& first, const Fraction& second );
friend Fraction operator * <>( const Fraction& first, const Fraction& second );
friend Fraction operator / <>( const Fraction& first, const Fraction& second );
friend Fraction operator + <>( const Fraction& first, const T& second );
friend Fraction operator - <>( const Fraction& first, const T& second );
friend Fraction operator * <>( const Fraction& first, const T& second );
friend Fraction operator / <>( const Fraction& first, const T& second );
friend Fraction Add <>( Fraction first, Fraction second );
friend Fraction Subtract <>( Fraction first, Fraction second );
friend Fraction Multiply <>( Fraction first, Fraction second );
friend Fraction Divide <>( Fraction first, Fraction second );
friend ostream& operator << <>( ostream& out, const Fraction& fraction );
friend istream& operator >> <>( istream& in, Fraction& fraction );
};
// Class Definition ends
// Member Function Definitions
template<typename T>
Fraction<T>::Fraction( const T& _numerator, const T& _denominator )
{
this->numerator = _numerator;
this->denominator = _denominator;
}
// Reduce the Fraction to the lowest terms
template<typename T>
Fraction<T>& Fraction<T>::Reduce()
{
if ( denominator == 0 )
{
throw domain_error( "" );
}
T gcd = GCD( numerator, denominator );
numerator /= gcd;
denominator /= gcd;
return *this;
}
template<typename T>
T Fraction<T>::GetNumerator() const
{
return numerator;
}
template<typename T>
T Fraction<T>::GetDenominator() const
{
return denominator;
}
template<typename T>
double Fraction<T>::value() const
{
if ( denominator == 0 )
{
throw domain_error( "" );
}
return numerator / ( double )denominator;
}
// Binary Arithmetic Function Definitions
template<typename T>
Fraction<T> operator +( const Fraction<T>& first, const Fraction<T>& second )
{
Fraction<T> fraction;
fraction.numerator = first.numerator * second.denominator + second.numerator * first.numerator;
fraction.denominator = first.denominator * second.denominator;
return fraction;
}
template<typename T>
Fraction<T> operator -( const Fraction<T>& first, const Fraction<T>& second )
{
Fraction<T> fraction;
fraction.numerator = first.numerator * second.denominator - second.numerator * first.numerator;
fraction.denominator = first.denominator * second.denominator;
return fraction;
}
template<typename T>
Fraction<T> operator *( const Fraction<T>& first, const Fraction<T>& second )
{
Fraction<T> fraction;
fraction.numerator = first.numerator * second.numerator;
fraction.denominator = first.denominator * second.denominator;
return fraction;
}
template<typename T>
Fraction<T> operator /( const Fraction<T>& first, const Fraction<T>& second )
{
Fraction<T> fraction;
fraction.numerator = first.numerator * second.denominator;
fraction.denominator = first.denominator * second.numerator;
return fraction;
}
template<typename T>
Fraction<T> Add( Fraction<T> first, Fraction<T> second )
{
Fraction<T> fraction;
T lcm = ( first.denominator * second.denominator ) / GCD( first.denominator, second.denominator );
fraction.denominator = lcm;
first.numerator *= lcm / first.denominator;
second.numerator *= lcm / second.denominator;
fraction.numerator = first.numerator + second.numerator;
return fraction;
}
template<typename T>
Fraction<T> Subtract( Fraction<T> first, Fraction<T> second )
{
Fraction<T> fraction;
T lcm = ( first.denominator * second.denominator ) / GCD( first.denominator, second.denominator );
fraction.denominator = lcm;
first.numerator *= lcm / first.denominator;
second.numerator *= lcm / second.denominator;
fraction.numerator = first.numerator - second.numerator;
return fraction;
}
template<typename T>
Fraction<T> Multiply( Fraction<T> first, Fraction<T> second )
{
Fraction<T> fraction;
T gcd = GCD( second.numerator, first.denominator );
second.numerator /= gcd;
first.denominator /= gcd;
gcd = GCD( first.numerator, second.denominator );
first.numerator /= gcd;
second.denominator /= gcd;
fraction.numerator = first.numerator * second.numerator;
fraction.denominator = first.denominator * second.denominator;
return fraction;
}
template<typename T>
Fraction<T> Divide( Fraction<T> first, Fraction<T> second )
{
std::swap( second.numerator, second.denominator );
Fraction<T> fraction;
T gcd = GCD( second.numerator, first.denominator );
second.numerator /= gcd;
first.denominator /= gcd;
gcd = GCD( first.numerator, second.denominator );
first.numerator /= gcd;
second.denominator /= gcd;
fraction.numerator = first.numerator * second.numerator;
fraction.denominator = first.denominator * second.denominator;
return fraction;
}
template<typename T>
Fraction<T> operator +( const Fraction<T>& first, const T& second )
{
return first + Fraction<T>( second, 1 );
}
template<typename T>
Fraction<T> operator -( const Fraction<T>& first, const T& second )
{
return first - Fraction<T>( second, 1 );
}
template<typename T>
Fraction<T> operator *( const Fraction<T>& first, const T& second )
{
return Fraction<T>( first.GetNumerator() * second, first.GetDenominator() );
}
template<typename T>
Fraction<T> operator /( const Fraction<T>& first, const T& second )
{
return Fraction<T>( first.GetNumerator(), first.GetDenominator() * second );
}
// I/O Operator Definitions
template<typename T>
ostream& operator <<( ostream& out, const Fraction<T>& fraction )
{
out << fraction.numerator << "/" << fraction.denominator;
return out;
}
template<typename T>
istream& operator >>( istream& in, Fraction<T>& fraction )
{
in >> fraction.numerator >> fraction.denominator;
return in;
}
// FRACTION ENDS
// Usage
int main()
{
Fraction<int> fraction1( 6, 5 );
Fraction<int> fraction2( 5, 2 );
Fraction<int> fraction3 = fraction1 * fraction2;
cout << fraction3 << endl;
fraction3.Reduce();
cout << fraction3 << endl;
}