-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrpn.py
More file actions
52 lines (50 loc) · 1.31 KB
/
Copy pathrpn.py
File metadata and controls
52 lines (50 loc) · 1.31 KB
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 python
# A simple reverse polish notation calculator
# Author Dario Clavijo 2018
r = 0
a = 0
b = 0
stack = []
while True:
ops = str(input(">"))
if ops != "":
ops = ops.split()
for op in ops:
stack.append(op)
if op == "*":
a = int(stack[-2])
b = int(stack[-3])
r = a * b
stack.pop()
stack.pop()
stack.pop()
stack.append(r)
print(r)
elif op == "+":
a = int(stack[-2])
b = int(stack[-3])
r = a + b
stack.pop()
stack.pop()
stack.pop()
stack.append(r)
print(r)
elif op == "-":
a = int(stack[-2])
b = int(stack[-3])
stack.pop()
stack.pop()
stack.pop()
r = a - b
stack.append(r)
print(r)
elif op == "/":
a = int(stack[-2])
b = int(stack[-3])
stack.pop()
stack.pop()
stack.pop()
r = a / b
stack.append(r)
print(r)
print(stack)