Java 中缀到中缀有公式吗?
嗨,我能寻求一些帮助吗?有中缀到后缀的公式吗?…我不知道如何转换它们,如何给出后缀表达式和后缀求值。。。Java 中缀到中缀有公式吗?,java,computer-science,Java,Computer Science,嗨,我能寻求一些帮助吗?有中缀到后缀的公式吗?…我不知道如何转换它们,如何给出后缀表达式和后缀求值。。。 例如,这个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 oper
例如,这个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.
如果您想获得不同符号的表达式,一种方法是使用二叉树并使用前/中/后顺序遍历进行转换。您查看了我们的教科书吗?我记得早期的CS课程要求我们做一个类似的转换,我们的教科书中概述了算法。那么后缀评估呢?