Advertisement
Kulas_Code20

ToPostfix

Oct 2nd, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. public String convertToPostfix() {
  2.         MyStack stack = new MyStackLinked();
  3.         char c;
  4.         StringBuffer postfix = new StringBuffer(infix.length());
  5.         char symbol;
  6.        
  7.         for(int i=0;i<infix.length();++i) {
  8.             symbol = infix.charAt(i);
  9.            if (Character.isDigit(symbol)) {
  10.                 postfix.append(symbol).append(" ");
  11.            }
  12.             else if (symbol=='(') {
  13.                 stack.push(symbol);
  14.             }
  15.             else if (symbol==')')
  16.             {
  17.                 while ((char)stack.peek() != '(') {
  18.                     postfix.append((char)stack.pop()).append(" ");
  19.                 }
  20.                 stack.pop();       
  21.             }
  22.             else{
  23.                  while (!stack.isEmpty() && !((char)stack.peek()=='(') && isLowerPrecedence(symbol) <= isLowerPrecedence((char)stack.peek()))
  24.                      postfix.append((char)stack.pop()).append(" ");
  25.                 stack.push(symbol);
  26.             }
  27.         }
  28.         while (!stack.isEmpty())
  29.             postfix.append((char)stack.pop()).append(" ");
  30.         return postfix.toString();
  31.     }// end convertToPostfix
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement