EVALUATION_POSTFIX

EVALUATIO OF POSTFIX EXPRESSION

HOME
/*….PROGRAM
TO FIND THE VALUE OF THE POSTFIX EXPRESSION.*/


#include<iostream.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
class
post//CLASS
DECLARATION

{
private:
  
 int res,x;

   
char data,*exp;

  
 post *next;

public:
  
 void evaluate(char *);

  
 void push(int);

   
int pop();

   
void show();

}
*top;

/*FUNCTION
FOR EVALUATION OF POSTFIX EXPRESSION*/

void
post::evaluate(char *c)

{
  
 exp=c;

  
 int n1,n2,res,i;

for(i=0;exp[i]!=”;i++)
  
 {

  
   if( isdigit(exp[i]) )

  
   {

  
   int item=exp[i]-‘0’;

  
   push(item);

  
    }

     
else

  
   {

  
 n1=pop();

  
 n2=pop();

  
     
     

       
switch(exp[i])

  
      {

   
     case ‘+’:

  
       
 res=n1+n2;

  
      
     break;

  
  case’-‘:

  
      res=n2-n1;

  
          
break;

  
     case ‘*’:

  
       
 res=n1*n2;

  
       
 break;

       
case ‘/’:

  
       
 res=n2/n1;

  
       
 break;

  
  case ‘$’:

  
       
 res=pow(n2,n1);

  
        
 break;

      
default:

  
cout<<“ENTER CORRECT EXPRESION”;

  
    exit(0);

  
      }  
 

    
push(res);

  
   }

  
 }

}
  
 

//FUNCTOIN
FOR PUSHING DATA

void
post::push(int item)

{
  
 post *n,*temp;

  
 n=new(post);

  
 n->data=item;

  
 n->next=NULL;

  
 if(top==NULL)

  
     top=n;

  
 else

  
 {

  
     temp=top;

  
     n->next=top;

  
     top=n;

  
 }

}

//FUNCTION
FOR SHOWING DATA

void
post::show()

  
 {

  
 cout<<“THE RESULT IS”;

  
 res=pop();

  
 cout<<res;  
     

  
 }

//FUNCTION
FOR POPPING DATA

int
post::pop()

{

  
 post *temp ;

  
 temp=top;

  
 x=temp->data;

  
 top=temp->next;

  
 return(x);

  
 delete temp;

 
 

}

void
main()//MAIN()
FUNCTION STARTS

{
  
 char s[22];

  
 top=NULL;

  
 post k;//OBJECT
CREATION

  
 cout<<“enter the POSTFIX expresion”;

  
 cin>>s;

 
    k.evaluate(s);

  
k.show();

}

/*
OUTPUT:
CASE
1:

enter
the POSTFIX

      
expresion 623+-382/+*2$3+

THE
RESULT IS 52

CASE
2:

enter
the POSTFIX

       
expresion 234*+

THE
RESULT IS 14

 Press
any key to continue

CASE
3:

enter
the POSTFIX expresion 12S+

 ENTER
CORRECT EXPRESION

 
Press any key to continue

*/

HOME

2 Comments »

  1. 1
    chandrakanth Says:

    average
    you need to work more

  2. 2
    harinath Says:

    very good…. n
    very nice work.


RSS Feed for this entry

Leave a comment