gpt4 book ai didi

java - Infix转postfix有公式吗?

转载 作者:行者123 更新时间:2023-12-01 15:13:20 25 4
gpt4 key购买 nike

您好,我可以寻求帮助吗?有中缀到后缀的公式吗?...我不知道如何转换它们并给出后缀表达式和后缀评估...例如这个 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.

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

关于java - Infix转postfix有公式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12002498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com