Infix to Postfix

僞代碼如下:

1. Scan the Infix Expression from left to right.
2. If the scannned character is an operand, copy it to the Postfix Expression.
3. If the scanned character is left parentheses, push it onto the stack.
4. If the scanned character is right parenthesis, the symbol at the top of the stack is popped off the stack and copied to the Postfix Expression. Repeat until the symbol at the top of the stack is a left parenthesis (both parentheses are discarded in this process).
5. If the scanned character is an operator and has a higher precedence than the symbol at the top of the stack, push it onto the stack.
6. If the scanned character is an operator and the precedence is lower than or equal to the precedence of the operator at the top of the stack, one element of the stack is popped to the Postfix Expression; repeat this step with the new top element on the stack. Finally, push the scanned character onto the stack.
7. After all characters are scanned, the stack is popped to the Postfix Expression until the stack is empty.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章