document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Tugas : ETS_Soal 2_Infix - Postfix
  3.  *
  4.  * Rafael Asi Kristanto Tambunan
  5.  * 5025201168
  6.  * Teknik Informatika
  7.  */
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Scanner;
  12.  
  13. class Stack <type>{
  14.     private List<type> list;
  15.     private int currentIndex;
  16.  
  17.     public Stack() // constructor
  18.     {
  19.         list = new ArrayList<type>();
  20.         currentIndex = -1;
  21.     }
  22.  
  23.     public void push(type data) // put item on top of stack
  24.     {
  25.         list.add(data);
  26.         currentIndex++;
  27.     }
  28.  
  29.     public type pop() // take item from top of stack
  30.     {
  31.         type removed=list.remove(currentIndex);
  32.         currentIndex--;
  33.         return removed;
  34.     }
  35.  
  36.     public type peek() // peek at top of stack
  37.     {
  38.         return list.get(currentIndex);
  39.     }
  40.  
  41.     public boolean isEmpty() // return size
  42.     {
  43.         if(list.size()==0) return true;
  44.         else return false;
  45.     }
  46.  
  47.     public void clear() // remove all elements in the Stack
  48.     {
  49.         list.clear();
  50.         currentIndex = -1;
  51.     }
  52. }
  53.  
  54. class Transformation{
  55.  
  56.     private static int type(char kar)
  57.     {
  58.         switch (kar)
  59.         {
  60.             case \'+\': case \'-\':
  61.                 return 1;
  62.             case \'*\': case \'/\': case \'%\':
  63.                 return 2;
  64.             case \'^\':
  65.                 return 3;
  66.             default:
  67.                 return -1;
  68.         }
  69.     }
  70.  
  71.     public static void Post(String expression){
  72.        
  73.         Stack <Character> theStack = new Stack(); //to save the operator
  74.         String Post = ""; //to save the Postfix form
  75.  
  76.         for(int i = 0; i < expression.length(); i++){
  77.             char ch = expression.charAt(i);
  78.            
  79.                 if(Character.isLetterOrDigit(ch))
  80.                 {
  81.                     Post += ch;
  82.                 }
  83.                 else if(ch == \'(\')
  84.                 {
  85.                     theStack.push(ch);
  86.                 }
  87.                 else if(ch == \')\')
  88.                 {
  89.                     while(!theStack.isEmpty() && !theStack.peek().equals(\'(\'))
  90.                     {
  91.                         Post+=theStack.pop();
  92.                     }
  93.                     theStack.pop();
  94.                 }
  95.                 else{        
  96.                     while(!theStack.isEmpty()&&type(theStack.peek()) >= type(ch))
  97.                     {
  98.                         Post+=theStack.pop();
  99.                     }
  100.                     theStack.push(ch);
  101.                 }
  102.         }
  103.        
  104.         while(!theStack.isEmpty())
  105.         {
  106.             Post+=theStack.pop();
  107.         }
  108.        
  109.         System.out.println(Post);
  110.         System.out.println();
  111.     }
  112. }
  113.  
  114. public class Infix_Postfix {
  115.  
  116.     public static void main(String[] args) {
  117.         Scanner input = new Scanner(System.in);
  118.        
  119.         String infix;
  120.                
  121.             System.out.print("Enter the Arithmetics : ");
  122.             infix = input.nextLine();
  123.            
  124.             System.out.print("\\n===================================\\n\\n");
  125.            
  126.             System.out.println("Infix Expression : " + infix);
  127.            
  128.             System.out.print("Postfix Expression : ");
  129.             Transformation.Post(infix);      
  130.     }
  131. }
');