-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc1_num_den.py
44 lines (35 loc) · 1.2 KB
/
c1_num_den.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
36
37
38
39
40
41
42
43
44
def gcd(s, m):
while m % s != 0:
m, s = s, m % s
return s
class Fraction(object):
def __init__(self, num, den):
self.num = num
self.den = den
# def show(self):
# print(str(self.num) + '/' + str(self.den))
def __str__(self):
return str(self.num) + '/' + str(self.den)
def __add__(self, other):
num_new = self.num * other.den + self.den * other.num
den_new = self.den * other.den
common = gcd(num_new, den_new)
if den_new / common < 0:
return Fraction(-num_new // common, -den_new // common)
else:
return Fraction(num_new // common, den_new // common)
def __sub__(self, other):
num_new = self.num * other.den - self.den * other.num
den_new = self.den * other.den
common = gcd(num_new, den_new)
if den_new / common < 0:
return Fraction(-num_new // common, -den_new // common)
else:
return Fraction(num_new // common, den_new // common)
def __eq__(self, other):
return self.num * other.den == self.den * other.num
a = Fraction(-3, 50)
b = Fraction(-1, 2)
c = a + b
d = b - a
print(a, b, c, d, a == b, sep=' ')