Sergei_Langin

Sergei Smart Calculator Stage 7/8 PostFixConverter

Apr 9th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.ArrayList;
  3. import java.util.Deque;
  4. import java.util.List;
  5.  
  6.  
  7. public class PostFixConverter {
  8.     private String infix; // The infix expression to be converted
  9.     private Deque<Character> stack = new ArrayDeque<Character>();
  10.     private List<String> postfix = new ArrayList<String>(); // To hold the postfix expression
  11.  
  12.     public PostFixConverter(String expression)
  13.     {
  14.         infix = expression;
  15.         convertExpression();
  16.     }
  17.  
  18.     /* The approach is basically, if it's a number, push it to postfix list
  19.      * else if it's an operator, push it to stack
  20.      */
  21.     private void convertExpression()
  22.     {
  23.         // Temporary string to hold the number
  24.         StringBuilder temp = new StringBuilder();
  25.  
  26.         for(int i = 0; i != infix.length(); ++i)
  27.         {
  28.             if(Character.isDigit(infix.charAt(i)))
  29.             {
  30.                 /* If we encounter a digit, read all digit next to it and append to temp
  31.                  * until we encounter an operator.
  32.                  */
  33.                 temp.append(infix.charAt(i));
  34.  
  35.                 while((i+1) != infix.length() && (Character.isDigit(infix.charAt(i+1))
  36.                         || infix.charAt(i+1) == '.'))
  37.                 {
  38.                     temp.append(infix.charAt(++i));
  39.                 }
  40.  
  41.  
  42.                 /* If the loop ends it means the next token is an operator or end of expression
  43.                  * so we put temp into the postfix list and clear temp for next use
  44.                  */
  45.                 postfix.add(temp.toString());
  46.                 temp.delete(0, temp.length());
  47.             }
  48.             // Getting here means the token is an operator
  49.             else
  50.                 inputToStack(infix.charAt(i));
  51.         }
  52.         clearStack();
  53.     }
  54.  
  55.  
  56.     private void inputToStack(char input)
  57.     {
  58.         if(stack.isEmpty() || input == '(')
  59.             stack.addLast(input);
  60.         else
  61.         {
  62.             if(input == ')')
  63.             {
  64.                 while(!stack.getLast().equals('('))
  65.                 {
  66.                     postfix.add(stack.removeLast().toString());
  67.                 }
  68.                 stack.removeLast();
  69.             }
  70.             else
  71.             {
  72.                 if(stack.getLast().equals('('))
  73.                     stack.addLast(input);
  74.                 else
  75.                 {
  76.                     while(!stack.isEmpty() && !stack.getLast().equals('(') &&
  77.                             getPrecedence(input) <= getPrecedence(stack.getLast()))
  78.                     {
  79.                         postfix.add(stack.removeLast().toString());
  80.                     }
  81.                     stack.addLast(input);
  82.                 }
  83.             }
  84.         }
  85.     }
  86.  
  87.  
  88.     private int getPrecedence(char op)
  89.     {
  90.         if (op == '+' || op == '-')
  91.             return 1;
  92.         else if (op == '*' || op == '/')
  93.             return 2;
  94.         else if (op == '^')
  95.             return 3;
  96.         else return 0;
  97.     }
  98.  
  99.  
  100.     private void clearStack()
  101.     {
  102.         while(!stack.isEmpty())
  103.         {
  104.             postfix.add(stack.removeLast().toString());
  105.         }
  106.     }
  107.  
  108.  
  109.     public void printExpression()
  110.     {
  111.         for(String str : postfix)
  112.         {
  113.             System.out.print(str + ' ');
  114.         }
  115.     }
  116.  
  117.  
  118.     public List<String> getPostfixAsList()
  119.     {
  120.         return postfix;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment