0
我正在编写一个代码,将中缀转换为后缀以进行hw分配 我已经调试过它,但似乎无法解决为什么当它弹出堆栈时它返回“(”而不是“+”为什么堆栈弹出(而不是+
StackInterface<Character> stack = new ArrayStack<Character>();
String postfix = "";
int length = infxEx.length();
for(int i =0; i != length; ++i){
char oneChar =infxEx.charAt(i);
if(oneChar == '('){
stack.push(oneChar);
}else
if(oneChar == '*' || oneChar == '/'|| oneChar == '%'|| oneChar == '+' || oneChar == '-'){
stack.push(oneChar);
//error checking input is int
}
else if(oneChar == ')'){
while (stack.pop() != '(' && !stack.empty()){
char popoff = stack.pop();
postfix = postfix + popoff;
}
}
谢谢!
添加允许我们重现问题的输入会很好。 – fabian
该算法不正确。它不处理运算符优先级。您需要查看Dijkstra调车码算法。 – EJP