gaurav_13191 0 Junior Poster in Training

I have the following code for converting a list of t infix expressions into their postfix form:-

#include<stdio.h>
#include<string.h>
#include<ctype.h>

#define MAX 400
struct stack
{
    char arr[MAX];
    int top;
};

void initstack(struct stack *);
void push(struct stack *,char);
char pop(struct stack *);
void convert(struct stack *s,char *);
int priority(char e);

int main()
{
   int t,k=0;
   char str[MAX];
   struct stack s1;
   scanf("%d",&t);          //No of infix expressions to be converted in postfix//
   initstack(&s1);
   while(t)
   {
       t--;
       scanf("%s",str);
       convert(&s1,str);
   }
   return 0;

}

void initstack(struct stack *s)
{
    s->top=-1;
}

void push(struct stack *s,char data)
{
    if(s->top==MAX-1)
     return;
    else
    {
        s->top++;
        s->arr[s->top]=data;
    }
}

char pop(struct stack *s)
{
    char a;
    if(s->top==-1)
     return -1;
    else
    {
        a=s->arr[s->top];
        s->top--;
        return a;
    }
}

void convert(struct stack *s,char *exp)
{
    char op[400],d,p1;
    int i=0,j=0,pr;
    while(*(exp))
    {
        if(*(exp)=='(')
         {
          push(s,*(exp));
          exp++;
         }
        else if(*(exp)=='+'||*(exp)=='-'||*(exp)=='^'||*(exp)=='/'||*(exp)=='*')
         {
           if(s->top==-1)
            {
                push(s,*(exp));
                exp++;
            }
            else
            {
                p1=pop(s);
                if(priority(p1)<priority(*(exp)))
                {
                    push(s,p1);
                    push(s,*(exp));
                    exp++;
                }
                else
                {
                    while(priority(p1)>=priority(*(exp)))
                    {
                      op[i]=p1;
                      i++;
                      p1=pop(s);
                    }
                    push(s,p1);
                    push(s,*(exp));
                    exp++;
                }
            }
          }
          else if(*(exp)==')')
          {
              p1=pop(s);
              while((p1)!='(');
              {
                  op[i]=p1;
                  i++;
                  p1=pop(s);
              }
              exp++;
           }
           else if(isalpha(*(exp)))
           {
               while(isalpha(*(exp)))
               {
               op[i]=*(exp);
               exp++;
               i++;
               }
            }
        }
        while(s->top!=-1)
        {
            d=pop(s);
            op[i]=d;
            i++;
        }
        op[i]='\0';
        while(op[j])
        printf("%c",op[j++]);
        printf("\n");
}


int priority(char e)
{
    if(e=='^')
     return 3;
    else if(e=='*'||e=='/')
     return 2;
    else if(e=='+'||e=='-')
     return 1;
    else
     return 0;
}

I am getting correct outputs for non-bracketed inputs.But for bracketed inputs I don't get any output. I have checked the code many times but couldn't find as to why there is no output for bracketed inputs.Please help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.