问题描述
非常感谢C,Python,C ++或Javascript的答案.我读了几本书,做了所有的例子.现在,我想编写一个简单的程序.但是,我已经遇到了以下障碍:
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]
然后为某个X值计算给定的表达式.像这样:
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.
推荐答案
在 C 中没有简单的方法可以做到这一点,但我认为 muParser 可能对您有用,它是用 C ++ 编写的,但具有 C 绑定. ExprTk 也是一个选项,但看起来是 C ++ 仅从正面看,获得有趣的结果要容易得多.
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.
另一个选择可以是表达式评估,它是图书馆.它位于 C 和 eval.h 标头对接口有一些很好的描述.
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.
这篇关于求解用户定义的变量方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!