-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathE2-7.py
33 lines (25 loc) · 802 Bytes
/
E2-7.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
# Copyright (C) Deepali Srivastava - All Rights Reserved
# This code is part of Python course available on CourseGalaxy.com
class Fraction:
def __init__(self,nr,dr=1):
self.nr = nr
self.dr = dr
if self.dr < 0:
self.nr *= -1
self.dr *= -1
def show(self):
print(f'{self.nr}/{self.dr}')
def multiply(self,other):
if isinstance(other,int):
other = Fraction(other)
return Fraction(self.nr * other.nr , self.dr * other.dr)
def add(self,other):
if isinstance(other,int):
other = Fraction(other)
return Fraction(self.nr * other.dr + other.nr * self.dr, self.dr * other.dr)
f1 = Fraction(2,3)
f1.show()
f2 = Fraction(2,-3)
f2.show()
f3 = Fraction(-5,-6)
f3.show()