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