EVALUATION_PREFIX

EVALUATION OF PREFIX EXPRESSION

HOME
/*…PROGRAM
FOR EVALUATION OF PREFIX

 EXPRESION
SUING STACK AND SELF REFERENTIAL CALSSES..*/

#include<iostream.h>
#include<math.h>
#include<ctype.h>
#include<string.h>

//DECLARATION
OF THE SELFREFEENTIAL CLASSES

class
prefix

{
  
 int data;

  
 prefix *next;

public:
       //MEMBER FUCTIONS

DECLARATION
  
 void evaluate(char []);

  
 void push(int );

  
 int pop();

  
 void show();

}*top;

/*FUNCTION
FOR EVALUATION OF PREFIX EXPRESION*/

void
prefix::evaluate(char *exp)

{
int
res;

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

{
  
 if(isdigit(exp[i]))

  
 {

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

  
 push(item);

  
 }

  
 else

  
 {

  
 int n1,n2;

  
 n1=pop();

  
 n2=pop();

  
 switch(exp[i])

  
 {

  
 case ‘+’:

  
     res=n1+n2;

  
     break;

  
 case ‘-‘:

  
     res=n2-n1;

  
     break;

  
 case ‘*’:

  
     res=n2*n1;

      
     break;

  
 case ‘$’:

  
     res=pow(n2,n1);

  
     break;

  
 case ‘/’:

  
     res=n2/n1;

  
     break;

  
 default:

  
     cout<<“ENTER
CORRECT EXPRESION”;

  
       
 }

  
 push(res);

  
     }

  
 }

}
//FUNCTION
FOR PUSHING DIGITS

void
prefix::push(int ele)

{
  
 prefix *n,*temp;

  
 n=new(prefix);

  
 n->data=ele;

  
 n->next=NULL;

  
 temp=top;

  
 if(top==NULL)

  
     top=n;

  
 else

  
 {

  
     temp=top;

  
     n->next=temp;

  
     top=n;

  
 }

}

//FUNCTION
FOR POPPING DIGITS

int
prefix::pop()

{
  
 int t;

  
 prefix *temp;

  
 

  
 if(top==NULL)

cout<<“LIST
IS EMPTY:”;

  
 else

  
 {

  
 temp=top;

  
 t=top->data;

  
 top=temp->next;

  
 delete temp;

  
 }

  
 return(t);

}

//FUNCTION
FOR SHOWING OUTPUT

void
prefix::show()

{
  
 int m=pop();

cout<<“AFTER
EVALUATION THE

       
VALUE OF THE EXPRESION  ->”;

  
 cout<<m;

}
void
main()//MAIN()
FUNCTION STARTS

{
  
 char x[22],*z;

  
 top=NULL;

prefix
p;//OBJECT

CREATION
  
cout<<“ENTER THE PREFIX EXPRESION”;

   
cin>>x;

z=strrev(x);//REVERSING OF THE

STRING
  
 p.evaluate(z);

  
 p.show();

}//END OF MAIN()
//END
OF PROGRAM

/*OUTPUT:
ENTER
THE PREFIX EXPRESION+1*23

AFTER
EVALUATION THE

 
VALUE OF THE EXPRESION  ->7

Press
any key to continue

ENTER
THE PREFIX EXPRESION+2-$24*52

AFTER
EVALUATION THE VALUE

    
OF THE EXPRESION  ->-4

Press any key to

continue*/

HOME

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: