Tuesday, July 28, 2009

How to evaluate the postfix expression using stack in cprogramming with source code?

it is in data structure .by using stack .please help me by giving me the source code in c language please.

How to evaluate the postfix expression using stack in cprogramming with source code?
/* Program to evaluate an epression entered in postfix form */





#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;math.h%26gt;


#include %26lt;ctype.h%26gt;





#define MAX 50





struct postfix


{


int stack[MAX] ;


int top, nn ;


char *s ;


} ;





void initpostfix ( struct postfix * ) ;


void setexpr ( struct postfix *, char * ) ;


void push ( struct postfix *, int ) ;


int pop ( struct postfix * ) ;


void calculate ( struct postfix * ) ;


void show ( struct postfix ) ;





void main( )


{


struct postfix q ;


char expr[MAX] ;





clrscr( ) ;





initpostfix ( %26amp;q ) ;








printf ( "\nEnter postfix expression to be evaluated: " ) ;


gets ( expr ) ;





setexpr ( %26amp;q, expr ) ;


calculate ( %26amp;q ) ;


show ( q ) ;





getch( ) ;


}





/* initializes data members */


void initpostfix ( struct postfix *p )


{


p -%26gt; top = -1 ;


}





/* sets s to point to the given expr. */


void setexpr ( struct postfix *p, char *str )


{


p -%26gt; s = str ;


}





/* adds digit to the stack */


void push ( struct postfix *p, int item )


{


if ( p -%26gt; top == MAX - 1 )


printf ( "\nStack is full." ) ;


else


{


p -%26gt; top++ ;


p -%26gt; stack[p -%26gt; top] = item ;


}


}





/* pops digit from the stack */


int pop ( struct postfix *p )


{


int data ;





if ( p -%26gt; top == -1 )


{


printf ( "\nStack is empty." ) ;


return NULL ;


}





data = p -%26gt; stack[p -%26gt; top] ;


p -%26gt; top-- ;





return data ;


}





/* evaluates the postfix expression */


void calculate( struct postfix *p )


{


int n1, n2, n3 ;


while ( *( p -%26gt; s ) )


{


/* skip whitespace, if any */


if ( *( p -%26gt; s ) == ' ' || *( p -%26gt; s ) == '\t' )


{


p -%26gt; s++ ;


continue ;


}





/* if digit is encountered */


if ( isdigit ( *( p -%26gt; s ) ) )


{


p -%26gt; nn = *( p -%26gt; s ) - '0' ;


push ( p, p -%26gt; nn ) ;


}


else


{


/* if operator is encountered */


n1 = pop ( p ) ;


n2 = pop ( p ) ;


switch ( *( p -%26gt; s ) )


{


case '+' :


n3 = n2 + n1 ;


break ;





case '-' :


n3 = n2 - n1 ;


break ;





case '/' :


n3 = n2 / n1 ;


break ;





case '*' :


n3 = n2 * n1 ;


break ;





case '%' :


n3 = n2 % n1 ;


break ;





case '$' :


n3 = pow ( n2 , n1 ) ;


break ;





default :


printf ( "Unknown operator" ) ;


exit ( 1 ) ;


}





push ( p, n3 ) ;


}


p -%26gt; s++ ;


}


}





/* displays the result */


void show ( struct postfix p )


{


p.nn = pop ( %26amp;p ) ;


printf ( "Result is: %d", p.nn ) ;


}


______________________________________...





Yes, the code seems a huge one; but look it carefully, u'll get it. For further need, u can mail me.
Reply:source code?????? dude if u need the algo, I can think.. source code for free... u must be insane.. LOOSER


No comments:

Post a Comment