-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path50.backward_calcul.py
63 lines (61 loc) · 1.91 KB
/
50.backward_calcul.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
57
58
59
60
61
62
63
def plus(a,b):
return int(a)+int(b)
def minus(a,b):
return int(a)-int(b)
def multiple(a,b):
return int(a)*int(b)
def divide(a,b):
return int(a)//int(b)
import sys
sys.stdin = open('input.txt','r')
T = int(input())
for testcase in range(1,T+1):
equation = list(map(str, input().split()))
stack = []
operator = ['+','-','*','/']
for letter in equation:
if letter.isdecimal() :
stack.append(letter)
if letter in operator:
if letter == '+':
try:
a = stack.pop()
b = stack.pop()
result = plus(b,a)
stack.append(result)
except:
print('#{}'.format(testcase),'error')
break
if letter == '-':
try:
a = stack.pop()
b = stack.pop()
result = minus(b,a)
stack.append(result)
except:
print('#{}'.format(testcase),'error')
break
if letter == '*':
try:
a = stack.pop()
b = stack.pop()
result = multiple(b,a)
stack.append(result)
except:
print('#{}'.format(testcase),'error')
break
if letter == '/':
try:
a = stack.pop()
b = stack.pop()
result = divide(b,a)
stack.append(result)
except:
print('#{}'.format(testcase),'error')
break
elif letter == '.':
if len(stack) == 1:
print('#{}'.format(testcase),stack[0])
else:
print('#{}'.format(testcase),'error')
break