如何使用math.pow()在Expression类中进行计算?

时间:2016-10-27 18:38:42

标签: java math infix-notation postfix

在我的程序中,我编写了一个evalPostfix()方法,该方法也使用了我的applyOperator()方法。

然而,我需要实现的目标之一是使用math.pow()进行求幂,以进行计算。我还需要将最终结果转换回int。

我不确定如何开始,以及如何将Math.pow()实现到我的Expression类中的evalPostfix()方法中。

这是我的代码:

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;
         default:
            return -1;
      }
   }

   public static String toPostfix(String infixExpr) {
      StringBuilder output = new StringBuilder();
      Stack<String> operators = new ArrayStack<>();        
      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 void main(String[] args) {
      System.out.println(rank("/"));
      String infix = ")a * b( * c + d ^ e / f";
      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));
      }
   }

1 个答案:

答案 0 :(得分:1)

我不确定我是否正确理解了您,但如果您尝试使用某个变量来计算数字的结果,为什么不使用Math.pow(在Java中加倍a,双b)?