1
#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.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • 4
    This seems like a very good time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. For example by using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through code statement by statement while monitoring variables and their values. – Some programmer dude Sep 06 '22 at 14:30
  • 2
    You should brush up your stack implementation to do everything itself. Leaving the task to update stack pointer (`top`) to caller of the stack is an invitation for errors. – Gerhardh Sep 06 '22 at 14:36
  • You should also turn up compiler warnings. For GCC use `-Wall -Wextra -pedantic`. It should show some warning about non-void functions (like `push`, `pop`, ...) that don't return values in all cases. – Gerhardh Sep 06 '22 at 14:37
  • Where do the `^` characters in the output come from? – Ian Abbott Sep 06 '22 at 15:03
  • @IanAbbott I converted it on a website and so the symbol ^ instead of $ is in the output. – Ashutosh Deshmukh Sep 06 '22 at 15:06
  • 1
    The code that handles the `)` character looks dodgy. – Ian Abbott Sep 06 '22 at 15:41
  • Thank you for responding. I have solved the problems. There were problems in the the code which handles `)` and the code which handles operators. – Ashutosh Deshmukh Sep 09 '22 at 15:31

1 Answers1

0

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!