-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
33 lines (29 loc) · 1000 Bytes
/
main.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
class postfix :
stack = list()
postfix = ''
def __init__(self, postfix) -> None:
if postfix[0].isdigit() :
self.postfix = postfix.split(',')
else :
self.postfix = postfix.split(',')[::-1]
def calculator(self) -> int:
for char in self.postfix :
if char.isdigit():
self.stack.append(int(char))
else :
a = self.stack.pop()
b = self.stack.pop()
match char :
case '+' :
self.stack.append(b + a)
case '-' :
self.stack.append(b - a)
case '*' :
self.stack.append(b * a)
case '/' :
self.stack.append(b / a)
case '^' :
self.stack.append(b ** a)
return self.stack.pop()
result = postfix('5,3,+,4,/').calculator()
print(result)