This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractions.py
75 lines (70 loc) · 2.16 KB
/
fractions.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''
Created on Mar 27, 2012
@author: msarialp
'''
def gcd(n, d):
while n != d:
if n > d:
n = n - d
else:
d = d - n
return n
class Fraction:
def __init__(self, n, d):
self.num = int(n / gcd(abs(n), abs(d)))
self.denom = int(d / gcd(abs(n), abs(d)))
if self.denom < 0:
self.denom = abs(self.denom)
self.num = -1*self.num
elif self.denom == 0:
raise ZeroDivisionError
def __add__(self, other):
return Fraction(self.num*other.denom + self.denom*other.num, self.denom*other.denom)
def __sub__(self, other):
return Fraction(self.num*other.denom - self.denom*other.num, self.denom*other.denom)
def __mul__(self, other):
return Fraction(self.num*other.num, self.denom*other.denom)
def __div__(self, other):
return Fraction(self.num*other.denom, self.denom*other.num)
def __str__(self):
if type(self) is tuple:
if self[1] < 0:
return '%s/%s' %(self[0], -1*self[1])
else:
return '%s/%s' %(self[0], self[1])
elif self.denom == 1:
return str(self.num)
else:
return '%s/%s' %(self.num, self.denom)
def __cmp__(self, arg):
if self > arg:
return -1
elif self == arg:
return 0
else:
return 1
def __nonzero__(self):
if self != 0:
return True
else:
return False
def __repr__(self):
return "{}/{}".format(self.num, self.denom)
"""
def main():
f1 = Fraction(2,-2)
f2 = Fraction(3,-4)
f3 = Fraction.Add(f1,f2)
f4 = Fraction.Sub(f1,f2)
f5 = Fraction.Mul(f1,f2)
f6 = Fraction.Div(f1,f2)
f8 = 0
print(Fraction.__cmp__(f4,f4),Fraction.__str__(f3),Fraction.__str__(f4),Fraction.__str__(f5),Fraction.__str__(f6), Fraction.__str__(f1), Fraction.__str__(f2))
if Fraction.__nonzero__(f8):
print("It is a fraction")
else:
print("It is not a fraction")
return 0
if __name__ == '__main__':
main()
"""