0

So I was assigned to create an method which takes in a string in infix notation as a parameter and returns a string in postfix notation.

My code seems to work for most examples I throw at it, but a few inputs cause wacky results.

public class Operations<T> {

public int value(char c){
    switch(c){
    case '(':
    case ')':
        return 3;
    case '*':
    case '/':
    case '%':
        return 2;
    case '+':
    case '-':
        return 1;
    default:
        return 0;   
    }
}

public String infixToPostfix(String infix){

    //Operator stack
    myStack<Character> ops = new myStack<Character>();

    //Postfix string
    String postfix = "";

    //Current char being read
    char c;

    //Marks if paranthesis are being passed in
    boolean flag = false;

    //Iterate through each character to find operators
    for(int i=0; i<infix.length(); i++){
        c = infix.charAt(i);

        //Add operand to postfix and operator to stack
        if(value(c)==0){
            postfix+=c;
        } else if(ops.isEmpty() || (value(c)>value(ops.getTop()) && c!=')') || c=='(') {
            ops.push(c);
        } else if(value(c)<value(ops.getTop()) && c!=')') {
            if(ops.getTop()=='(' || flag) {
                ops.push(c);
                flag = true;
            } else {
                postfix+=ops.pop();
                while(!ops.isEmpty() && value(c)<value(ops.getTop())) {
                    postfix+=ops.pop();
                }
                ops.push(c);
            }
        } else if(c==')') { 
            while(ops.getTop()!='('){
                postfix+=ops.pop();
            }
            ops.pop();
            flag = false;
        } else {
            postfix+=ops.pop();
            ops.push(c);
        }
    }
    while(!ops.isEmpty()){
        postfix+=ops.pop();
    }       

    return postfix;
}

}

for example, the equation: - A * B + (C/D*7) – ( (A%C-8) / (H+F-D))

outputs:

ABCD/7+AC8-%HF+D-/-

while the correct answer is:

ABCD/7+AC%8-HF+D-/

What is causing the problem? Thanks

Sam Nayerman
  • 135
  • 1
  • 1
  • 8
  • 1
    Well, that's asking for someone to debug your code. Without doing that I can tell you having two stacks is perhaps a "better" option. One to push all operands on and the other to help you establish precedence for the operators. – ChiefTwoPencils Oct 15 '15 at 01:15
  • Well, yeah. I'm stuck and don't know what's wrong so that's why I'm asking for assistance. As for the 2 stacks - we can only use one stack and one string to hold the information. – Sam Nayerman Oct 15 '15 at 01:38
  • I understand. The problem is debugging isn't something that can't be done by you the same way we'd have to do it. Set some breakpoints and step through the code and observe what's happening particularly around the areas which are incorrect. Getting help will be easier if you can identify the problem. – ChiefTwoPencils Oct 15 '15 at 01:44
  • something's wrong in your example/ you are missing mults `*` – sve Oct 15 '15 at 03:05
  • ...and the "correct" answer cannot be correct. It's missing other operators in addition to the *s. – ChiefTwoPencils Oct 15 '15 at 04:55

0 Answers0