#include<stdio.h>

#include<ctype.h>

int top=-1,stacksize=9;

char st[10];

int push(char value)

{

 if(top==stacksize-1)

 {

  printf("\n stack is full");

  return 0;

 }

 else

 {

  top++;

  st[top]=value;

  return 1;

 } 

}

char pop()

{

 if(top==-1)

 {

 printf("stack is empty");

 //return 1;

 }

 else

  return st[top--];

}

char peek()

{

 if(top==-1)

  {

   //return -1;

  }

 else

  {

   return st[top];

  }

}

int main ()

{

 int j=0,i=0;

 char infix[20],postfix[20],cc,p;

 printf("\n Enter the infix expresssion:");

 scanf("%s",infix);

 while(infix [i]!='\0')

 {

  cc=infix[i];

  if(cc=='(')

  {

   push(cc);

  }

  else if (cc==')')

  {

   p=pop();

   while(p!='(')

   {

    postfix[j]=p;

    j++;

    p=pop();

   }

  }

  else if (isalpha(cc)||isdigit(cc))

  {

   postfix[j]=cc;

   j++;

  }

  else

  {

   p=peek();

   if(p=='('||top==-1)

   {

    push(cc);

   }

   while(p=='+'||p=='-'||p=='*'||p=='/')

   {

    if((p=='+'||p=='-')&&(cc=='*'||cc=='/'))

    {

     push(cc);

     break;

    }

    else

    {

     p=pop();

     postfix[j]=p;

     j++;

     p=peek();

    }

   }

  }

  i++;

 }

 while(top>-1)

 {

  p=pop();

  postfix[j]=p;

  j++;

 }  

 postfix[j]!='\0';

 printf("\n postfix expression is %s\n",postfix);

}


Comments