-
Notifications
You must be signed in to change notification settings - Fork 0
/
中缀表单转换.py
64 lines (53 loc) · 1.79 KB
/
中缀表单转换.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
64
#!/usr/bin/python
# -*- coding: utf-8 -*-
from pythonds.basic.stack import Stack
import string
"""
操作符对计算顺序的影响:
前缀表达式:+AB
后缀表达式:AB-
中缀表单式:C+(A*B)
! 我们都应该避免中缀表达式,将中缀表达式转后缀表单式
"""
def infixToPostfix(x: str) -> str:
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = list(x)
for token in tokenList:
print(token)
print(postfixList)
print(opStack.items, "\n")
# 从左向右读
if token in string.ascii_uppercase or token in string.digits:
# 读到数字和字母就进列表
postfixList.append(token)
elif token == "(":
# 读到中缀就进栈
opStack.push(token)
elif token == ")":
# 读到右括号就弹出一个左括号
topToken = opStack.pop()
while topToken != "(":
# 如果不是左括号,就加入列表
postfixList.append(topToken)
# 然后继续弹出
topToken = opStack.pop()
else:
# 读到符号,栈不为空,且栈顶符号优先级大于等于此刻的符号
while (not opStack.isEmpty()) and prec[opStack.peek()] >= prec[token]:
# 栈弹出,并且加入list
postfixList.append(opStack.pop())
# 将等级低的加入堆栈中
opStack.push(token)
while not opStack.isEmpty():
# 在判断是否为空,不为空,我们就把剩余栈数据写入list
postfixList.append(opStack.pop())
# 返回 字符串
return " ".join(postfixList)
print(infixToPostfix("A*B+1"))