-
Notifications
You must be signed in to change notification settings - Fork 0
/
Objects.py
55 lines (49 loc) · 1.96 KB
/
Objects.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
class ComplexNumber(object):
def __init__(self, real=0, imaginary=0):
self.real = float(real)
self.imaginary = float(imaginary)
def __add__(self, other):
return ComplexNumber(self.real + other.real,
self.imaginary + other.imaginary)
def __sub__(self, other):
return ComplexNumber(self.real - other.real,
self.imaginary - other.imaginary)
def __mul__(self, other):
return ComplexNumber(self.real * other.real -
self.imaginary * other.imaginary,
self.real * other.imaginary +
self.imaginary * other.real)
def __div__(self, other):
ab = other.real ** 2 + other.imaginary ** 2
return self * ComplexNumber(other.real / ab,
-other.imaginary / ab)
def __str__(self):
if(self.real != 0 and self.imaginary > 0):
return '{0:.2f} + {1:.2f}i'.format(self.real, self.imaginary)
elif(self.real != 0 and self.imaginary == 0):
return '{0:.2f}'.format(self.real)
elif(self.real != 0 and self.imaginary < 0):
return '{0:.2f} - {1:.2f}i'.format(self.real, -self.imaginary)
elif(self.real == 0 and self.imaginary > 0):
return '{0:.2f}i'.format(self.imaginary)
elif(self.real == 0 and self.imaginary == 0):
return '0.00'
elif(self.real == 0 and self.imaginary < 0):
return '{0:.2f}i'.format(self.imaginary)
class Stack(object):
def __init__(self, a):
self.stack = []
self.stack.extend(a)
def push(self, a):
self.stack.append(a)
def pop(self):
return self.stack.pop()
def top(self):
return self.stack[len(self.stack) - 1]
def __len__(self):
return len(self.stack)
def __str__(self):
s = ''
for x in self.stack:
s += str(x) + ' '
return s