Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi can anyone help with my postfix evaluate. Can\'t make it work. #include<iostr

ID: 3601745 • Letter: H

Question

Hi can anyone help with my postfix evaluate. Can't make it work.

#include<iostream>
#include<cctype>
#include<stack>
#include<string>
using namespace std;

int eValuate(int oper1, int oper2, char P);
int evaluatepostfix(std::string exp);
int main()
{


    string e;
    cin>>e;
    int i = evaluatepostfix(e);
    cout << i;
    return 0;


}

int evaluatepostfix(std::string exp)
{
int s = exp.size();
int Operand1, Operand2, result;

stack<int> operatorstack;
char ch;
for(int i = 0; i < s; i++)
{

    ch = exp[i];
     if(isdigit(ch))
     {

        operatorstack.push(ch);


     }
     else
     {
         Operand2 = operatorstack.top();
         operatorstack.pop();

         Operand1 = operatorstack.top();
         operatorstack.pop();

         result = eValuate(Operand1, Operand2, ch);
         operatorstack.push(result);
     }
}
     return result;

}


int eValuate(int oper1, int oper2, char P)
{
    switch(P)
    {
    case '-':
        return (oper2 - oper1);
        break;

    case '+':
        return (oper2 + oper1);
        break;

    case '*':
        return (oper2 * oper1);
        break;

    case '/':
        return (oper2 / oper1);
        break;

    default:
        return 0;
    }
}

Explanation / Answer

#include<iostream>

#include<cctype>

#include<stack>

#include<string>

using namespace std;

/*where ever corecction is made comments are added to that line..Hope it helps you..Thank you*/

/* This program will work only for single digit numbers*/

int eValuate(int oper1, int oper2, char P);

int evaluatepostfix(std::string exp);

int main()

{

    string e;

    cin>>e;

    int i = evaluatepostfix(e);

    cout << i;

    return 0;

}

int evaluatepostfix(std::string exp)

{

               int s = exp.size();

               int Operand1, Operand2, result;

               stack<int> operatorstack;

               char ch;

               for(int i = 0; i < s; i++)

               {

               ch = exp[i];

     if(isdigit(ch))

     {

        operatorstack.push(ch-'0');//ch is character if we directly push it its ASCII value will be pushed

                                                                                                                                       //Therefore we have to subtract it from '0' .

     }

     else

     {

         Operand1 = operatorstack.top();//Operator 1

         operatorstack.pop();

         Operand2 = operatorstack.top();//Operator 2

         operatorstack.pop();

         result = eValuate(Operand1, Operand2, ch);

         operatorstack.push(result);

     }

               }

     return result;

}

int eValuate(int oper1, int oper2, char P)

{

    switch(P)

    {

    case '-':

        return (oper2 - oper1);

        break;

    case '+':

        return (oper2 + oper1);

        break;

    case '*':

        return (oper2 * oper1);

        break;

    case '/':

        return (oper2 / oper1);

        break;

    default:

        return 0;

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote