9

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.

user2388026
  • 145
  • 1
  • 6
  • Wouldn't you use `-3 * x` instead of `-3x`? You could use `eval`, but that isn't safe if you're expecting arbitrary user input. – Blender May 16 '13 at 01:52
  • You're right, -3 * X would be the appropriate syntax. Regarding eval, what do you mean by "not safe"? – user2388026 May 16 '13 at 01:57
  • `eval("__import__('os').system('rm -Rf /')")` does just what you think it does. – Blender May 16 '13 at 01:58
  • Thank you, but I don't imagine a User will input "rm -Rf /" when asked for an equation they want to solve. Any chance you could give me an example of its usage? It wouldn't be as simple as eval(scanf("%s"), x=5) right? – user2388026 May 16 '13 at 02:05
  • In Python, it's just `eval('x + 2', {'x': 3})` – Blender May 16 '13 at 02:12

3 Answers3

13

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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

In compiled languages like C, C++, or Java there is no easy way to do this--you basically have to rewrite a whole compiler (or use an external library with an interpreter). This is only trivial in "scripting" languages like Python and Javascript, which have a function (often called "eval()") that evaluates expressions at runtime. This function is often dangerous, because it can also do things like call functions with side effects.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • Thank you for your answer, Mr. Crocker. It looks like I'm in way over my head. My idea was to write a simple program to approximate an integral from a to b using Simpson's and Trapezoid rule. I would then solve the function for the appropriate x-values determined by the length and n, then multiply the answers by the appropriate coefficients, .... But it all depends on being able to calculate the user's function for a given X. So it seems I will have to use a simpler language. Thanks again. – user2388026 May 16 '13 at 02:16
  • Couldn't this be done by implementing a sudo try-catch in C, like described here http://stackoverflow.com/questions/10586003/try-catch-statements-in-c? – user3282276 Sep 29 '14 at 05:16
0

Ffmpeg/libav has a nice simple function evaluator you could use.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711