Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *Infiks to Postfiks
- *Samuel Berkat Hulu
- *Version 5.0 ETS Struktur Data
- *
- */
- class Ifikstopostfiks
- {
- private stack theStack;
- private String input;
- private String output = "";
- public Ifikstopostfiks(String in)
- {
- input = in;
- int stackSize = input.length();
- theStack = new stack(stackSize);
- }
- public String doTrans()
- {
- for(int j=0; j<input.length(); j++)
- {
- char ch = input.charAt(j);
- switch(ch)
- {
- case '+':
- case '-':
- gotOper(ch, 1);
- break;
- case '*':
- case '/':
- gotOper(ch, 2);
- break;
- case '^':
- gotOper(ch, 3);
- break;
- case '(':
- theStack.push(ch);
- break;
- case ')':
- gotParen(ch);
- break;
- default:
- output = output + ch;
- }
- }
- while( !theStack.isEmpty() )
- {
- output = output + theStack.pop();
- }
- return output;
- }
- //--------------------------------------------------------------
- public void gotOper(char opThis, int prec1)
- {
- while( !theStack.isEmpty() )
- {
- char opTop = theStack.pop();
- if( opTop == '(' )
- {
- theStack.push(opTop);
- break;
- }
- else
- {
- int prec2;
- if(opTop=='+' || opTop=='-')
- prec2 = 1;
- else if(opTop == '*' || opTop == '/')
- prec2 = 2;
- else
- prec2 = 3;
- if(prec2 < prec1)
- {
- theStack.push(opTop);
- break;
- }
- else
- output = output + opTop;
- }
- }
- theStack.push(opThis);
- }
- public void gotParen(char ch)
- {
- while( !theStack.isEmpty() )
- {
- char chx = theStack.pop();
- if( chx == '(' )
- break;
- else
- output = output + chx;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement