簡體   English   中英

是否有Postfix的公式?

[英]Is there formula for Infix to postix?

嗨,我可以尋求幫助嗎?是否有后綴表達式的公式?...我不知道如何轉換它們並給出后綴表達式和后綴評估...例如1 * 2(5 + 2) )-9/6。

請按照以下步驟將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.

請在此處閱讀以更好地理解使用圖表的過程。

如果要以不同的符號表示該表達式,一種方法是使用二叉樹並使用前/后/后順序遍歷進行轉換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM