Conversion : Infix to postfix expression

  /* Program to convert infix expression to postfix expression  */
   
    #include
    #include
    int prec(char ch)
    {
          switch(ch)
          {
                  case '(':return 0;
                  case '^':return 3;
                  case '*':
                  case '/':return 2;
                  case '+':
                  case '-':return 1;
           }
    }
    char pop(char st[],int *top)
    {
        return(st[(*top)--]);
    }
    void push(char st[],int *top,char item)
    {
        st[++(*top)]=item;
    }
    void in_to_post(char infx[], char postfx[])
    {
        int i,j,l,top=-1;
        char st[20],ch,x;
        l=strlen(infx);
        infx[l]=')';
        push(st,&top,'(');
        j=0;
        for(i=0;i<=l;i++)
        {
            ch=infx[i];
            switch(ch)
            {
                case'(':
                    push(st,&top,ch);
                    break;
                case'^':
                    push(st,&top,ch);
                    break;
                case'*':
                case'/':
                case'+':
                case'-':
                    while(prec(ch)<=prec(st[top]))
                    {
                        postfx[j++]=pop(st,&top);
                                       }
                                       push(st,&top,ch);
                                    break;
                            case ')':
                    while((x=pop(st,&top))!='(')
                                      {
                                            postfx[j++]=x;
                                      }
                                      break;
                             default: postfx[j++]=ch;
            }
             }
             postfx[j]='\0';
    }


    main()
    {
        printf("\nProgram to convert infix expression to postfix expression : \n");
        char infx[20],postfx[20];
        printf("\nEnter the infix expression:");
        scanf("%s",infx);
        printf("\nThe infix exp.: %s",infx);
        in_to_post(infx,postfx);
        printf("\n");
        printf("\nThe postfix exp.: %s",postfx);
        printf("\n\n");
    }

Top