forked from qiwsir/StarterLearningPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddfractions.py
More file actions
36 lines (29 loc) · 786 Bytes
/
addfractions.py
File metadata and controls
36 lines (29 loc) · 786 Bytes
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
#coding:utf-8
'''
filename: addfractions.py
'''
def gcd(a, b):
if not a > b:
a, b = b, a
while b != 0:
remainder = a % b
a, b = b, remainder
return a
def lcm(a, b):
return (a * b) / gcd(a,b)
class Fraction:
def __init__(self, number, denom=1):
self.number = number
self.denom = denom
def __str__(self):
return str(self.number) + '/' + str(self.denom)
__repr__ = __str__
def __add__(self, other):
lcm_num = lcm(self.denom, other.denom)
number_sum = (lcm_num / self.denom * self.number) + (lcm_num / other.denom * other.number)
return Fraction(number_sum, lcm_num)
if __name__ == "__main__":
m = Fraction(1, 3)
n = Fraction(1, 2)
s = m + n
print(m,"+",n,"=",s)