Tuesday, 8 April 2014

/*write a program of Evaluation of postfix expression*/
#include<stdlib.h>
#include<stdio.h>
int priority(char);
int isoperator(char);
int push(int);
char pop();
int evaluation(char arr[],int);
int 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;
       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]);

   printf("\n evaluation is-> ");
   evaluation(postfix,j);
   printf("%d",stack[0]);
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(int 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;
}

int evaluation(char arr[],int size)
{
       int a,i,v1,v2;
       for(i=0;i<size;i++)
       {
              a=isoperator(arr[i]);
              if(a==0)
              {
                     if(arr[i]=='1')
                         push(1);
                     else if(arr[i]=='2')
                                 push(2);
                     else if(arr[i]=='3')
                                 push(3);
                     else if(arr[i]=='4')
                                 push(4);
                     else if(arr[i]=='5')
                                 push(5);
                     else if(arr[i]=='6')
                                 push(6);
                     else if(arr[i]=='7')
                                 push(7);
                     else if(arr[i]=='8')
                                  push(8);
                     else if(arr[i]=='9')
                                 push(9);
                     else
                           printf("wrong equation");
              }
              else
              {
                     v1=pop();
                  v2=pop();
                  if(arr[i]=='+')
                      push(v2+v1);
                  else if(arr[i]=='-')
                     push(v2-v1);
                  else if(arr[i]=='/')
                            push(v2/v1);
                  else if(arr[i]=='*')
                      push(v2*v1);
                  else
                     printf("nothing to do");
              }
       }
       return 0;
}


No comments:

Post a Comment