**

Wednesday, 26 February 2014

Program to evaluvate postfix expression

//Program to evaluvate postfix expression:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

#define max 20

int s[max],top=0;

void main()
{
        char postfix[max],ch;
        int i,op1,op2,res;
        printf("\n Enter the postfix expn:  ");
        scanf("%s",postfix);
        for(i=0;i<strlen(postfix);i++)
        {
        ch=postfix[i];
        if(isdigit(ch))
                 push(ch-'0');
                 else
                 {
                 op2=pop();
                 op1=pop();
                 switch(ch)
                 {
                 case'+':res=op1+op2;
                 break;
                 case'-':res=op1-op2;
                 break;
                 case'*':res=op1*op2;
                 break;
                 case'/':res=op1/op2;
                 break;
                 case'^':res=op1^op2;
                 break;
                 default:printf("\nInvalid character\n\n");
                 exit(0);
                 }
               
                 push(res);
                 }
                 }
                 printf("Result of above expn is %d\n",pop());
                 }
                 push(int element)
                 {
                 ++top;
                 s[top]=element;
                 }
                
                 int pop()
                 {
                 int element;
                 element=s[top];
                 --top;
                 return(element);
                 }

No comments:

Post a Comment