-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrational.cpp
More file actions
executable file
·189 lines (165 loc) · 3.87 KB
/
rational.cpp
File metadata and controls
executable file
·189 lines (165 loc) · 3.87 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
// rational.cpp Andrew Levy Matthew Puentes
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cctype>
#include "rational.h"
int gcd(int a, int b) {
int c;
while (a != 0) {
c = a;
a = b % a;
b = c;
}
return b;
}
double getDouble(int x) {
double a = x;
return a;
}
Rational::Rational() {
numerator = 0;
denominator = 1;
}
Rational::Rational(const int wholeNumber) {
numerator = wholeNumber;
denominator = 1;
}
void Rational::giveValue() {
std::cout << numerator;
std::cout << "/";
std::cout << denominator << std::endl;
}
void Rational::simplify() {
if (denominator == 0) {
perror("ERROR: Denominator = 0");
exit(0);
}
int factor = gcd(numerator, denominator);
numerator = numerator / factor;
denominator = denominator / factor;
if (denominator != abs(denominator)) {
denominator = abs(denominator);
numerator = 0 - numerator;
}
}
Rational::Rational(const int num, const int den) {
numerator = num;
denominator = den;
}
std::ostream& operator <<(std::ostream& stream, const Rational &a){
stream << a.numerator << "/" << a.denominator;
return stream;
}
std::istream& operator >>(std::istream& stream, Rational &a){
int num;
char c;
int denum;
stream >> std::ws;
if(!(std::isdigit(stream.peek())) && stream.peek() != '-'){
throw devide_by_zero_exception();
}
stream >> num;
if(stream.peek() != '/'){
a = * (new Rational(num));
return stream;
}
stream >> c >> denum;
a = *(new Rational(num, denum));
return stream;
}
const Rational Rational::operator +(const Rational &a) const {
int sumNum = numerator * a.denominator + a.numerator * denominator;
int sumDen = denominator * a.denominator;
Rational rat(sumNum, sumDen);
rat.simplify();
return rat;
}
const Rational Rational::operator -(const Rational &a) const {
int sumNum = numerator * a.denominator - a.numerator * denominator;
int sumDen = denominator * a.denominator;
Rational rat(sumNum, sumDen);
rat.simplify();
return rat;
}
const Rational Rational::operator *(const Rational &a) const {
int sumNum = numerator * a.numerator;
int sumDen = denominator * a.denominator;
Rational rat(sumNum, sumDen);
rat.simplify();
return rat;
}
const Rational Rational::operator /(const Rational &a) const {
int sumNum = numerator * a.denominator;
int sumDen = denominator * a.numerator;
Rational rat(sumNum, sumDen);
rat.simplify();
return rat;
}
const bool Rational::operator ==(const Rational &a) const {
double A, B;
A = getDouble(numerator) / getDouble(denominator);
B = getDouble(a.numerator) / getDouble(a.denominator);
if (A == B) {
return true;
} else {
return false;
}
}
const bool Rational::operator !=(const Rational &a) const {
double A, B;
A = getDouble(numerator) / getDouble(denominator);
B = getDouble(a.numerator) / getDouble(a.denominator);
if (A == B) {
return false;
} else {
return true;
}
}
const bool Rational::operator <(const Rational &a) const {
double A, B;
A = getDouble(numerator) / getDouble(denominator);
B = getDouble(a.numerator) / getDouble(a.denominator);
if (A < B) {
return true;
} else {
return false;
}
}
const bool Rational::operator >(const Rational &a) const {
double A, B;
A = getDouble(numerator) / getDouble(denominator);
B = getDouble(a.numerator) / getDouble(a.denominator);
if (A >= B) {
return true;
} else {
return false;
}
}
const bool Rational::operator <=(const Rational &a) const {
double A, B;
A = getDouble(numerator) / getDouble(denominator);
B = getDouble(a.numerator) / getDouble(a.denominator);
if (A < B) {
return true;
} else {
return false;
}
}
const bool Rational::operator >=(const Rational &a) const {
double A, B;
A = getDouble(numerator) / getDouble(denominator);
B = getDouble(a.numerator) / getDouble(a.denominator);
if (A >= B) {
return true;
} else {
return false;
}
}
double Rational::toDouble() {
double n, d, y;
n = numerator;
d = denominator;
y = n / d;
return y;
}