Jump to content

postfix evaluation

- - - - -

  • Please log in to reply
7 replies to this topic

#1
mayra

mayra

    Newbie

  • Members
  • PipPip
  • 12 posts
hello everyone. i am doing a program that converts mathematical expression(treated as strings) into its postfix notation. everything is doing fine except the division operation. i am using a stack of characters so before the evaluation, i have to subtract the value of '0' to obtain the integer value of the character. here is some of my code:


int evaluate(string sPostfix)

{

    int nCounter, nPop1,  nPop2;

    int nResult;

    CStack stack;


    for(nCounter = 0; nCounter < sPostfix.length(); nCounter++)

    {

        if(isdigit(sPostfix[nCounter]))

        {

            stack.push(sPostfix[nCounter] - '0');

        }

        else if(isOperator(sPostfix[nCounter]))

        {

            nPop1 = stack.pop();

            nPop2 = stack.pop();

            dResult = calculate(nPop1, nPop2, sPostfix[nCounter]);

            stack.push(nResult);

        }

    }

    return nResult;

}

//************************************************************************ 

//

// Function:	calculate

// Purpose:	    calculate 2 operands

// Parameters:  2 operands and the operator

// Return:      integer result of the operation

// Note:        quotients are treated as integers

// 

//************************************************************************


int calculate(char chOperand1, char chOperand2, char chOperator)

{

    int nResult, nOp1, nOp2;

    

    nOp1 = (int) chOperand1;

    nOp2 = (int) chOperand2;


    switch(chOperator)

    {

    case '+':

        nResult = nOp2 + nOp1;

        break;

    case '-':

        nResult = nOp2 - nOp1;

        break;

    case '*':

        nResult = nOp2 * nOp1;

        break;

    case '/':

        nResult = nOp2 / nOp1;;

        break;

    case '^':

        nResult = pow(nOp2, nOp1);

        break;

    case '%':

        nResult = nOp2 % nOp1;

        break;

    }


    return nResult;

}


instead of int, i want the result to be double so as to accommodate division, however things get complicated. can anyone help? thanks.

Edited by TkTech, 15 July 2010 - 11:24 AM.
Added code tags.


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Note: Please use code tags when posting code to make it easier for us to read. Paste your code in, highlight it, and click the # button. VoilĂ ! Readable. :)

This looks like homework. Are you allowed to use STL classes, or do you have to use specific ones? If you can use STL your life will become sunshine and daisies. If not...that's why I'm here.
sudo rm -rf /

#3
mayra

mayra

    Newbie

  • Members
  • PipPip
  • 12 posts
thank you so much for the reply but i have already resolved the problem. our activity was only to convert mathematical expressions treated as strings into its postfix notation. i just extended the activity on its evaluation for practice.

reply appreciated dear. ☺

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
So you're not curious at all, then?
sudo rm -rf /

#5
mayra

mayra

    Newbie

  • Members
  • PipPip
  • 12 posts
what do you mean not curious? i dont get it.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You seemed like you fixed the problem but didn't want to go any further than that.
sudo rm -rf /

#7
mayra

mayra

    Newbie

  • Members
  • PipPip
  • 12 posts
of course i wanted to. i have already tried using stl classes with that activity when you asked me. its good and ive learned a lot from your sugesstion. thanks.

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Oh, ok. Good. As long as I'm of use to someone, I'm happy. :)
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users