23

Okay, so what I want to do is to use a string as input (for instance "16*12+25"), convert it to a mathematical evaluation that the computer can comprehend and return the evaluated value. I could probably write this myself, but it would most likely take quite a while and in the end, it still wouldn't end up as good as I'd like it to unless I want to put even more time into it.

So my question is, is there any script, library or api that you know can do this for C++? I have found some for both java, python and .NET. But I am not working with any of these languages and I would like to remain within C++ for as long (hopefully throughout the entire project) as possible. Do you have any good ideas or links?

pmr
  • 58,701
  • 10
  • 113
  • 156
Anton
  • 1,435
  • 2
  • 10
  • 21

2 Answers2

22

I found what I was looking for! The downloadable source is C++ and a CodeBlocks project. You can find it here: http://www.speqmath.com/tutorials/expression_parser_cpp/index.html

A far more sophisticated expression parser recommended by Jared: http://www.partow.net/programming/exprtk/index.html

Anton
  • 1,435
  • 2
  • 10
  • 21
6

There is nothing built into C++ for this; all the expression parsing code belongs in the compiler. You will need to use some external library. A quick Google search brings up muParser which looks pretty reasonable.

spencercw
  • 3,320
  • 15
  • 20
  • +1 for muParser. We've been using it in our neural simulator MOOSE without any problem. Its speed is excellent. – Dilawar Apr 28 '16 at 08:53