-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction.cpp
More file actions
157 lines (134 loc) · 3.31 KB
/
Copy pathfraction.cpp
File metadata and controls
157 lines (134 loc) · 3.31 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
#include <iostream>
#include <cmath>
using namespace std;
int gcd(int m, int n)
{
while (m % n != 0)
{
int oldm = m;
int oldn = n;
m = oldn;
n = oldm % oldn;
}
return n;
}
class Fraction
{
public:
Fraction(int top, int bottom)
{
int common = gcd(top, bottom);
num = top / common;
den = bottom / common;
}
Fraction(int top)
{
num = top;
den = 1;
}
Fraction()
{
num = 1;
den = 1;
}
Fraction operator+(Fraction otherFrac)
{
int newnum = num * otherFrac.den + den * otherFrac.num;
int newden = den * otherFrac.den;
return Fraction(newnum, newden);
}
Fraction operator-(Fraction otherFrac)
{
int newnum = num * otherFrac.den - den * otherFrac.num;
int newden = den * otherFrac.den;
return Fraction(newnum, newden);
}
Fraction operator*(Fraction otherFrac)
{
int newnum = num * otherFrac.num;
int newden = den * otherFrac.den;
return Fraction(newnum, newden);
}
Fraction operator/(Fraction otherFrac)
{
int newnum = num * otherFrac.den;
int newden = den * otherFrac.num;
return Fraction(newnum, newden);
}
bool operator==(Fraction otherFrac)
{
int firstnum = num * otherFrac.den;
int secondnum = otherFrac.num * den;
return firstnum == secondnum;
}
bool operator>(Fraction otherFrac)
{
int firstnum = num * otherFrac.den;
int secondnum = otherFrac.num * den;
return firstnum > secondnum;
}
bool operator>=(Fraction otherFrac)
{
int firstnum = num * otherFrac.den;
int secondnum = otherFrac.num * den;
return firstnum >= secondnum;
}
bool operator<(Fraction otherFrac)
{
int firstnum = num * otherFrac.den;
int secondnum = otherFrac.num * den;
return firstnum < secondnum;
}
bool operator<=(Fraction otherFrac)
{
int firstnum = num * otherFrac.den;
int secondnum = otherFrac.num * den;
return firstnum <= secondnum;
}
bool operator!=(Fraction otherFrac)
{
int firstnum = num * otherFrac.den;
int secondnum = otherFrac.num * den;
return firstnum != secondnum;
}
int getNum()
{
return num;
}
int getDen()
{
return den;
}
friend ostream &operator<<(ostream &stream, const Fraction &fraction);
private:
int num, den;
};
ostream &operator<<(ostream &stream, const Fraction &fraction)
{
stream << fraction.num << "/" << fraction.den;
return stream;
}
int main()
{
Fraction x(1.2, 2);
Fraction y(2, 4);
cout << x << " + " << y << " = " << x + y << endl;
if (x == y)
{
cout << "x is equal y" << endl;
}
else
{
cout << "x is not equal y" << endl;
}
cout << "The numerator of x is " << x.getNum() << ", the denominator is " << x.getDen() << endl;
cout << x << " - " << y << " = " << x - y << endl;
cout << x << " * " << y << " = " << x * y << endl;
cout << x << " / " << y << " = " << x / y << endl << endl;
cout << (x<=y) << endl;
cout << (x<y)<< endl;
cout << (x>y)<< endl;
cout << (x>=y) << endl;
cout << (x!=y) << endl;
return 0;
}