SHARE
TWEET

Untitled

a guest Sep 16th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public static int precedence(String operator) {
  2.         switch (operator) {
  3.         case ("+"):
  4.             return 1;
  5.         case ("-"):
  6.             return 1;
  7.         case ("*"):
  8.             return 2;
  9.         case ("/"):
  10.             return 2;
  11.         case ("^"):
  12.             return 3;
  13.         case ("("):
  14.             return 4;
  15.         }
  16.         return -1;
  17.     }
  18.  
  19.     public static boolean isOperator(String input) {
  20.         return input.matches("[\\+\\-\\*\\/\\(\\)]");
  21.     }
  22.  
  23.     public static String infixToPreFix(String input) {
  24.         Stack<String> operands = new Stack<String>();
  25.         Stack<String> operators = new Stack<String>();
  26.         String[] arr = input.split(" ");
  27.  
  28.         for (int i = 0; i < arr.length; i++) {
  29.  
  30.             if (!isOperator(arr[i])) {
  31.                 operands.push(arr[i]);
  32. //              System.out.println("push " + arr[i]);
  33.                 continue;
  34.             }
  35.  
  36.             if (arr[i].equals("(") || arr[i].equals(")")) {
  37.                 evaluateParentheses(operators, operands, arr[i]);
  38. //              System.out.println("( or )");
  39.                 continue;
  40.             }
  41.  
  42. //          System.out.println("eval " + arr[i]);
  43.             pushOperatorPrefix(operands, operators, arr[i]);
  44.         }
  45.  
  46.         System.out.println(operands);
  47.         return operands.pop();
  48.     }
  49.  
  50.     public static void evaluateParentheses(Stack<String> operators, Stack<String> operands, String input) {
  51.  
  52.         if (input.equals("(")) {
  53.             operators.push(input);
  54.  
  55.         } else if (input.equals(")")) {
  56.  
  57.             while (!operators.isEmpty() && !operators.peek().equals("(")) {
  58.                 String operand2 = operands.pop();
  59.                 String operand1 = operands.pop();
  60.                 String operator = operators.pop();
  61.  
  62.                 operands.push(" " + operator + " " + operand1 + " " + operand2);
  63.             }
  64.  
  65.             // pops (
  66.             operators.pop();
  67.         }
  68.     }
  69.  
  70.     public static void pushOperatorPrefix(Stack<String> operands, Stack<String> operators, String operator) {
  71.        
  72.         while (!operators.isEmpty() && precedence(operator) <= precedence(operators.peek()) && !operators.peek().equals("(")) {
  73.             String op2 = operands.pop();
  74.             String op1 = operands.pop();
  75.             operands.push(" " + operators.pop() + " " + op1 + " " + op2);
  76.         }
  77.         operators.push(operator);
  78.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top