Question

I am trying to create a program that takes in infix notation and prints out its postfix form. My problem is that the output is still in its infix form, thus a+b*c is output as a+b*c. I believe my problem lies with my first while loop. My goal in the while loop is that if the character is an operator then while the stack is not empty my priority method is called which should pop and print out operators of greater value, then peek the top of the stack to check priority. I put the stack.push() method outside the while loop. I hoped by doing this that after priority was established or if the stack is empty that the operator would be pushed onto the stack. My while second while loop should pop and print out anything left in the stack. Thanks for any help you guys can give me and I appreciate the time people take to guide a programming greenhorn such as myself. Here is my code thus far:

import java.util.*;

public class PostfixConverter {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    Stack<Character> stack = new Stack<Character>();

    System.out.print("Enter infix expression: ");

    String expression = new String(sc.nextLine());

    for (int i = 0; i < expression.length(); i++) {
        /*
         * ch as a variable in place of expression.charAt(i) just seems like
         * a good idea, ch is easier to write
         */
        char ch = expression.charAt(i);

        /*
         * I hope that this prints out anything that is not a parentheses or
         * an operator
         */
        if (ch != '(' & !(isOperator(ch)))
            System.out.print(ch);

        if (ch == '(') {
            /*
             * This is to push '(' onto the stack
             */
            stack.push(ch);
        } else if (ch == ')') {
            stack.pop();
        }

        /*
         * If the character is an operator, I want to enter into a while
         * loop
         */
        if (isOperator(ch)) {
            /*
             * This while loop should check for operator priority, pop 
             * an operator if it is greater than the operator being              
             * pushed onto the stack then peek at the top of the stack
             */
            while (!(stack.isEmpty())) {
                if (priority(ch) <= priority(stack.peek()))
                    System.out.print(stack.pop());
                stack.peek();
            }
            stack.push(ch);

        }

        while (!(stack.isEmpty())) {
            System.out.print(stack.pop());
        }
    }
}

/*
 * Both of my methods below use a switch statement to check priority and
 * whether a char is an operator
 */

public static int priority(char operator) {

    switch (operator) {
    case '+':
        return 1;
    case '-':
        return 1;
    case '*':
        return 2;
    case '/':
        return 2;
    case '^':
        return 3;
    default:
        return 0;
    }
}

public static boolean isOperator(char operator) {
    switch (operator) {
    case '+':
        return true;
    case '-':
        return true;
    case '*':
        return true;
    case '/':
        return true;
    case '^':
        return true;
    default:
        return false;

    }
}
}
Was it helpful?

Solution

You are printing and clearing the stack for every character of your input expression. That's probably not what you want.

You have this in your parsing loop:

while (!(stack.isEmpty())) {
    System.out.print(stack.pop());
}

Having that bit where it is essentially just prints out the current character (which was just pushed onto an empty stack) then clears the stack (by popping everything until it is empty).

You probably mean to move that outside the loop, after it.

You want to parse the whole expression and build the stack, then print it, so you should do exactly that in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top