Infix有postfix的公式吗?

时间:2012-08-17 08:38:18

标签: java computer-science

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

1 个答案:

答案 0 :(得分:1)

按照此程序将infix转换为postfix

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.

Take a read here for a better understanding on the procedure with diagrams.