-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpn.py
53 lines (44 loc) · 1.14 KB
/
rpn.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
#!/usr/bin/env python3
import operator
from termcolor import colored
def concat(arg1, arg2):
return int(str(arg1) + str(arg2))
def col_print(stk):
print("[ ", end="")
for ele in stk:
if ele in operators:
print(colored(ele, "green"), end=" ]\n")
elif int(ele) < 0:
print(colored(ele, "red"), end=", ")
else:
print(ele, end=", ")
operators = {
'+' : operator.add,
'-' : operator.sub,
'*' : operator.mul,
'/' : operator.truediv,
'**' : operator.pow,
'++' : concat,
}
def calculate(arg):
stack = list()
col_print(arg.split())
for token in arg.split():
try:
token = int(token)
stack.append(token)
except:
fn = operators[token]
arg2 = stack.pop()
arg1 = stack.pop()
result = fn(arg1, arg2)
stack.append(result)
if len(stack) != 1:
raise TypeError("Too many parameters")
return stack.pop()
def main():
while True:
result = calculate(input("rpn calc> "))
print("Result: ", result)
if __name__ == '__main__':
main()