Tuesday, 8 April 2014

/*write a program to convert  infix expression in postfix expression*/


#include<stdlib.h>
#include<stdio.h>
int priority(char);
int isoperator(char);
int push(char);
char pop();
char stack[10];
static int top=-1;
int main()
{
       int count=0,j=0,i=0,chk;
       char p,infix[20],postfix[20];
printf("enter the infix-> ");
p=getchar();
while(p!='\n')
{
       infix[count]=p;
       //printf("%c",infix[count]);
       count++;
       p=getchar();
}
while(count!=0)
{
       chk=isoperator(infix[i]);
       if(chk==0)
       {
              postfix[j]=infix[i];
              i++;
              j++;
       }
       else if(chk==1)
              {
               if(infix[i]=='(')
                 {
                       push(infix[i]);
                       i++;
                 }
               else
                       {
                                     while(stack[top]!='(')
                                     {
                                          postfix[j]=pop();
                                          j++;
                                     }pop();
                                     i++;
                               }
              }
       else if(chk==2)
            {
                           while(priority(stack[top])>=priority(infix[i]) && top>-1)
                           {
                           postfix[j]=pop();
                           j++;
                           }
                          push(infix[i]);
                           i++;
              }

       else
       printf("nothing to do");
count--;
}

while(top>-1)
{
       postfix[j]=pop();
       j++;
}
printf("\nentered infix is-> ");

 for(count=0;count<i;count++)
     printf("%c",infix[count]);

printf("\n postfix is-> ");

   for(count=0;count<j;count++)
       printf("%c",postfix[count]);
return 0;
}

int isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case ':':return 2;
case '(':
case ')':return 1;
break;
default:return 0;
}
}

int priority (char opt)
{
       switch(opt)
       {
              case '^' : return(4);break;
              case '*' :
              case '/' :return(3);break;
              case '+' :
              case '-' :return(2);break;
              case '(' : return(1);
              default : return (0);
       }
}

int push(char ch)
{
              if(top<10)
              {
                  stack[++top]=ch;
                  //printf(" %c is pushed \n",stack[top]);
              }
                     else
                     {
                           //printf("\n stack overflow...\n");
                           exit(1);
                     }
              return 0;
              }
char pop()
{
       char c;
                     if(top>=0)
                     {
                           c=stack[top];
                         stack[top]='\0';
                         top--;
                     }
                           else{
                                  //printf("\n stack underflow...\n");
                                  exit(1);
                           }
       return c;

}



No comments:

Post a Comment