-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathStack.py
85 lines (57 loc) · 1.78 KB
/
mathStack.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
76
77
78
79
80
81
82
83
class Stack:
def __init__(self):
self.numList = []
self.len = 0
def populateList(self, userString):
for char in userString:
if char != ' ':
self.numList.append(char)
self.len += 1
def isEmpty(self):
if self.len == 0:
return True
else:
return False
def pop(self):
tempVal = self.numList.pop()
self.len -= 1
return tempVal
def peek(self):
tempVal = self.numList[-1]
return tempVal
def doMath(stack):
tempVal = 0
total = 0
operator = ''
while stack.isEmpty() != True:
#print(stack.peek())
print (total)
if stack.peek().isdigit():
tempVal = int(stack.pop())
elif (stack.peek() == ' '):
stack.pop()
else:
if stack.peek() == '+':
stack.pop()
#print ("temp:",tempVal)
total += (tempVal + int(stack.pop()))
elif stack.peek() == '-':
stack.pop()
#print("here")
val = (int(stack.peek()) - tempVal)
print ("val:", val)
total += (int(stack.pop()) - tempVal)
elif stack.peek() == '*':
stack.pop()
total += (tempVal * int(stack.pop()))
elif stack.peek() == '/':
stack.pop()
total += (int(stack.pop() /tempVal))
tempVal = 0
print (total)
return total
s1 = Stack()
s1.populateList("5 - 4 - 3")
val = s1.peek()
#print (val)
doMath(s1)