I am attempting to write a program that evaluates a mathematical expression after parsing it. I have the parser correct, using a string to strip it into tokens and compare it to grammar rules to validate the expression. It should be able to evaluate expressions of type
(3+2)*4+-1= where negative numbers are included. I am having a tough time with the evaluation part.
string result;
string token1;
string token2;
double c=0;
double d=0;
stack<double> nums;
for(int i=0; i<exp.size();i++)
{
token1= exp.at(i);
if(isdigit(exp.at(i)))
{ c= atof (token1.c_str());
nums.push(c);
}
switch(s_mapStringValues[token1]) //
{
case PLUS:
break;
case MINUS:
break;
case MUL:
break;
case DIV:
break;
}
}
Using the idea of pushing the number onto a stack and then popping last two and evaluating when finding an operator does not work when I have two operators following each other as 3*-4. Any ideas around this?