-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction.py
More file actions
28 lines (22 loc) · 889 Bytes
/
fraction.py
File metadata and controls
28 lines (22 loc) · 889 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
class Fraction:
def __init__(self, n, d):
self.num = n
self.den = d
def __str__(self): # Searched by interpreter when print function is called
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)