| hannes (97) | |
|
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 1There 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 | |
|
|
|
| hamsterman (4227) | |
|
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? | |
|
|
|
| hannes (97) | |
|
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. | |
|
|
|
| hamsterman (4227) | |
|
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. | |
|
|
|
| hannes (97) | |
|
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 | |
|
|
|