-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rational_overload.cpp
139 lines (113 loc) · 3.71 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
/*
* Apr 1, 2020
* Mizuki Hashimoto
* 7th edition p519-520 Ex11.10
* Member-function definitions for Rational_Overload class
*/
#include "Rational_overload.h" // include definition of Rational_Overload class from rational_overload.h
#include "ZeroDenomException.h" // include definition of ZeroDenomException class from ZeroDenomException.h
#include <iostream>
using namespace std;
int Rational_Overload::gcd(int n, int d) { // gcd
if(d == 0) return n;
return gcd(d, n % d);
}
void Rational_Overload::reduce() { // reduce
int d = gcd(num, den);
if (den < 0) { // if denominator is negative
num = -(num / d); // set numerator to be negative
den = -(den / d); // set denominator to be positive (3/-6 -> -1/2)
} else if (den == 0) {
throw ZeroDenomException(); // throw exception if inputted denominator is zero
} else {
num /= d;
den /= d;
}
}
Rational_Overload::Rational_Overload() { num = 0; den = 1; } // constructor
Rational_Overload::Rational_Overload(int n, int d) { // constructor
num = n;
den = d;
reduce();
}
void Rational_Overload::setValue(int n, int d) { // set numerator and denominator
num = n;
den = d;
reduce();
}
void Rational_Overload::negate() { num = -num; } // negate only the numerator
void Rational_Overload::reciprocal() { // switch numerator and denominator
int temp = num;
num = den;
den = temp;
reduce();
}
Rational_Overload Rational_Overload::operator-() { // negation overloading
Rational_Overload x = *this;
x.negate();
return x;
}
Rational_Overload Rational_Overload::operator!() { // reciprocal overloading
Rational_Overload x = *this;
x.reciprocal();
return x;
}
void Rational_Overload::add(Rational_Overload r) { // addition
int n = num * r.den + den * r.num;
int d = den * r.den;
setValue(n, d);
// compute (ad+bc)/bd
}
void Rational_Overload::sub(Rational_Overload r) { // subtraction
r.negate();
add(r); // num * r.den + (den) * (-r.num)
}
void Rational_Overload::mul(Rational_Overload r) { // multiplication
int n = num * r.num;
int d = den * r.den;
setValue(n, d);
}
void Rational_Overload::div(Rational_Overload r) { // division
r.reciprocal();
mul(r); // n = num * r.den; d = den * r.num
}
Rational_Overload Rational_Overload::operator+(Rational_Overload r) { // addition overloading
Rational_Overload x = *this;
x.add(r);
return x;
}
Rational_Overload Rational_Overload::operator-(Rational_Overload r) { // subtraction overloading
Rational_Overload x = *this;
x.sub(r);
return x;
}
Rational_Overload Rational_Overload::operator*(Rational_Overload r) { // multiplication overloading
Rational_Overload x = *this;
x.mul(r);
return x;
}
Rational_Overload Rational_Overload::operator/(Rational_Overload r) { // division overloading
Rational_Overload x = *this;
x.div(r);
return x;
}
bool Rational_Overload::operator<(Rational_Overload r) { // smaller than overloading
return num * r.den < den * r.num;
}
bool Rational_Overload::operator<=(Rational_Overload r) { // smaller than or equal overloading
return num * r.den <= den * r.num;
}
bool Rational_Overload::operator>(Rational_Overload r) { // greater than overloading
return num * r.den > den * r.num;
}
bool Rational_Overload::operator>=(Rational_Overload r) { // greater than or equal overloading
return num * r.den >= den * r.num;
}
bool Rational_Overload::operator==(Rational_Overload r) { // equal to overloading
return num * r.den == den * r.num;
}
bool Rational_Overload::operator!=(Rational_Overload r) { // not equal to overloading
return num * r.den != den * r.num;
}
void Rational_Overload::print() { cout << num << '/' << den; } // num/den format
void Rational_Overload::printf() { cout << (float)num / (float)den; } // d.dd format (decimal point)