bind string to function

I'm making a program where the user can input a string and the result is shows.

For example: If the user inputs cos(0) then the output is 1

There are more functions then cos, in fact, there are a thousand more functions. Is there a way, instead of checking which function the user entered, that I can somehow bind the input to a real c++ function?

hannes
If all your functions are in form "name(argument)" it's simple. First parse the input to split it to the function name "cos" and the argument "0". Then convert the argument to a float.
Have an std::map<std::string, float(*function)(float)> with all the functions you need.

Where did you find 1000 functions?
Thanks for reply. Is there also a more C-like solution?

I had a look at maple, matlab,.. and I found some real nice functions to make myself.
The idea would be the same. Split input into function name and argument, find a function in a table, call it with the argument.

Note: if you want to compute "cos(0)+1" or "exp( sin(1) )", you'll have more work.
Ok. Computing "cos(0)+1" isn't a problem really. I have two algorithms to do that. I only had to know if there is a faster way to call functions.

hannes
Topic archived. No new replies allowed.