Placing an equation in a string and solving for it

I am a bit new to C++, but I am trying to make a string variable such as

string fx;
fx = "2*x + 1";

I am wanting to set x to a certain value, lets say x = 1 for now and I want to somehow return 2*1 + 1 which is 3.

To all who is familiar with MATLAB, it is the equivalent to the following code:

a = 1;
fx = 2*x + 1;
subs(fx, x, a);
disp(fx)

Is there a way to do this in C++?
You have to write the code yourself to parse the string and perform the computation.

I suggest looking up "Reverse Polish Notation", converting the string to that, and
solve.
You can see this example implementation of a calculator: http://www.research.att.com/~bs/dc_command_line.c
If you give as input "x = 1\n2*x + 1", it should output "3"
Topic archived. No new replies allowed.