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.


Sign In
Create Account


Back to top









