cdata-structuresstackruntime-errorpostfix-notation

Code to convert infix to postfix is not working in case of a+(b-c)*d$e/f+g$(h-i) as input


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int n;
int stackfull(int top)
{
    if(top==n)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int stackempty(int top)
{
    if(top==-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int push(char stack[],int top,int data)
{
    if(!stackfull(top))
    {
        top+=1;
        stack[top]=data;
    
    }
    else
    {
        printf("Stack full!");
    }
}
int pop(char stack[],int top)
{
    if(!stackempty(top))
    {
        char data=stack[top];
        top--;
        return data;
    }
    /*else
    {
        printf("Stack empty!");
    }*/
}
void getstr(char a[])
{
    for(int i=0;i<70;i++)
    {
        char c;
        scanf("%c",&c);
        if(c=='\n')
        {
            a[i]='\0';
            break;
        }
        else{
            a[i]=c;
        }
    }
}
int operator(char o)
{
    int r=0;
    if(o=='+'||o=='-'||o=='*'||o=='/'||o=='$')
    {
        r=1;
    }
    return r;
}
int priority(char o)
{
    int r;
    switch(o)
    {
        case '+': r= 1;
        break;
        case '-': r= 1;
        break;
        case '*': r= 2;
        break;
        case '/': r= 2;
        break;
        case '$': r= 3;
    }
    return r;
}
int intopost(char a[],char out[])
{
    int i=0,j=0,top=-1;
    char stk[n];
    while(a[i]!='\0')
    {
        if(operator(a[i]))
        {
            if(stackempty(top))
            {
                push(stk,top,a[i]);
                top++;
            }
            else
            {
                if(stk[top]=='('||priority(a[i])>priority(stk[top]))
                {
                    printf("\t2=%s %s\t",stk,out);
                    push(stk,top,a[i]);
                    top++;
                }
                else if(priority(a[i])<priority(stk[top])||priority(a[i])==priority(stk[top]))
                {
                    printf("\t2=%s %s\t",stk,out);
                    out[j]=pop(stk,top);
                    top--;
                    j++;
                    push(stk,top,a[i]);
                    top++;
                }
            }
        }
        else if(a[i]=='(')
        {
                push(stk,top,a[i]);
                top++;
            }
        else if(a[i]==')')
        {
                while (stk[top]!='(')
                {
                    if(stackempty(top)&&stk[top]=='(')
                    {
                        return -1;
                    }
                    else
                    {    
                        out[j] = pop(stk,top);
                        j++;
                    }
                    top--;
                }
                char ch=pop(stk,top);
                top--;
            }
        else if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z'))
        {
            out[j]=a[i];
            j++;
        }
        i++;
    }
    while(stackempty(top));
    {
        out[j]=pop(stk,top);
        j++;
        top--;
    }
    if(!stackempty(top))
    {
        out[j]=pop(stk,top);
        j++;
        top--;
    }
    
    out[j]='\0';
    j++;
}
int main()
{
    char s[70],o[70],ch;
    int i,f; 
    printf("Enter the expression:");
    getstr(s);
    printf("%s -> ",s);
    n=strlen(s);
    intopost(s,o);
    printf("%s",o);
}

I was writing this program which should convert infix expression to postfix expression but it is not working in case of a+(b-c)*d$e/f+g$(h-i) as input. The output comes out to be abc-de$f/ghi-$+ but the output should be abc-de^*f/+ghi-^+. An asterisk is missing in the code's output.


Solution

  • I found the solution:

    #include<stdio.h>
    int n,top=-1;
    char stk[50];
    int stackfull()
    {
        if(top==n)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    int stackempty()
    {
        if(top==-1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    void push(char data)
    {
        if(!stackfull())
        {
            top++;
            stk[top]=data;
        
        }
        else
        {
            printf("Stack full!");
        }
    }
    char pop()
    {
        if(!stackempty())
        {
            char data=stk[top];
            stk[top]=' ';
            top--;
            return data;
        }
        else
        {
            printf("Stack empty!");
        }
    }
    void getstr(char a[])
    {
        int i;
        for(i=0;i<70;i++)
        {
            char c;
            scanf("%c",&c);
            if(c=='\n')
            {
                a[i]='\0';
                break;
            }
            else{
                a[i]=c;
            }
        }
    }
    int operator(char o)
    {
        int r=0;
        if(o=='+'||o=='-'||o=='*'||o=='/'||o=='$')
        {
            r=1;
        }
        return r;
    }
    int priority(char o)
    {
        int r;
        switch(o)
        {
            case '(': r=0;
            break;
            case '+': r= 1;
            break;
            case '-': r= 1;
            break;
            case '*': r= 2;
            break;
            case '/': r= 2;
            break;
            case '$': r= 3;
        }
        return r;
    }
    int intopost(char a[],char out[])
    {
        int i=0,j=0;
        while(a[i]!='\0')
        {
            if(operator(a[i]))
            {
                if(stackempty())
                {
                    push(a[i]);
                }
                else
                {
                    if(priority(a[i])>priority(stk[top]))
                    {
                        push(a[i]);
                    }
                    else
                    {
                        while(!stackempty() && priority(a[i])<=priority(stk[top]))
                        {
                            if (stk[top]!='('||stk[top]!='['||stk[top]!='{')
                            {
                                out[j] = pop();
                                j++;
                            }
                            else
                            {
                                printf("stk=%s",stk);
                                continue;
                            }
                        }
                        push(a[i]);
                    }
                }
            }
            else if(a[i]=='('||a[i]=='['||a[i]=='{')
            {
                push(a[i]);
            }
            else if (a[i]==')'||a[i]==']'||a[i]=='}') 
            { 
                if(a[i]==')')
                {
                    while (!stackempty() && stk[top] != '(')
                    {
                        out[j] = pop();
                        j++;
                    }
                    pop();
                }
                else if(a[i]==']')
                {
                    while (!stackempty() && stk[top] != '[')
                    {
                        out[j] = pop();
                        j++;
                    }
                    pop();
                }
                else if(a[i]=='}')
                {
                    while (!stackempty() && stk[top] != '{')
                    {
                        out[j] = pop();
                        j++;
                    }
                    pop();
                }
            }
            else if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z'))
            {
                out[j]=a[i];
                j++;
            }
            i++;
        }
        printf("stk=%s",stk);
        while(top!=-1)
        {
            out[j]=pop();
            j++;
        }
        out[j]='\0';
        j++;
    }
    int strlength(char a[])
    {
        int count=0,i;
        for(i=0;i<50;i++)
        {
            if(a[i]!='\0')
            {
                count++;
            }
            else{
                return count;
            }
        }
    }
    int main()
    {
        char s[70],o[70],ch;
        int i,f; 
        printf("Enter the expression:");
        getstr(s);
        printf("%s -> ",s);
        n=strlength(s);
        intopost(s,o);
        printf("%s",o);
    }
    

    Thanks to all for commenting suggestions!