-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRat Class.cpp
160 lines (135 loc) · 3.47 KB
/
Rat Class.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
#include <iostream>
#include <cmath>
using namespace std;
class Rat{
private:
int n;
int d;
public:
// constructors
// default constructor
Rat(){
n=0;
d=1;
}
// 2 parameter constructor
Rat(int i, int j){
n=i;
d=j;
}
// conversion constructor
Rat(int i){
n=i;
d=1;
}
//accessor functions (usually called get() and set(...) )
int getN(){ return n;}
int getD(){ return d;}
void setN(int i){ n=i;}
void setD(int i){ d=i;}
//arithmetic operators
Rat operator+(Rat r){
Rat t;
t.n = n*r.d + d*r.n;
t.d = d*r.d;
reduced(t);
return t;
}
// Write the other 3 operators (operator-, operator*, operator/).
Rat operator-(Rat r){
Rat t;
t.n=n*r.d-d*r.n;
t.d = d*r.d;
reduced(t);
return t;
}
Rat operator*(Rat r){
Rat t;
t.n= n*r.n;
t.d= d*r.d;
reduced(t);
return t;
}
Rat operator/(Rat r){
Rat t;
t.n=n*r.d;
t.d=d*r.n;
reduced(t);
return t;
}
// Write a function to reduce the Rat to lowest terms, and then you can call this function from other functions.
// Also make sure that the denominator is positive. Rats should be printed in reduced form.
void reduced(Rat& r){
int cd= gcd(r.n,r.d);
r.d=r.d/cd;
r.n=r.n/cd;
}
// Calculate the GCD (Euclid's algorithm)
int gcd(int n, int d){
return d == 0 ? n : gcd(d, n%d);
}
// 2 overloaded i/o operators
friend ostream& operator<<(ostream& os, Rat r);
friend istream& operator>>(istream& is, Rat& r);
}; //end Rat
// operator<<() is NOT a member function but since it was declared a friend of Rat
// it has access to its private parts.
ostream& operator<<(ostream& os, Rat r){
// Rewrite this function so that improper fractions are printed as mixed numbers. For example:
// 3/2 should be printed as 1 1/2
// 1/2 should be printed as 1/2
// 2/1 should be printed as 2
// 0/1 should be printed as 0
// Negative numbers should be printed the same way, but beginning with a minus sign
int whole=0,remain=0;
if(r.n==0){
os<<r.n;
return os;
}
if(abs(r.n)>=r.d){
whole = r.n/r.d;
remain= r.n % r.d;
if(r.n%r.d==0){
os<<whole;
}else{
os<<whole<<" "<<remain<<"/"<<r.d;
}
}else{
os<<r.n<<"/"<<r.d;
}
return os;
}
// operator>>() is NOT a member function but since it was declared a friend of Rat
// it has access to its private parts.
istream& operator>>(istream& is, Rat& r){
is >> r.n >> r.d;
return is;
}
int main() {
Rat r1(5, 2), r2(3, 2);
cout << "r1: " << r1 << endl;
cout << "r2: " << r2 << endl;
cout << "r1 + r2: " << r1 + r2 << endl;
cout << "r1 - r2: " << r1 - r2 << endl;
cout << "r1 * r2: " << r1 * r2 << endl;
cout << "r1 / r2: " << r1 / r2 << endl;
cout << endl;
r1 = r2;
r2 = r1 * r2;
cout << "r1: " << r1 << endl;
cout << "r2: " << r2 << endl;
cout << "r1 + r2: " << r1 + r2 << endl;
cout << "r1 - r2: " << r1 - r2 << endl;
cout << "r1 * r2: " << r1 * r2 << endl;
cout << "r1 / r2: " << r1 / r2 << endl;
cout << endl;
r1 = r2 + r1 * r2 / r1;
r2 = r2 + r1 * r2 / r1;
cout << "r1: " << r1 << endl;
cout << "r2: " << r2 << endl;
cout << "r1 + r2: " << r1 + r2 << endl;
cout << "r1 - r2: " << r1 - r2 << endl;
cout << "r1 * r2: " << r1 * r2 << endl;
cout << "r1 / r2: " << r1 / r2 << endl;
return 0;
}