C++ 将字符串转换为数学计算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9439295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to mathematical evaluation
提问by Anton
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.
好的,那么我想要做的是使用一个字符串作为输入(例如“16 * 12 + 25”),将其转换为计算机可以理解的数学评估并返回评估值。我可能会自己写这个,但它很可能需要很长时间,最后,除非我想投入更多时间,否则它仍然不会像我希望的那样好。
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?
所以我的问题是,你知道有没有任何脚本、库或 API 可以为 C++ 做到这一点?我找到了一些适用于 java、python 和 .NET 的方法。但我不使用任何这些语言,我希望尽可能长时间地(希望在整个项目中)留在 C++ 中。你有什么好的想法或链接吗?
回答by Anton
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
我找到了我要找的东西!可下载的源是 C++ 和一个 CodeBlocks 项目。你可以在这里找到它: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
Jared 推荐的更复杂的表达式解析器:http: //www.partow.net/programming/exprtk/index.html