-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpn.py
executable file
·56 lines (51 loc) · 1.43 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
53
54
55
56
#!/usr/bin/env python3
import operator
import readline
from termcolor import colored,cprint
operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
'^': operator.pow,
'%': operator.mod,
'<<': operator.lshift,
'>>': operator.rshift,
}
def calculate(myarg):
stack = list()
for token in myarg.split():
try:
token = int(token)
stack.append(token)
except ValueError:
function = operators[token]
arg2 = stack.pop()
arg1 = stack.pop()
result = function(arg1, arg2)
stack.append(result)
#print(stack)
if len(stack) != 1:
raise TypeError("Too many parameters")
return stack.pop()
def main():
while True:
arg=input("rpn calc> ")
cprint("Input: ",end='')
for token in arg.split():
try:
token=int(token)
if token<0:
cprint(str(token),'red',attrs=['bold'],end=' ')
else:
cprint(str(token),end=' ')
except ValueError:
if token=='+' or token=='-' or token=='*' or token=='/':
cprint(token,'green',end=' ')
else:
cprint(token, 'blue', end=' ')
result = calculate(arg)
print('')
print("Result: ", result)
if __name__ == '__main__':
main()