-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rational_Overload.cpp
163 lines (135 loc) · 4.08 KB
/
Rational_Overload.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
//
// Keita Nonaka
//
// This is Rational class, which calculate rational numbers with overloading.
// This class is for implementing methods.
//
#include "Rational_Overload.h"
#include <iostream>
using namespace std;
Rational_Overload::Rational_Overload() { // constructor
numerator = 0;
denominator = 0; // doesnt matter if denominator is assigned 0
}
Rational_Overload::Rational_Overload(int a, int b) {
int GCD = gcd(a, b);
numerator = a / GCD;
denominator = b / GCD;
reduce(numerator, denominator);
}
Rational_Overload::~Rational_Overload() {
// nothing
}
int Rational_Overload::gcd(int a, int b){ // gcd
if(b == 0)
return a;
return gcd(b, a % b);
}
void Rational_Overload::add(Rational_Overload obj) { // addition
numerator = numerator * obj.denominator + obj.numerator * denominator;
denominator = denominator * obj.denominator;
reduce(numerator, denominator);
}
void Rational_Overload::sub(Rational_Overload obj) { // subtraction
obj.negate();
this->add(obj);
}
void Rational_Overload::mul(Rational_Overload obj) { // multiplication
numerator = numerator * obj.numerator;
denominator = denominator * obj.denominator;
reduce(numerator, denominator);
}
void Rational_Overload::div(Rational_Overload obj) { // division
obj.reciprocal();
this->mul(obj);
}
Rational_Overload Rational_Overload::operator+(Rational_Overload obj) { // addition
Rational_Overload x = *this;
x.add(obj);
return x;
}
Rational_Overload Rational_Overload::operator-(Rational_Overload obj) { // subtraction
Rational_Overload x = *this;
x.sub(obj);
return x;
}
Rational_Overload Rational_Overload::operator*(Rational_Overload obj) { // multiplication
Rational_Overload x = *this;
x.mul(obj);
return x;
}
Rational_Overload Rational_Overload::operator/(Rational_Overload obj) { // division
Rational_Overload x = *this;
x.div(obj);
return x;
}
Rational_Overload Rational_Overload::operator-() { // negation
Rational_Overload x = *this;
x.negate();
return x;
}
Rational_Overload Rational_Overload::operator!() { // reciprocal
Rational_Overload x = *this;
x.reciprocal();
return x;
}
bool Rational_Overload::operator<(Rational_Overload obj) { // less
if(this->numerator * obj.denominator < this->denominator * obj.numerator)
return true;
return false;
}
bool Rational_Overload::operator<=(Rational_Overload obj) { // less or equal
if(this->numerator * obj.denominator <= this->denominator * obj.numerator)
return true;
return false;
}
bool Rational_Overload::operator>(Rational_Overload obj) { // greater
if(this->numerator * obj.denominator > this->denominator * obj.numerator)
return true;
return false;
}
bool Rational_Overload::operator>=(Rational_Overload obj) { // greater or equal
if(this->numerator * obj.denominator >= this->denominator * obj.numerator)
return true;
return false;
}
bool Rational_Overload::operator==(Rational_Overload obj) { // equal
if(this->numerator * obj.denominator == this->denominator * obj.numerator)
return true;
return false;
}
bool Rational_Overload::operator!=(Rational_Overload obj) { // not equal
if(this->numerator * obj.denominator != this->denominator * obj.numerator)
return true;
return false;
}
void Rational_Overload::reciprocal() { // reciprocal
int numer = numerator;
numerator = denominator;
denominator = numer;
reduce(numerator, denominator);
}
void Rational_Overload::negate() { // negation
numerator = -numerator;
}
void Rational_Overload::reduce(int& numer, int& denom){ // reduce
int GCD = gcd(numer, denom);
numer /= GCD;
denom /= GCD;
if (denom < 0) {
numer = -numer;
denom = -denom;
}
}
int Rational_Overload::getNumer() {
return numerator;
}
int Rational_Overload::getDenom() {
return denominator;
}
void Rational_Overload::print() { // print fraction
cout << numerator << "/" << denominator;
}
void Rational_Overload::printFloat() { // print float
cout << numerator / (float)denominator;
}