使用Stack评估Postfix

时间:2014-12-16 09:35:49

标签: java parsing stack postfix-notation

我正在尝试创建一个评估后缀表达式的程序。例如 “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。

2 个答案:

答案 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个项目

答案 1 :(得分:0)

试试这个:

 String operand = "";

时也改变它
if(Character.isDigit(postfixExpr.charAt(i+1)) == false){
    s.push(operand);
    operand = "";
}