Data structures 关于将答案中缀转换为后缀的混淆

Data structures 关于将答案中缀转换为后缀的混淆,data-structures,postfix-notation,Data Structures,Postfix Notation,我试图从互联网上了解中缀到后缀的转换 我遇到了两个来源,以下是我对同一中缀表达式的不同答案: a/b^c+d*e/f-g+h 我想知道哪个算法是正确的 来源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

我试图从互联网上了解中缀到后缀的转换 我遇到了两个来源,以下是我对同一中缀表达式的不同答案:

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

我想知道哪个算法是正确的

来源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
资料来源2:

答案是


abc^/de*+f/gh+-

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

答复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
看来那个错了

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

从中缀到后缀的转换也很容易手动完成。只需按正确的顺序执行运算符,将
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+]

同样对于中缀表达式a+b-(cd)/e^(f-g)^h,计算器的答案是ab+cdefg-h^/-但是,按照视频的算法得到的答案是ab+cd*efg-^h^/-我认为两者都是对的,它们都是错的<代码>a/b^c+d*e/f-g+h应评估为
(a/(b^c)+(d*e/f))-g+h
。上面两个例子都在做
(a/(b^c)+(d*e)/f)-g+h
。请看。实际上,第一个来源似乎是正确的。复制结果时出错。