-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractionClass.cpp
232 lines (198 loc) · 6.11 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
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* fractionClass.cpp
*
* Author: Harry Lewis
* Implementation for a Fraction Class using overloaded operators for CMPE320 assignment 1.
*/
// dependencies
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cmath>
// internal dependencies
#include "fractionClass.h"
// current name space
using namespace std;
// Fraction Exception Methods
// --------------------------
// initialize the message to be outputted
FractionException::FractionException(const string& message) : message(message) {}
// display the message
string& FractionException::what() { return message; }
// Fraction Class Methods
// ----------------------
// no-parameter initializer
Fraction::Fraction() : numer(0), denom(1) {}
// 1 parameter initializer
Fraction::Fraction(int numerator) : numer(numerator), denom(1) {}
// 2 parameter intializer
Fraction::Fraction(int numerator, int denominator) : numer(numerator), denom(denominator) {
if (denominator == 0) {
throw FractionException("You cannot have a fraction with a denominator of 0");
} else {
int gcf = greatestCommonFactor(abs(denominator), abs(numerator));
numer = numerator / gcf;
denom = denominator / gcf;
if (denominator < 0) {
denom = abs(denominator/gcf);
numer = -numerator/gcf;
}
}
}
// put in lowest terms
int Fraction::greatestCommonFactor(int m, int n) {
if (m <= n && (n % m == 0)) {
return m;
} else if (n < m) {
return greatestCommonFactor(n, m);
} else {
return greatestCommonFactor(m, n % m);
}
}
// let's negate
Fraction Fraction::operator - (void) const {
return Fraction(-numer, denom);
}
// let's add
Fraction operator + (const Fraction& addend, const Fraction& addend2) {
if (addend2.denom == addend.denom) {
return Fraction(addend2.numer + addend.numer, addend.denom);
} else {
return Fraction(addend2.numer * addend.denom + addend2.denom * addend.numer, addend2.denom * addend.denom);
}
}
// let's subtract
Fraction operator - (const Fraction& subtractor, const Fraction& subtractor2) {
if (subtractor2.denom == subtractor.denom) {
return Fraction(subtractor.numer - subtractor2.numer, subtractor2.denom);
} else {
return Fraction(subtractor.numer * subtractor2.denom - subtractor2.numer * subtractor.denom, subtractor2.denom * subtractor.denom);
}
}
// let's multiply
Fraction operator * (const Fraction& multiplicant, const Fraction& multiplicant2) {
return Fraction(multiplicant.numer * multiplicant2.numer, multiplicant.denom * multiplicant2.denom);
}
// let's divide
Fraction operator / (const Fraction& dividend, const Fraction& dividend2) {
return Fraction(dividend.numer * dividend2.denom, dividend.denom * dividend2.numer);
}
// post increment
Fraction Fraction::operator ++ (int unused) {
Fraction clone(numer, denom);
numer += denom;
return clone;
}
Fraction Fraction::operator ++ () {
numer += denom;
return *this;
}
// increment assignment
Fraction Fraction::operator += (const Fraction& addend) {
if (denom == addend.denom)
numer += addend.numer;
else {
numer = numer*addend.denom + addend.numer*denom;
denom = denom*addend.denom;
}
return *this;
}
// equality operator
bool Fraction::operator == (const Fraction& equalizer) {
if (numer == equalizer.numer && denom == equalizer.denom) {
return true;
}
return false;
}
// inequality operator
bool operator != (const Fraction& equalizer, const Fraction& negator) {
if (equalizer.numer != negator.numer || equalizer.denom != negator.denom)
return true;
return false;
}
// greater than operator
bool operator > (const Fraction& greater, const Fraction& lesser) {
if (greater.denom == lesser.denom) {
if (greater.numer > lesser.numer)
return false;
return true;
} else {
if (greater.numer*lesser.denom > lesser.numer*greater.denom)
return true;
return false;
}
}
// greater than or equal to operator
bool Fraction::operator >= (const Fraction& lesseror) {
if (denom == lesseror.denom) {
if (numer >= lesseror.numer)
return false;
return true;
} else {
numer = numer*lesseror.denom;
if (numer >= lesseror.numer*denom)
return true;
return false;
}
}
// less than operator
bool operator < (const Fraction& lesser, const Fraction& greater) {
if (lesser.denom == greater.denom) {
if (lesser.numer < greater.numer)
return false;
return true;
} else {
if (lesser.numer*greater.denom < greater.numer*lesser.denom)
return true;
return false;
}
}
// less than or equal to operator
bool Fraction::operator <= (const Fraction& greateror) {
if (denom == greateror.denom) {
if (numer <= greateror.numer)
return false;
return true;
} else {
numer = numer*greateror.denom;
if (numer <= greateror.numer*denom)
return true;
return false;
}
}
// let's put into the stream
ostream& operator << (ostream& os, const Fraction& fraction) {
os << fraction.numerator() << "/" << fraction.denominator();
return os;
}
// let's take out of the stream
istream& operator >> (istream& is, Fraction& fraction) {
string input;
//Fraction frac;
int n, d;
getline(is, input);
string numerString;
string denomString;
bool slashFlag(false);
for (int i = 0; i < input.length(); i++) {
if (input[i] == '/')
slashFlag = true;
}
//in.find
if (slashFlag) {
for(int i = 0; i < input.length(); i++){
if(input[i] == '/'){
numerString.append(input.substr(0,i));
denomString.append(input.substr(i+1,input.length() - 1));
fraction.numer = atoi(numerString.c_str());
fraction.denom = atoi(denomString.c_str());
}
}
} else {
fraction.numer = atoi(input.c_str());
fraction.denom = 1;
}
return is;
}