Advertisement
Samuel_Berkat_Hulu

infixtopostfix

Apr 27th, 2021
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class infixtopostfix here.
  4.  *
  5.  * @author Samuel Berkat Hulu
  6.  * @version 5.0 21-April-2021
  7.  */
  8. public class infixtopostfix
  9. {
  10.     private Stack theStack;
  11.     private String input;
  12.     private Queue queue;
  13.    
  14.     public infixtopostfix(String in)
  15.     {
  16.         input = in;
  17.         int stackSize = input.length();
  18.         int queueSize = input.length();
  19.         queue = new Queue(queueSize);
  20.         theStack = new Stack(stackSize);
  21.     }
  22.    
  23.     //Lakukan terjemahan ke postfix
  24.     public String doTrans()
  25.     {
  26.         for(int j=0; j<input.length(); j++)
  27.         {
  28.             char ch = input.charAt(j);
  29.             switch(ch)
  30.             {
  31.                 case '+':
  32.                 case '-':
  33.                 gotOper(ch, 1);
  34.                 break;
  35.                 case '*':
  36.                 case '/':
  37.                 gotOper(ch, 2);
  38.                 break;
  39.                 case '(':
  40.                 theStack.push(ch);
  41.                 break;
  42.                 case ')':
  43.                 gotParen(ch);
  44.                 break;
  45.                 default:
  46.                 queue.enqueue(ch);
  47.                 break;
  48.             }
  49.         }
  50.         while( !theStack.isEmpty() )
  51.         {
  52.             queue.enqueue(theStack.pop());
  53.         }
  54.         return queue.getString();
  55.     }
  56.    
  57.     public void gotOper(char opThis, int prec1)
  58.     {
  59.         while( !theStack.isEmpty() )
  60.         {
  61.             char opTop = theStack.pop();
  62.             if( opTop == '(' )
  63.             {
  64.                 theStack.push(opTop);
  65.                 break;
  66.             }
  67.             else
  68.             {
  69.                 int prec2;
  70.                 if(opTop=='+' || opTop=='-')
  71.                     prec2 = 1;
  72.                 else
  73.                     prec2 = 2;
  74.                 if(prec2 < prec1)
  75.                 {
  76.                     theStack.push(opTop);
  77.                     break;
  78.                 }
  79.                 else
  80.                     queue.enqueue(opTop);
  81.             }
  82.         }
  83.         theStack.push(opThis);
  84.     }
  85.    
  86.    
  87.     public void gotParen(char ch)
  88.     {
  89.         while( !theStack.isEmpty() )
  90.         {
  91.             char chx = theStack.pop();
  92.             if( chx == '(' )
  93.                 break;
  94.             else
  95.                 queue.enqueue(chx);
  96.         }
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement