-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin2post.py
54 lines (50 loc) · 1.83 KB
/
in2post.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
#==================================================
#==> Title: 中缀 --> 后缀
#==> Author: Zhang zhen
#==> Email: hustmatnoble.gmail.com
#==> GitHub: https://github.com/MatNoble
#==> Date: 2/20/2021
#==================================================
# coding:utf-8
def priority(z):
if z in ['×', '*', '/']:
return 2
elif z in ['+', '-']:
return 1
def in2post(expr):
""" :param expr: 中缀表达式
:return: 后缀表达式
Example:
"1+((2+3)×4)-5"
"1 2 3 + 4 × + 5 -"
"""
stack = [] # 存储栈
post = [] # 后缀表达式存储
for z in expr:
if z not in ['×', '*', '/', '+', '-', '(', ')']: # 数字直接输出
post.append(z)
else:
if z != ')' and (not stack or z == '(' or stack[-1] == '('
or priority(z) > priority(stack[-1])): # stack 不空;栈顶为(;优先级大于
stack.append(z) # 运算符入栈
elif z == ')': # 右括号出栈
while True:
x = stack.pop()
if x != '(':
post.append(x)
else:
break
else: # 比较运算符优先级,看是否入栈出栈
while True:
if stack and stack[-1] != '(' and priority(z) <= priority(stack[-1]):
post.append(stack.pop())
else:
stack.append(z)
break
while stack: # 还未出栈的运算符,需要加到表达式末尾
post.append(stack.pop())
return post
# if __name__ == '__main__':
s = "1+((2+3)×4)-5"
post = in2post(s)
print('后缀表达式: ',post)