为什么我的evalPostfix方法不起作用?

时间:2016-10-28 01:17:59

标签: java eval infix-notation

在我的Java程序文件中,我正在尝试编写一个评估后缀表达式的方法。

我也试图为我的Expression类编写一个eval()方法,通过首先将其转换为postfix然后评估后缀来评估中缀表达式。

例如,在main中,如果我写:eval(“1 + 2 * 3”)我应该得到7.

然而,在我的程序中,当我写这个时,它返回的全部是-1。

任何人都可以帮我在evalPostfix()或eval()方法中查明代码中的错误吗?

public class Expression {
   private static final String SPACE = " ";
   private static final String PLUS = "+";
   private static final String MINUS = "-";


   public static int rank(String operator) {
      switch (operator) {
         case "^":       //5
            return 3;
         case "*":
         case "/":
            return 2;
         case PLUS:
         case MINUS:     //2
            return 1;
         case "()":      //6
            return 0;
         default:
            return -1;
      }
   }

   public static boolean isOperator(String token) {     //4
      if (rank(token) > 0){
         return true;
      }
      return false;
   }

   public static int applyOperator(String operator,int op1,int op2){     //7
      switch (operator) {
         case PLUS:
            return op1+op2;
         case MINUS:
            return op1-op2;
         case "*":
            return op1*op2;
         case "/":
            return op1/op2;
         case "^":
            return (int) Math.pow( (double) op1, (double) op2);    //9
         default:
            return -1;
      }
   }

   public static String toPostfix(String infixExpr) {
      StringBuilder output = new StringBuilder();
      Stack<String> operators = new ArrayStack<>();
      Stack<Character> s = new ArrayStack<Character>();    //6
      for (int i = 0; i < infixExpr.length(); i++){
         if(infixExpr.charAt(i) == '(');
            s.push('(');
         if(infixExpr.charAt(i) == ')' && !operators.isEmpty()){
            s.pop();
            if(s.isEmpty()) 
               output.append(s.pop());
            if(s.pop() != '(') 
               operators.pop();
            }
         }

      for (String token: infixExpr.split("\\s+")) {
         if (isOperator(token)) { // operator //4
            // pop equal or higher precedence
            while (!operators.isEmpty() &&
                  rank(operators.peek()) >= rank(token)) {
               output.append(operators.pop() + SPACE);
            }
            operators.push(token);
         } else {               // operand
            output.append(token + SPACE);
         }
      }
      while (!operators.isEmpty()) {
         output.append(operators.pop() + SPACE);
      }
      return output.toString();
   }

    public static int evalPostfix(String infixExpr) {     //8
      Stack <String> s = new ArrayStack<String>();
      String operand = null;
      for(int i = 0; i < infixExpr.length(); i++) {
         if (Character.isDigit(infixExpr.charAt(i)))
             s.push(infixExpr.charAt(i) + "");
         else {
             if (s.size() > 1) {
                int value1 = Integer.parseInt(s.pop());
                int value2 = Integer.parseInt(s.pop());
                s.push(applyOperator(infixExpr.charAt(i) + "", value1, value2) + "");
             }
         }
      }
      return Integer.parseInt(s.pop());
      }

   public static int eval(String infix){
      return evalPostfix(toPostfix(infix));
   }


   public static void main(String[] args) {
      System.out.println(rank("/"));
      String infix = "(a * b) * c + d ^ e / f";
      String stat = "5 * 3";
      System.out.println(toPostfix(infix));
      System.out.print("Using applyOperator method, 7 * 3 = ");
      System.out.println(applyOperator("*", 3, 7));
      System.out.print("Using applyOperator method, 50 + 12 = ");
      System.out.println(applyOperator("+", 50, 12));
      System.out.print("Using Math.pow to perform exponentiation, 6 to the 2nd power is: ");
      System.out.println(applyOperator("^", 6,2));
      System.out.println(eval(stat));
      }
   }

0 个答案:

没有答案