program to implement post fix evaluation in the given code using C

mananarts's Avatar, Join Date: Jul 2010
Newbie Member
without changing the main function i need to write the program as described above in subject line. the algoritham to be follwed is also given at last of this forum.

Can anyone give me the exact solution to it


code:

#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>

//#include "stack_int.h"

int evalExp( int exp[], int size );

int main ( void )
{
int expList[][35] =
{
{25, 7, '*', 14, '-', 6, '+'},
{1, 24, 3, '+', '*', 41, '-'},
{2, 37, 4, '+', '*', 15, '-'},
{8, 30, 9, '-', 6, '/', '+', 4, '*'}
};
int i;

printf( "\nLAB 3: STACKS\n\n"
"This program is a test driver for Problem 15, Page 142,\n"
"that evaluates postfix expressions.\n\n" );
for( i = 0; i < 4; i++ )
{
printf ("%d\n", evalExp(expList[i], sizeof expList[i]/sizeof(int)));
}

printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Leak\n");

return 0;
}

/* =============================================
Evalute a postfix expression
Pre: exp - contains the expression
size - number of elements in the expression
Return: the value of the expression
*/

int evalExp( int exp[], int size )
{
return 0;
}




ALGORITHM:
[IMG]file:///C:/DOCUME%7E1/manan/LOCALS%7E1/Temp/moz-screenshot.png[/IMG]
create Stack
loop ( for each character)
if (its opearand )
push stack ( stack, character)
else
popStack ( stack.op2)
popStack (stack, op1)
operator = character
set value to calculate ( op1,operator, op2)
pushStack (stack,value)
end if
end loop
popStack (Stack,Result)
return (result)
end postFixEvaluate
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
The idea of these exercises is that you learn by programming. You don't learn anything by studying complete samples, otherwise that's what the teacher would have given you in the first place.

Seems the code has mostly been written for you, leaving you with very little to do except change the algorithm syntaax into C, for example "loop (for each character)" might translate to something like
Code:
for (int i=0; i<size; i++)
{
  // ... exp[i] ...
}
So where exactly are you stuck?