使用堆栈评估中缀表达式时的算术错误评估和 KeyDict 错误;
Arithmetically incorrect evaluation and KeyDict errors when evaluating infix expression using stacks;
我的程序应该计算给定的中缀表达式并计算它们。假设只有代码底部方法调用中的运算符将用于任何给定的中缀表达式,程序将 运行。
from pythonds.basic.stack import Stack
import operator
def infixToValue(expr):
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["≤"] = 1
prec["$"] = 0
opStack = Stack()
valStack = Stack()
ops = {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv, "≤": operator.le}
opList = ["+", "-", "*", "/", "≤"]
tokens = expr.split()
def doOp():
x = int(valStack.pop())
y = int(valStack.pop())
op = ops[opStack.pop()]
valStack.push(op(x, y))
def repeatOps(refOp):
while valStack.size() > 1 and opStack.size() > 0 and prec[refOp] <= prec[opStack.peek()]:
doOp()
for token in tokens:
if token.isdigit():
valStack.push(token)
else:
repeatOps(token)
opStack.push(token)
repeatOps("$")
return valStack.pop()
print(infixToValue('14 - 3 * 2 + 7'))
print(infixToValue('14 ≤ 4 - 3 * 2 + 7'))
print(infixToValue('15 + 16 – 2 + 7 * 3 * 2 – 14'))
当前输出如下:
C:\Users\skarl\AppData\Local\Programs\Python\Python37\python.exe C:/Users/skarl/PycharmProjects/Project02/Root.py
Traceback (most recent call last):
File "C:/Users/skarl/PycharmProjects/Project02/Root.py", line 40, in <module>
-1
print(infixToValue('15 + 16 – 2 + 7 * 3 * 2 – 14'))
True
File "C:/Users/skarl/PycharmProjects/Project02/Root.py", line 32, in infixToValue
repeatOps(token)
File "C:/Users/skarl/PycharmProjects/Project02/Root.py", line 25, in repeatOps
while valStack.size() > 1 and opStack.size() > 0 and prec[refOp] <= prec[opStack.peek()]:
KeyError: '–'
Process finished with exit code 1
-1 似乎是第一个中缀表达式的结果,这在算术上是不正确的,也是我在第一个中缀表达式中引用的第二个表达式上的“-”的 KeyError。哪些缺陷导致我的代码无法正常工作?
您第三次通话中的 –
个角色:
print(infixToValue('15 + 16 – 2 + 7 * 3 * 2 – 14'))
实际上是 Unicode U+2013 字符(破折号)。
如果您将两个破折号替换为 ASCII -
符号,您的代码将可以正常工作:
print(infixToValue('15 + 16 - 2 + 7 * 3 * 2 - 14'))
我的程序应该计算给定的中缀表达式并计算它们。假设只有代码底部方法调用中的运算符将用于任何给定的中缀表达式,程序将 运行。
from pythonds.basic.stack import Stack
import operator
def infixToValue(expr):
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["≤"] = 1
prec["$"] = 0
opStack = Stack()
valStack = Stack()
ops = {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv, "≤": operator.le}
opList = ["+", "-", "*", "/", "≤"]
tokens = expr.split()
def doOp():
x = int(valStack.pop())
y = int(valStack.pop())
op = ops[opStack.pop()]
valStack.push(op(x, y))
def repeatOps(refOp):
while valStack.size() > 1 and opStack.size() > 0 and prec[refOp] <= prec[opStack.peek()]:
doOp()
for token in tokens:
if token.isdigit():
valStack.push(token)
else:
repeatOps(token)
opStack.push(token)
repeatOps("$")
return valStack.pop()
print(infixToValue('14 - 3 * 2 + 7'))
print(infixToValue('14 ≤ 4 - 3 * 2 + 7'))
print(infixToValue('15 + 16 – 2 + 7 * 3 * 2 – 14'))
当前输出如下:
C:\Users\skarl\AppData\Local\Programs\Python\Python37\python.exe C:/Users/skarl/PycharmProjects/Project02/Root.py
Traceback (most recent call last):
File "C:/Users/skarl/PycharmProjects/Project02/Root.py", line 40, in <module>
-1
print(infixToValue('15 + 16 – 2 + 7 * 3 * 2 – 14'))
True
File "C:/Users/skarl/PycharmProjects/Project02/Root.py", line 32, in infixToValue
repeatOps(token)
File "C:/Users/skarl/PycharmProjects/Project02/Root.py", line 25, in repeatOps
while valStack.size() > 1 and opStack.size() > 0 and prec[refOp] <= prec[opStack.peek()]:
KeyError: '–'
Process finished with exit code 1
-1 似乎是第一个中缀表达式的结果,这在算术上是不正确的,也是我在第一个中缀表达式中引用的第二个表达式上的“-”的 KeyError。哪些缺陷导致我的代码无法正常工作?
您第三次通话中的 –
个角色:
print(infixToValue('15 + 16 – 2 + 7 * 3 * 2 – 14'))
实际上是 Unicode U+2013 字符(破折号)。
如果您将两个破折号替换为 ASCII -
符号,您的代码将可以正常工作:
print(infixToValue('15 + 16 - 2 + 7 * 3 * 2 - 14'))