-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfraction.py
35 lines (28 loc) · 873 Bytes
/
fraction.py
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
def gcd(m,n):
while m % n != 0:
m, n = n, m % n;
return n;
class Fraction:
def __init__(self, top, bottom):
self.num = top;
self.den = bottom;
def __str__(self):
return "{:d}/{:d}".format(self.num,self.den);
def __eq__(self, other_fraction):
first_num = self.num * other_fraction.den
second_num = other_fraction.num * self.den
return first_num == second_num
def __add__(self, other_fraction):
new_num = self.num * other_fraction.den \
+ self.den * other_fraction.num
new_den = self.den * other_fraction.den
cmmn = gcd(new_num, new_den)
return Fraction(new_num // cmmn, new_den // cmmn)
def show(self):
print("{:d}/{:d}".format(self.num, self.den))
x = Fraction(1, 2)
x.show()
y = Fraction(2, 3)
print(y)
print(x + y)
print(x == y)