我正在尝试创建一个评估后缀表达式的程序。例如 “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。
答案 0 :(得分:1)
稍作重写:
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 :(得分:0)
试试这个:
String operand = "";
时也改变它
if(Character.isDigit(postfixExpr.charAt(i+1)) == false){
s.push(operand);
operand = "";
}