Advertisement
Samuel_Berkat_Hulu

lsdks

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