Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   program to implement post fix evaluation in the given code using C (http://www.go4expert.com/forums/program-implement-post-fix-evaluation-t22665/)

mananarts 9Jul2010 07:36

program to implement post fix evaluation in the given code using C
 
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

xpi0t0s 9Jul2010 14:42

Re: program to implement post fix evaluation in the given code using C
 
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?


All times are GMT +5.5. The time now is 15:33.