how to parse a symbolic string to a double variable in C++?
suppose I input a string from the keyboard which is like a mathematical function of x . Then I want to write a function that would take a double variable x and the string as its argument and then return the开发者_如何学编程 value by evaluating the string for any particular value of x.
Example
string mystr;
mystr = " x*x + 3*x";
Now I want to write a function
double func( double x, string str)
such that when called as
func( 2, mystr)
it should return the value 10 i.e (2*2 + 2*3)
you can use an algorithm such as this one to convert the string to postfix notation. but whenever you encounter the 'x'
character, push the function argument onto the stack instead.
then, use an algorithm such as this one to evaluate that postfix representation to get the actual result
精彩评论