-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfraction.py
31 lines (22 loc) · 869 Bytes
/
fraction.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
class Fraction:
def __init__(self,n,d):
self.num = n
self.den = d
def __str__(self):
return "{}/{}".format(self.num, self.den)
def __add__(self,other):
temp_num = self.num * other.den + other.num * self.den
temp_den = self.den * other.den
return "{}/{}".format(temp_num, temp_den)
def __sub__(self,other):
temp_num = self.num * other.den - other.num * self.den
temp_den = self.den * other.den
return "{}/{}".format(temp_num, temp_den)
def __mul__(self,other):
temp_num = self.num * other.num
temp_den = self.den * other.den
return "{}/{}".format(temp_num, temp_den)
def __truediv__(self,other):
temp_num = self.num * other.den
temp_den = self.den * other.num
return "{}/{}".format(temp_num, temp_den)