Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving a Variable Equation defined by the User

Answers in C, Python, C++ or Javascript would be very much appreciated. I've read a few books, done all the examples. Now I'd like to write a simple program. But, I already ran into the following roadblock:

My intention is to take an equation from the user and save it in a variable, For example:

-3*X+4 or pow(2,(sin(cos(x))/5))       >  [In valid C Math syntax]

And then calculate the given expression for a certain X-Value. Something like this:

printf("%g", UserFunction(3.2))   // Input 3.2 for X in User's Function and Print Result

Any ideas? For the life of me, I can't figure this out. Adding to my frustration, the solution is likely a very simply one. Thank you in advance.

like image 680
user2388026 Avatar asked May 16 '13 01:05

user2388026


People also ask

What are the 4 steps to solving an equation?

We have 4 ways of solving one-step equations: Adding, Substracting, multiplication and division. If we add the same number to both sides of an equation, both sides will remain equal.

What is an example of an equation with a variable?

The linear equations in one variable is an equation which is expressed in the form of ax+b = 0, where a and b are two integers, and x is a variable and has only one solution. For example, 2x+3=8 is a linear equation having a single variable in it. Therefore, this equation has only one solution, which is x = 5/2.


1 Answers

There isn't a simple way to do this in C but I think muParser may be useful to you, it is written in C++ but has C binding. ExprTk is also an option but looks like it is C++ only, on the plus side it looks much easier to get interesting results with.

Another option may be the Expression Evaluation which is part of Libav. It is in C and the eval.h header has some good descriptions of the interface.

like image 160
Shafik Yaghmour Avatar answered Oct 27 '22 06:10

Shafik Yaghmour