0
1.3kviews
Program to evaluate postfix expression using STACK ADT.
1 Answer
0
19views
  • Every character of the post-fix expression is scanned from left to right.

  • If the character encountered is an operand it is pushed onto the stack.

  • However, if an operator is encountered, then the top two values are popped from the stack and the operator is applied on these values.

  • The result is then pushed onto the stack.

Program :

include <stdio.h>

include <conio.h>

define N 100

float st [N];

int top = -1 ;

void push (float st [ ] , float val );

float pop ((float st [ ] );

float evaluate (har exo [ ] );

void main ( )

<

float val ;

chat exp [100];

drscr( ) ;

print f ("\n enter any postfix expression");

f flush (stdin);

gets (exp);

val = evaluate (exp);

print f ("in value of postfix expression = % 2 f " , val );

getch ( );

3

float evaluate (char exp ( ) )

<

int i = 0;

float op1, op2, value;

while (exp [i] ! = '1-')

<

if (is digit ( exp [i] ) )

push (st, (flood) (exp [i] - '0') );

else

<

op2 = pop (st);

op1 = pop (st);

switch ( exp [i])

<

case '+':

value = op1 + op2;

break;

case '1':

value = op1 - op2;

break;

case '*':

value = op1 * op2;

break;

case '/' :

value = op1 / op2;

break;

case ' % ' :

value = (int) op1 % (int) op2;

break;

3

push (st, value);

3

i++;

3

return (pop (st) );

3

void push (float st [ ] , float val)

<

if (top = = N - 1)

print f ("in stack overflow")/

else

<

top + + ;

st [ top] = val;

3

float pop (float st [ ] )

< float val = -1;

of (top = = -1)

print f (") n stack underflow");

else

< ral = st [top];

top - - ;

3 return val;

3

Please log in to add an answer.