Friday, January 25, 2013

C Program for Evaluation of Postfix Expression

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define Max 20
int st[Max], top=-1;

void push(int ch)
{
  if (top == Max-1)
   {
      printf("Stack is full\n");
   }
   else
   {
     top++;
     st[top]=ch;
   }
}

int  pop()
{
   int ch;
     if (top==-1)
      {
            printf("Stack is empty\n");
      }
      else
      {
               ch=st[top];
               top--;
      }
      return ch;
}

void dispstack()
{
  int k;
  printf("stack Content: ");
  for (k=top; k>=0; k--)
  {
    printf("%d, ", st[k]);
  }
  printf("\n");
}


int PostEval(char s[25])
{
  char temp[25];
  int i,val=0,ch1,ch2,j=0;
  i=0; top=-1;
  while (s[i]!='\0')
  {
    /*if operand is countered print it*/
    if ( (s[i]>=48 && s[i]<=57) )
    {
      j=0;
      temp[j]=s[i];
      j++;
      temp[j]='\0';
      push(atoi(temp));
    }
    else
    {
      ch1=pop();
      ch2=pop();
      switch(s[i])
      {
            case '+' :{
                             val=ch2+ch1;
                             break;
                          }
            case '-' :{
                             val=ch2-ch1;
                             break;
                          }
            case '*' :{
                             val=ch2*ch1;
                             break;
                          }
            case '/' :{
                             val=ch2/ch1;
                             break;
                          }
       }
     push(val);
    }
   i++;
  }
  val=pop();
  return val;
}




void main()
{
  char s[25];
  int val;
  clrscr();
  printf("enter a Postfix expression for evaluation\n");
  scanf("%s",s);
  val= PostEval(s);
  printf("Value of Postfix Expression=%d\n", val);
  getch();
}

No comments:

Post a Comment