-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_fractions.py
More file actions
73 lines (54 loc) · 1.92 KB
/
_fractions.py
File metadata and controls
73 lines (54 loc) · 1.92 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
# coding=utf-8
def gcd(m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
class Fraction:
def __init__(self, top, bottom):
common = gcd(top, bottom)
self.num = top // common
self.den = bottom // common
def getNum(self):
return self.num
def getDen(self):
return self.den
def show(self):
print(self.num,"/",self.den)
def __add__(self,otherfraction):
newnum = self.num*otherfraction.den + self.den*otherfraction.num
newden = self.den * otherfraction.den
return Fraction(newnum, newden)
def __iadd__(self,otherfraction):
newnum = self.num*otherfraction.den + self.den*otherfraction.num
newden = self.den * otherfraction.den
return Fraction(newnum, newden)
def __sub__(self, otherfraction):
newnum = self.num*otherfraction.den - self.den*otherfraction.num
newden = self.den * otherfraction.den
return Fraction(newnum, newden)
def __mul__(self, otherfraction):
newnum = self.num*otherfraction.num
newden = self.den * otherfraction.den
return Fraction(newnum, newden)
def __truediv__(self, otherfraction):
newnum = self.num * otherfraction.den
newden = self.den * otherfraction.num
return Fraction(newnum, newden)
def __div__(self, otherfraction):
newnum = self.num * otherfraction.den
newden = self.den * otherfraction.num
return Fraction(newnum, newden)
def __eq__(self, other):
firstnum = self.num * other.den
secondnum = other.num * self.den
# def __str__(self):
# return str(self.num)+"/"+str(self.den)
def __repr__(self):
return str(self.num)+"/"+str(self.den)
return firstnum == secondnum
if __name__ == '__main__':
a = Fraction(1, 7)
b = Fraction(3, 7)