-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack3
45 lines (36 loc) · 1010 Bytes
/
Stack3
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
class Stack():
def __init__(self):
self.list = []
def push(self,item):
self.list.append(item)
def pop(self):
if len(self.list) > 0:
return self.list.pop()
else:
print("list is empty")
def isEmpty(self):
return len(self.list) == 0
def size(self):
return len(self.list)
def postFixeval():
print(" ***Postfix expression calcuation***")
token = input("Enter Postfix expression : ").split()
s = Stack()
for i in token:
if i in ('+','-','*','/'):
op1,op2 = s.pop(),s.pop()
if i == '+':
s.push(op2+op1)
elif i == '-':
s.push(op2-op1)
elif i == '*':
s.push(op2*op1)
elif i == '/':
s.push(op2/op1)
else:
s.push(int(i))
if not s.isEmpty():
print("Answer : ",'{:.2f}'.format(s.pop()))
else:
print("list is empty")
postFixeval()