evaluating postfix expression using stacks

gau

Joined
Sep 24, 2006
Messages
1
Reaction score
0
This program should first convert an infix expression to postfix expression then evaluate.

theres no problem in converting, the problem is in the evaluation part, here is the code.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

#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<l;i++)
{
if ( isdigit ( eval ) )
{
Push(eval);
}
else if ( eval == '*' || eval == '+' || eval == '/' || eval == '%' || eval == '-' )
{
a = Pop ( );
b = Pop ( );

switch( eval )
{
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;
}

I can't find whats wrong in the evaluation function, when i type 1+2+3 the answer is -106 T_T

can anyone help me?
 
Joined
Apr 6, 2008
Messages
2
Reaction score
0
In your program the problem occur in Evaluate fuction. In that
Code:
 a = Pop ( );
           b = Pop ( );

The pop function return the char according to prototype char Pop (void );
but the variable you are using in Evaluate function a and b are integer. So the ascii value can be taken.

Example:
Enter an infix expression:1+2
The Postfix expression is:21+
answer:99

Because: a=50,b=49 so answer:50+49=99

I hope it helps 8)
 
Joined
Aug 13, 2009
Messages
4
Reaction score
0
can anyone help me in project "evaluation postfix in c"........simple as possible thanks.......plsssssss....very urgent....
 
Joined
Aug 13, 2009
Messages
4
Reaction score
0
can you help me with my project in school it is just the same of what you posted...evaluation of postfix but in C.......plsssss....i really need it very urgent thanksssss..........i expect you to help me....
 
Joined
Jun 9, 2011
Messages
1
Reaction score
0
please help me............Converting postfix expression into infix expression by using c progm
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top