Friday, April 17, 2009

POSTE


#include
#include
#include
#include
#define MAX 50

char stack[MAX] ;
char infix[MAX] ;
char postfix[MAX] ;
char eval[MAX] ;
char *s, *t ; /*pointers to input and output strings*/
int top; /*Stack top*/

/*Function Prototypes*/
void Initialize (void);
void SetExpression (char *);
char Pop (void );
void Push (char);
int priority (char);
void Convert (void);
int Evaluate(void);

void main( )
{
int m;

clrscr( ) ;
Initialize ( ) ;
printf ( "\nEnter an infix expression: " ) ;
gets ( infix ) ;
SetExpression (infix) ;
Convert( ) ;
printf ( "\nThe Postfix expression is: " ) ;
puts(postfix);
strcpy(eval,postfix);
m=Evaluate( );
printf("answer: %d", m );

getch( ) ;
}

void Initialize (void)
{
top = -1 ;/*Make stack empty*/
}

void SetExpression ( char *str )
{
s = str ;
t = postfix;
}

/* adds operator to the stack */
void Push ( char c )
{
if ( top == MAX - 1 )
printf ( "\nStack is full.\n" ) ;
else
{
top++ ;
stack[top] = c ;
}
}

/* pops an operator from the stack */
char Pop ( void )
{
if ( top == -1 ) /* Stack is empty*/
return -1 ;
else
{
char item = stack[top] ;
top-- ;
return item ;
}
}

int priority(char c)
{
if ( c == '*' || c == '/' || c == '%' )
return 2;
else if ( c == '+' || c == '-' )
return 1;
else
return 0;
}

/* converts the infix expr. to postfix form */
void Convert (void)
{
char x ;

while ( *( s ) )
{ /*Skip white spaces, if any*/
if ( *( s ) == ' ' || *( s ) == '\t' )
{
s++ ;
continue ;
}

if ( isdigit ( *( s ) ) )/*Operands*/
{
while ( isdigit ( *( s ) ) )
{
*( t ) = *( s ) ;
s++ ;
t++ ;
}
}
if ( *( s ) == '(' )/*Opening Parenthesis*/
{
Push ( *( s ) ) ;
s++ ;
}
if ( *( s ) == '*' || *( s ) == '+' || *( s ) == '/' || *( s ) == '%' || *( s )
== '-' ) /*operators*/
{
if ( top != -1 )
{
x = Pop ( ) ;
while ( priority ( x ) >= priority ( *( s ) ) )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
Push( x ) ;
Push ( *( s ) ) ;
}
else Push( *( s ) ) ;
s++ ;
}
if ( *( s ) == ')' )/*Closing Parenthesis*/
{
x = Pop ( ) ;
while ( x != '(' )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
s++ ;
}
}
while ( top != -1 )/*While stack is not empty*/
{
x = Pop ( ) ;
*( t ) = x ;
t++ ;
}
t++ ;
}

int Evaluate(void)
{
int i,l,a,b,q,z;
l=strlen(eval);
for(i=0;i{
if ( isdigit ( eval[i] ) )
{
Push(eval[i]);
}
else if ( eval[i] == '*' || eval[i] == '+' || eval[i] == '/' || eval[i] == '%'
|| eval[i] == '-' )
{
a = Pop ( );
b = Pop ( );

switch( eval[i] )
{
case '+' : q=b+a; break;
case '-' : q=b-a; break;
case '*' : q=b*a; break;
case '/' : q=b/a; break;
}
Push ( q );
}
}
z = Pop ( );
return z;
}

No comments: