-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFraction Class.py
More file actions
55 lines (36 loc) · 1.13 KB
/
Fraction Class.py
File metadata and controls
55 lines (36 loc) · 1.13 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
import math
from math import gcd
class Fraction:
# Create your fraction class here.
def __init__(self, num, den=1):
self.num = num
self.den = den
def simplify(self):
gcd = math.gcd(int(self.num), int(self.den))
self.num /= gcd
self.den /= gcd
def __add__(self, object2):
ansNum = (self.num * object2.den + object2.num * self.den)
ansDen = self.den * object2.den
self.num = ansNum
self.den = ansDen
self.simplify()
def __mul__(self, object2):
self.num *= object2.num
self.den *= object2.den
self.simplify()
def print(self):
print('{}/{}'.format(int(self.num),int(self.den)))
# Driver code goes here.
num1,den1 = map(int, input().split())
fraction1 = Fraction(num1, den1)
queries = int(input())
for i in range(0, queries):
typeOfOperation , num2 , den2 = map(int, input().split())
fraction2 = Fraction(num2, den2)
if (typeOfOperation == 1):
fraction1+fraction2
fraction1.print()
elif (typeOfOperation == 2):
fraction1*fraction2
fraction1.print()