I'm doing a parsing function in C++, which takes a string and a double as arguments and returns the "value" of the string.
Here's the code :
double evaluate (char * toParse, int length, double x)
{
// Case 'x'
if ((toParse[0] == 'x') &&
(length == 1))
{
return x;
}
// Case value
char * endptr;
double num = strtod(toParse, &endptr);
if(endptr - toParse == length)
{
return num;
}
// Parsing
int nBrackets = 0;
for (int i = 0; i < length; i++)
{
if (toParse[i] == '(')
{
nBrackets++;
}
else if (toParse[i] == ')')
{
nBrackets--;
}
// Remove brackets.
double _x = (toParse[0] == '(' && toParse[i-1] == ')' ) ?
evaluate(&toParse[1], i-2, x) : evaluate(toParse, i, x);
double _y = (toParse[i+1] == '(' && toParse[length-1] == ')' ) ?
evaluate(&toParse[i+2], length - (i+1) - 2, x) : evaluate (&toParse[i+1] , length - (i+1), x);
// Supports +, -, * and /
if (nBrackets == 0 &&
toParse[i] == '+')
{
return _x + _y;
}
else if (nBrackets == 0 &&
toParse[i] == '-')
{
return _x - _y;
}
else if (nBrackets == 0 &&
toParse[i] == '*')
{
return _x * _y;
}
else if (nBrackets == 0 &&
toParse[i] == '/')
{
return _x / _y;
}
}
return 0.;
}
int main()
{
cout << evaluate("((4*x)+7)-x", 11, 5.) << endl;
// Outputs 22, which sounds correct.
return 0;
}
It's far from being flawless (no priority on operators, doesn't work if the string contains too much brackets, etc.), but I want to remove the double x argument, and to work on functions directly. (because I want to plot the function, and if I don't work on functions, I will have to parse the same string for each value of x...)
Is it possible ? I mean, doing something like :
double (double) operator+ (double f(double), double g(double))
{
double h (double x)
{
return f(x)+g(x);
}
return h;
}
But it doesn't work, of course. Any ideas ? (class, etc.)
Thanks.