Converting a string to a F(x)

Hello,
When you make the user input a string like "5X / ( X^2 - 5 )"
How could I make that string into an useble function, so it makes it possible to give the X any value, and then it gives the solution?

I already thought about putting it apart, and make a array of it, but how could I then make the "*" "/" chars into an operator?

Any advise is welcome :)
To decide which operator a char represents, use a switch with each operator char being a case.
but, when you make the string into this array,
how could i make it know the function[2] represents a operator?

char function[] = { 5, +, 10 }
int A = function[0] function[1] fucntion[2];

won't work :/
Last edited on
What do you understand about a switch statement?

http://www.cplusplus.com/doc/tutorial/control/


If you read this, it should be easy to see how to solve your problem.
The biggest challenge is to parse the string and put it into an appropriate format. I don't think using an array is very useful. It's probably better to think about the expressions, and subexpressions.

5X / ( X^2 - 5 ) is division of the two expressions 5X and X^2 - 5. 5X is multiplication of the expressions 5 and X. etc.

Last edited on
i know how a switch statement works. When I use it, would it be possible to return an operator? If yes, how?
Topic archived. No new replies allowed.