Guest User

Untitled

a guest
May 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. public class Postfix
  2. {public int priority(char ch)
  3. {if(ch=='^')
  4. return 3;
  5. if(ch=='/'||ch=='*')
  6. return 2;
  7. if(ch=='+'||ch=='-')
  8. return 1;
  9. return 0;
  10. }
  11. public String toPostfix(String in)
  12. {String copy=in+")";
  13. Stack s=new Stack(copy.length());
  14. s.push('(');
  15. int i,l=copy.length();
  16. char ch;
  17. String r="";
  18. for(i=0;i<l;i++)
  19. {ch=copy.charAt(i);
  20. if(Character.isLetter(ch)==true)
  21. r+=ch;
  22. else if(ch=='(')
  23. s.push(ch);
  24. else if(ch==')')
  25. {while(s.seeTop()!='(')
  26. r+=s.popSeeTop();
  27. s.pop();
  28. }
  29. else
  30. {while(priority(ch)<=priority(s.seeTop()))
  31. r+=s.popSeeTop();
  32. s.push(ch);
  33. }
  34. }
  35. return r;
  36. }
Add Comment
Please, Sign In to add comment