我正在尝试创建一个计算后缀表达式的程序。例如 “3500 43 12 * 47 2/+ -”。这是我的代码
public static int EvaluatePostfixExpression(String postfixExpr){
Stack s = new Stack();
int result = 0;
String operand = null;
for(int i = 0; i<postfixExpr.length();i++){
if(Character.isDigit(postfixExpr.charAt(i)) == true){
operand = operand + postfixExpr.charAt(i);
if(Character.isDigit(postfixExpr.charAt(i+1)) == false){
s.push(operand);
operand = null;
}
}
if(postfixExpr.charAt(i) == '+'){
result = result + Integer.parseInt((String) s.pop()) + Integer.parseInt((String) s.pop()) ;
}
if(postfixExpr.charAt(i) == '-'){
result = result + Integer.parseInt((String) s.pop()) - Integer.parseInt((String) s.pop()) ;
}
if(postfixExpr.charAt(i) == '*'){
result = result + Integer.parseInt((String) s.pop()) * Integer.parseInt((String) s.pop()) ;
}
if(postfixExpr.charAt(i) == '/'){
result = result + Integer.parseInt((String) s.pop()) / Integer.parseInt((String) s.pop()) ;
}
}
return result;
} //end-EvaluatePostfixExpression
当我尝试运行它时,发生错误。
Exception in thread "main" java.lang.NumberFormatException: For input string: "null12"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
我找不到解决方案。如果有人能提供帮助,那就太好了。
编辑: 我处理了这些错误,我的代码现在可以运行了;
public static int EvaluatePostfixExpression(String postfixExpr){
Stack s = new Stack();
int result = 0;
String operand = "";
for(int i = 0; i<postfixExpr.length();i++){
if(Character.isDigit(postfixExpr.charAt(i)) == true){
operand = operand + postfixExpr.charAt(i);
if(Character.isDigit(postfixExpr.charAt(i+1)) == false){
s.push(operand);
operand = "";
}
}
if(postfixExpr.charAt(i) == '+'){
int x = Integer.parseInt((String) s.pop()) + Integer.parseInt((String) s.pop());
result = result + x ;
s.push(String.valueOf(x));
}
if(postfixExpr.charAt(i) == '-'){
int x = Integer.parseInt((String) s.pop()) - Integer.parseInt((String) s.pop());
result = result + x ;
s.push(String.valueOf(x));
}
if(postfixExpr.charAt(i) == '*'){
int x = Integer.parseInt("" + s.pop()) * Integer.parseInt("" + s.pop());
result = result + x ;
s.push(String.valueOf(x));
}
if(postfixExpr.charAt(i) == '/'){
int x = Integer.parseInt((String) s.pop()) / Integer.parseInt((String) s.pop());
result = result + x ;
s.push(String.valueOf(x));
}
}
return result;
}
但现在结果是错误的。应该是2961,但我得到的是-1952。
最佳答案
稍微重写:
public static int evaluatePostfixExpression(String postfixExpr) {
Stack<Integer> s = new Stack<Integer>();
String[] items = postfixExpr.split(" ");
for (String item : items) {
try {
s.push(Integer.valueOf(item));
} catch (NumberFormatException e) {
Integer value1 = s.pop();
Integer value2 = s.pop();
switch (item) {
case "+":
s.push(value2 + value1);
break;
case "-":
s.push(value2 - value1);
break;
case "*":
s.push(value2 * value1);
break;
case "/":
s.push(value2 / value1);
break;
}
}
}
return s.pop();
}
对此的改进是:
- 假定每个项目都是有效的
Integer
或有效的操作数 - 假设
Stack
在接收操作数时包含 2 个项目 - 应该在返回前检查堆栈是否只有 1 项
关于java - 使用堆栈评估 Postfix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501461/