Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2017
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1.  
  2. import java.util.*;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class _12_InfixToPostfix {
  7.  
  8.     private static final String NUM_OR_VAR = "[a-z1-9]+";
  9.     private static final String OPERATORS = "[*+-/]";
  10.  
  11.     public static void main(String[] args) {
  12.         _12_InfixToPostfix obj = new _12_InfixToPostfix();
  13.         obj.run();
  14.     }
  15.  
  16.     public void run() {
  17.         Scanner sc = new Scanner(System.in);
  18.         ArrayDeque<String> outputQ = new ArrayDeque();
  19.         Stack<Operator> opStack = new Stack();
  20.         String[] input = sc.nextLine().split(" ");
  21.  
  22.         for (int i = 0; i < input.length; ++i) {
  23.             if (Pattern.matches(NUM_OR_VAR, input[i])) {
  24.                 outputQ.add(input[i]);
  25.             } else if (Pattern.matches(OPERATORS, input[i])) {
  26.                 Operator op = new Operator(input[i]);
  27.                 if (!opStack.isEmpty()) {
  28.                     while (opStack.size() > 0
  29.                         && op.getPrecedence() <= opStack.peek().getPrecedence()) {
  30.                         Operator o = opStack.pop();
  31.                         if (!o.symbol.equals("(")) {
  32.                             outputQ.add(o.toString());
  33.                         }
  34.                     }
  35.                 }
  36.                 opStack.push(op);
  37.             } else if (input[i].equals("(")) {
  38.                 opStack.push(new Operator("("));
  39.             } else if (input[i].equals(")")) {
  40.                 while (opStack.size() > 0) {
  41.                     Operator o = opStack.pop();
  42.                     if (!o.symbol.equals("(")) {
  43.                         outputQ.add(o.toString());
  44.                     }
  45.                 }
  46.             }
  47.            
  48.  
  49.         }
  50.         while (opStack.size() > 0 && !opStack.peek().symbol.equals("(")) {
  51.             outputQ.add(opStack.pop().symbol);
  52.         }
  53.  
  54.         System.out.println(String.join(" ", outputQ));
  55.     }
  56.  
  57.     private class Operator {
  58.  
  59.         byte precedence;
  60.         String symbol;
  61.  
  62.         byte getPrecedence() {
  63.             return this.precedence;
  64.         }
  65.  
  66.         Operator(String symbol) {
  67.             this.symbol = symbol;
  68.             switch (symbol) {
  69.                 case "*":
  70.                 case "/":
  71.                     this.precedence = 3;
  72.                     break;
  73.                 case "+":
  74.                 case "-":
  75.                     this.precedence = 2;
  76.                     break;
  77.                 case "(":
  78.                 case ")":
  79.                     break;
  80.             }
  81.         }
  82.  
  83.         @Override
  84.         public String toString() {
  85.             return this.symbol;
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement