-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyFraction.py
41 lines (34 loc) · 1.12 KB
/
MyFraction.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
class MyFraction:
def __init__(self, numerator, denominator):
self.numerator = numerator
self.denominator = denominator
def __str__(self):
return str(self.numerator) + "/" + str(self.denominator)
def __add__(self, fraction):
self.sumNum = self.numerator + fraction.numerator
self.sumDen = self.gcf(self.denominator, fraction.denominator)
answer = MyFraction(self.sumNum, self.sumDen)
return answer
def __sub__(self, fraction):
self.difNum = self.numerator*fraction.denominator - fraction.numerator*self.denominator
if not self.difNum:
self.difDen = 0
else:
self.difDen = self.denominator*fraction.denominator
answer = MyFraction(self.difNum, self.difDen)
return answer
def __mul__(self, fraction):
answer = "Calculate the answer. The answer will be a fraction"
return answer
def __truediv__(self, fraction):
answer = "Calculate the answer. The answer will be a fraction"
return answer
def gcf(self, x, y):
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller + 1):
if((x % i == 0) and (y % i == 0)):
gcf = i
return gcf