Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Postfix
- {public int priority(char ch)
- {if(ch=='^')
- return 3;
- if(ch=='/'||ch=='*')
- return 2;
- if(ch=='+'||ch=='-')
- return 1;
- return 0;
- }
- public String toPostfix(String in)
- {String copy=in+")";
- Stack s=new Stack(copy.length());
- s.push('(');
- int i,l=copy.length();
- char ch;
- String r="";
- for(i=0;i<l;i++)
- {ch=copy.charAt(i);
- if(Character.isLetter(ch)==true)
- r+=ch;
- else if(ch=='(')
- s.push(ch);
- else if(ch==')')
- {while(s.seeTop()!='(')
- r+=s.popSeeTop();
- s.pop();
- }
- else
- {while(priority(ch)<=priority(s.seeTop()))
- r+=s.popSeeTop();
- s.push(ch);
- }
- }
- return r;
- }
Add Comment
Please, Sign In to add comment