关于转换答案中缀到后缀的困惑

时间:2019-05-20 04:09:58

标签: data-structures postfix-notation

我正在尝试从互联网上了解从Infix到Postfix的转换 我遇到了2个来源,在此之后我对相同的中缀表达式有不同的答案:

a / b ^ c + d * e / f-g + h

我想知道哪种算法正确

源1:https://youtu.be/IAxCAbcqQFA?t=803,您可以在此处看到答案是

abc ^ / de * + f / g-h +

源2:https://raj457036.github.io/Simple-Tools/prefixAndPostfixConvertor.html

答案是

abc ^ / de * + f / gh +-

1 个答案:

答案 0 :(得分:0)

如果要检查输出,可以自己计算表达式。只需打开编辑器,然后将一行作为堆栈:

答案1:abc ^ / de * + f / g-h +

a:  a
b:  a  b
c:  a  b  c
^:  a  b^c
/:  a/b^c
d:  a/b^c  d
e:  a/b^c  d  e
*:  a/b^c  d*e
+:  a/b^c+d*e
f:  a/b^c+d*e  f
/:  (a/b^c+d*e)/f
g:  (a/b^c+d*e)/f  g
-:  (a/b^c+d*e)/f-g
h:  (a/b^c+d*e)/f-g  h
+:  (a/b^c+d*e)/f-g+h

看起来像是错了。

您可以自己做第二个。这也是错误的。

从infix到postfix的转换也很容易手动完成。您只需按照正确的顺序进行运算符,将arg op arg op arg...更改为arg arg op arg op...,在这里,我使用[]来保存已经转换的子表达式:

a/b^c+d*e/f-g+h
a/[bc^]+d*e/f-g+h
[abc^/]+[de*f/]-g+h
[abc^/de*f/+g-h+]
相关问题