desislava_topuzakova

08. Infix to Postfix

Jan 12th, 2022
1,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. // 09. Infix to Postfix
  2. // Implement an algorithm that converts the mathematical expression from infix notation into a postfix notation.
  3. // Postfix notations (also called Reverse Polish Notation) - https://en.wikipedia.org/wiki/Reverse_Polish_notation
  4. package A_StackAndQueue.Exercises;
  5.  
  6. import java.util.*;
  7.  
  8. public class E9 {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         // operator/precedence
  13.         Map<Character, Integer> operators = new HashMap<>();
  14.         operators.put('-', 2);
  15.         operators.put('+', 2);
  16.         operators.put('*', 3);
  17.         operators.put('/', 3);
  18.         operators.put('(', 0);
  19.         operators.put(')', 0);
  20.  
  21.         StringBuffer stringBuffer = new StringBuffer();
  22.         Deque<Character> stack = new ArrayDeque<>();
  23.  
  24.         boolean isOpenBracket = false;
  25.  
  26.         for (String tokens : scanner.nextLine().split("\\s+")) {
  27.  
  28.             char token = tokens.charAt(0);
  29.  
  30.             if (tokens.length() > 1) {
  31.                 stringBuffer.append(tokens + " ");
  32.                 continue;
  33.             }
  34.  
  35.             if (operators.containsKey(token)) {
  36.  
  37.                 // operators for comparing by precedence
  38.                 int a = operators.get(token);
  39.                 int b = stack.isEmpty() ? 0 : operators.get(stack.peek());
  40.  
  41.                 // open bracket
  42.                 if (token == '(') {
  43.                     stack.push(token);
  44.                     isOpenBracket = true;
  45.                 }
  46.  
  47.                 // close bracket
  48.                 else if (token == ')') {
  49.  
  50.                     // pop the operator from the operator stack onto the output queue.
  51.                     while (!stack.isEmpty() && stack.peek() != '(') {
  52.                         stringBuffer.append(stack.pop());
  53.                         stringBuffer.append(" ");
  54.                     }
  55.                     isOpenBracket = false;
  56.  
  57.                     // pop the open bracket
  58.                     if (stack.isEmpty()) {
  59.                         // there are mismatched parentheses.
  60.                         return;
  61.                     } else {
  62.                         stack.pop();
  63.                     }
  64.                 }
  65.  
  66.                 // open bracket flag
  67.                 else if (isOpenBracket) {
  68.                     stack.push(token);
  69.                 }
  70.  
  71.                 // there is an operator at the top of the operator stack with greater precedence
  72.                 else if (a > b) {
  73.                     stack.push(token);
  74.                 }
  75.  
  76.                 // greater or equal  precedence
  77.                 else {
  78.                     // pop operators from the operator stack onto the output queue.
  79.                     while (!stack.isEmpty() && a <= operators.get(stack.peek())) {
  80.                         stringBuffer.append(stack.pop());
  81.                         stringBuffer.append(" ");
  82.                     }
  83.                     // push the operator onto the operator stack.
  84.                     stack.push(token);
  85.                 }
  86.  
  87.             } else {
  88.                 stringBuffer.append(token);
  89.                 stringBuffer.append(" ");
  90.             }
  91.         }
  92.  
  93.         // append operators from stack
  94.         while (!stack.isEmpty()) {
  95.  
  96.             char token = stack.pop();
  97.  
  98.             // there are mismatched parentheses.
  99.             if (token == '(' || token == ')') {
  100.                 return;
  101.             }
  102.  
  103.             stringBuffer.append(token);
  104.             stringBuffer.append(" ");
  105.         }
  106.  
  107.         System.out.println(stringBuffer.toString());
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment