[try Beta version]
Not logged in

 
bind string to function

Feb 20, 2011 at 8:40am
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
Feb 20, 2011 at 9:16am
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?
Feb 20, 2011 at 10:13am
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.
Feb 20, 2011 at 10:42am
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.
Feb 20, 2011 at 11:06am
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.