-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1918.py
More file actions
51 lines (43 loc) · 1.18 KB
/
1918.py
File metadata and controls
51 lines (43 loc) · 1.18 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
import sys
import copy
# 깊은복사 조심!!!!!!!
temp = list(sys.stdin.readline().strip())
middle = copy.deepcopy(temp)
operator = ["-", "+", "*", "/"]
stack1 = [] # 알파벳 넣기
stack2 = [] # 연산자 넣기
stack3 = [] # 빼낸거 넣기
i = 0
for el in temp:
if middle[i] == ")":
i += 1
if el == "*" or el == "/":
if (middle[i - 1] != ")") and (middle[i + 1] != "("):
# print("삽입하러 들어옴!")
middle.insert(i + 2, ")")
middle.insert(i - 1, "(")
i += 1
# print("temp=", temp)
# print("el=", el, "i=", i, middle)
i += 1
middle.insert(0, "(")
middle.append(")")
print("middle=", middle)
for i in range(len(middle)):
if middle[i] == ")":
stack3.append(stack2.pop())
while True:
el = stack1.pop()
if el == "(":
break
else:
stack3.append(el)
while len(stack3) != 0:
stack1.append(stack3.pop())
elif middle[i] not in operator:
stack1.append(middle[i])
elif middle[i] in operator:
stack2.append(middle[i])
print("stack1=", stack1)
for el in stack1:
print(el, end="")