Advertisement
haikid

Untitled

Mar 26th, 2024
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class PostFix {
  4.     public LinkedList<String> inFix;
  5.     public LinkedList<String> postFix;
  6.    
  7.     public static void main(String[] args) {
  8.         PostFix test = new PostFix();
  9.         test.inFix = new LinkedList<String>();
  10.         test.inFix.add("2.1");
  11.         test.inFix.add("/");
  12.         test.inFix.add("3.2");
  13.         test.inFix.add("-");
  14.         test.inFix.add("(");
  15.         test.inFix.add("6.3");
  16.         test.inFix.add("-");
  17.         test.inFix.add("4.2");
  18.         test.inFix.add(")");
  19.        
  20.         // === For testing ===
  21.         //while (!test.inFix.isEmpty()) System.out.print(test.inFix.remove());
  22.        
  23.         test.inFixToPostFix();
  24.        
  25.         // === For testing ===
  26.         //while (!test.postFix.isEmpty()) System.out.print(test.postFix.remove() + " ");
  27.     }
  28.    
  29.     public static boolean isoperator(String arg) {
  30.         return arg.equals("+") || arg.equals("-") || arg.equals("*") || arg.equals("/") || arg.equals("(") || arg.equals(")");
  31.     }
  32.    
  33.     public void inFixToPostFix() {
  34.         if (inFix == null)
  35.             return;
  36.    
  37.         // create ranking
  38.         HashMap<String, Integer> rank = new HashMap<String, Integer>();
  39.         rank.put("*", 2);
  40.         rank.put("/", 2);
  41.         rank.put("+", 1);
  42.         rank.put("-", 1);
  43.    
  44.         postFix = new LinkedList<String>();
  45.         LinkedList<String> operatorStack = new LinkedList<String>();
  46.    
  47.         while (!inFix.isEmpty()) {
  48.             String curr = inFix.remove();
  49.            
  50.             if (!isoperator(curr))
  51.             {
  52.                 postFix.add(curr);
  53.             } else if (curr.equals("(")) {
  54.                 operatorStack.push(curr);
  55.             } else if (curr.equals(")")) {
  56.                 while (!operatorStack.peek().equals("(")) {
  57.                     postFix.add(operatorStack.pop());
  58.                 }
  59.                 operatorStack.remove(); // remove "("
  60.             } else { // it's an operator +,-,*,/
  61.                 int currRank = rank.get(curr);
  62.    
  63.                 if (!operatorStack.isEmpty() && !operatorStack.peek().equals("(") && rank.get(operatorStack.peek()) >= currRank) {
  64.                     while (!operatorStack.isEmpty() && rank.get(operatorStack.peek()) >= currRank) {
  65.                         postFix.add(operatorStack.pop());
  66.                     }
  67.                 }
  68.                 operatorStack.add(curr);
  69.             }
  70.         }
  71.    
  72.         while (!operatorStack.isEmpty()) {
  73.             postFix.add(operatorStack.pop());
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement