我有关于使用堆栈评估后缀表达式的问题
for(int i=0;i<postfix.length;i++){
System.out.println("iteration:"+(i+1));
if(postfix[i].equalsIgnoreCase("+") || postfix[i].equalsIgnoreCase("-")
|| postfix[i].equalsIgnoreCase("*") || postfix[i].equalsIgnoreCase("/") ) {
num1 = Float.parseFloat((String)out.pop());
System.out.println("pop:"+num1);
num2 = Float.parseFloat((String)out.pop());
System.out.println("pop:"+num2);
if(postfix[i].equalsIgnoreCase("+")) {
temp = num2+num1;
out.push(temp);
System.out.println("push:"+temp);
}
else if(postfix[i].equalsIgnoreCase("-")) {
temp = num2-num1;
out.push((""+temp));
System.out.println("push:"+temp);
}
else if(postfix[i].equalsIgnoreCase("*")) {
temp = num2*num1;
out.push((""+temp));
System.out.println("push:"+temp);
}
else if(postfix[i].equalsIgnoreCase("/")) {
temp = num2/num1;
out.push((""+temp));
System.out.println("push:"+temp);
}
}
else{
System.out.println("push:"+postfix[i]);
out.push(postfix[i]);
}
}
如果我只在两个数字之间进行操作,例如“2 4 +”,则代码运行良好,但是当它变成“2 4 + 5 +”时,错误突然出现..我什至放了标记..打印在那里检查流量我的代码..谢谢任何帮助?