0

嗨,我可以寻求帮助吗?是否有中缀到后缀的公式?...我不知道如何转换它们并给出后缀表达式和后缀评估...例如这个 1*2(5+2 )-9/6。

4

2 回答 2

1

按照此过程从 转换infixpostfix

Define a stack
Go through each character in the string
If it is between 0 to 9, append it to output string.
If it is left brace push to stack
If it is operator *+-/ then 
    If the stack is empty push it to the stack
    If the stack is not empty then start a loop:
            If the top of the stack has higher precedence
            Then pop and append to output string
            Else break
        Push to the stack

If it is right brace then
    While stack not empty and top not equal to left brace
    Pop from stack and append to output string
    Finally pop out the left brace.

If there is any input in the stack pop and append to the output string.

阅读此处,以更好地理解带有图表的过程。

于 2012-08-17T08:46:19.857 回答
0

如果您想以不同的符号获得表达式,一种方法是使用二叉树并使用前/中/后遍历进行转换。

于 2012-08-17T08:46:33.770 回答