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

Discussion in 'C' started by mananarts, Jul 9, 2010.

  1. mananarts

    mananarts New Member

    Joined:
    Jul 9, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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, sizeof expList/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]
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice