GET ADMISSION in any stream CONTACT :: Mr. JAYDEEP MEHTA :: CONTACT NO :: 9228217183
Get & share knowledge with us... Be a part of GTU-MATERIAL. send study Material at gtumca1@gmail.com with your Name - College name...

Monday, March 7, 2011

postfix

/*   Write a Program for to evaluate postfix expression    */
char postfix[100];
static i,top;
long int stack[20];
void main()
{
  int temp,p;
   clrscr();
   printf("Enter  Postfix Expression : ");
   scanf("%s",postfix);

   for(i=0;postfix[i]!='\0';i++)
    {

      if(postfix[i]=='$' || postfix[i]=='^')
       {
         temp = stack[top-1];
         p = stack[top-2];
         while(temp>1)
          {
            stack[top-2] *=p;
            temp--;
          }
         top--;
       }
      else if(postfix[i] == '+')
       {
         stack[top-2] += stack[top-1];
          top--;
       }
      else if(postfix[i] == '-')
       {
         stack[top-2] -= stack[top-1];
          top--;
       }
      else if(postfix[i] == '*')
       {
         stack[top-2] *= stack[top-1];
          top--;
       }
      else if(postfix[i] == '/')
       {
         stack[top-2] /= stack[top-1];
          top--;
       }
      else if(postfix[i]!=',')
       {
         stack[top] = postfix[i]-48;
          while(postfix[i+1]!=',')
            {
              stack[top] *= 10 + postfix[i+1]-48;
              i++;
            }
         top++;
       }
    }
    printf("\nValue of Postfix Expression : %ld",stack[top-1]);
 getch();
}




0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More