Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infix to postfix not working as expected [duplicate]

Tags:

java

I have this homework in Java where I have to convert an infix string without parenthesis to a postfix string. I've been tinkering with the code from two days but I haven't been able to catch the bug. Here's my code.

public class itp
{
    String exp, post;
    double res;
    int l;
    stack st;

    public itp(String s)
    {
        exp = s;
        post = "";
        l = exp.length();
        st = new stack(l);
        conv();
        calc();
        System.out.println("The postfix notation of "+exp+" is "+post);
        System.out.println("The result of "+exp+" is "+res);
    }

    public void conv()
    {
        char ch = ' ';
        char pre = ' ';
        for(int i =0;i<l;i++)
        {
            ch = exp.charAt(i);
            if("+-*/".indexOf(ch)==-1)post = post + ch;
            else
            {
                    pre = st.pop();
                    if(val(ch)>=val(pre))
                    {
                        st.push(pre);
                        st.push(ch);
                    }
                    else
                    {
                        while((val(ch)<=val(pre))&&(pre!='$'))
                        {
                            post = post + pre;
                            pre = st.pop();
                        }
                        st.push(ch);
                    }
            }
        }
        for(pre = st.pop();pre!='$';pre = st.pop())
        {
            post = post + pre;
        }
    }

    public void calc()
    {
        res = 0.0;
    }

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

Here, the variables are as follows:

  • st is a character stack
  • ch is the current character
  • pre is the topmost char on the stack
  • exp is the input infix expression
  • post is the output postfix expression

The pop() methods works as expected except when the stack is empty, where it will return $. The function val() takes a char input and returns 4 for /, 3 for *. 2 for -. 1 for +. The integer l holds the length of exp.

It works well in most cases except when I give it a string like a*b-b*c+c*d-d*e where it outputs ab*bc*-cd*de*- which is the expected output without a + in the end.

Any advice would be much appreciated. This bug is making me crazy!

Here's the entire code:

public class itp
{
String exp, post;
double res;
int l;
stack st;

public itp(String s)
{
    exp = s;
    post = "";
    l = exp.length();
    st = new stack(l);
    conv();
    calc();
    System.out.println("The postfix notation of "+exp+" is "+post);
    System.out.println("The result of "+exp+" is "+res);
}

public void conv()
{
    char ch = ' ';
    char pre = ' ';
    for(int i =0;i<l;i++)
    {
        ch = exp.charAt(i);
        if("+-*/".indexOf(ch)==-1)post = post + ch;
        else
        {
                pre = st.pop();
                if(val(ch)>=val(pre))
                {
                    st.push(pre);
                    st.push(ch);
                }
                else
                {
                    while((val(ch)<=val(pre))&&(pre!='$'))
                    {
                        post = post + pre;
                        pre = st.pop();
                    }
                    st.push(ch);
                }
        }
    }
    for(pre = st.pop();pre!='$';pre = st.pop())
    {
        post = post + pre;
    }
}

public void calc()
{
    res = 0.0;
}

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

here's the stack class:

public class stack
{
char[] a;
int top,size;

public stack(int s)
{
    size = s;
    a = new char[size];
    top = -1;
}

public void push(char el)
{
    a[++top] = el;
}

public char pop()
{
    if(empty()) return '$';
    else return a[top--];
}

public boolean empty()
{
    return (top == -1);
}
}

Here's the main class

import java.util.Scanner;
class client
{
public static void main(String args[])
{
    System.out.println("Enter the expression");
    Scanner in = new Scanner(System.in);
    itp i = new itp(in.next());
}
}
like image 625
Alok Mysore Avatar asked Sep 26 '13 15:09

Alok Mysore


People also ask

What are the rules for converting infix to postfix?

Rules for the conversion from infix to postfix expressionIf the incoming symbol is '(', push it on to the stack. If the incoming symbol is ')', pop the stack and print the operators until the left parenthesis is found. If the incoming symbol has higher precedence than the top of the stack, push it on the stack.

Which is best for converting infix to postfix?

To convert infix expression to postfix expression, we will use the stack data structure. By scanning the infix expression from left to right, when we will get any operand, simply add them to the postfix form, and for the operator and parenthesis, add them in the stack maintaining the precedence of them.

Which of the following is incorrect with respect to infix to postfix conversion algorithm?

Which of the following statement is incorrect with respect to infix to postfix conversion algorithm? Explanation: Parentheses are not included in the output.

What is the time complexity of infix to postfix?

The time complexity of the above solution to convert infix to postfix notation is O(n), where n is the length of infix expression. Similarly, the space complexity for the conversion is O(n) as it requires equal space to execute the solution using the stack data structure.


1 Answers

First of all the post fix of a*b-b*c+c*d-d*e is not ab*bc*-cd*de*-+ but ab*bc*-cd*+de*-.

Secondly the mistake is in your val() function. It should instead be :

case '$' : return 0;
case '+' : return 1;
case '-' : return 1;
case '*' : return 2;
case '/' : return 2;
default : return -1;

Change it and check. It will certainly work.

like image 182
Bhargav Rao Avatar answered Oct 08 '22 21:10

Bhargav Rao