输入文本分析器,解析字符串以进行计算. [英] Input text analisator, parse string to function and calculate that.
问题描述
例如文本分析器,我们有输入字符串:"y =(x ^ 2)/2",我们如何用任何一种语言(C ++,C#,C或Pascal)计算该值?
一些C ++原型:
...
对象=新的string_analisator_object-> set("y =(x ^ 2)/2");
...
双y = 0;
双x = 0;
...
当然我们知道这是一个"x":
...
数组参数= {(例如我们的x = 3.12)};
object-> set(params);
...
双重结果= object-> calculate();
...
所以,也许有人有解决方案?
Text analisator, for example we have input string: "y = (x^2)/2", how can we calculate that in any language(C++, C#, C or Pascal)?
Some C++ prototype:
...
object = new string_analisator_object->set("y = (x^2)/2");
...
double y = 0;
double x = 0;
...
Ofcourse we know that is a "x":
...
array params = { (for example our x = 3.12) };
object->set(params);
...
double result = object->calculate();
...
So, maybe anyone have solutions?
推荐答案
有很多选择:
- Google提供您喜欢的语言(例如
)的即用型表达式评估器 快速搜索"C#中的表达式计算器"的结果 [ ^ ].- 在您的应用程序中嵌入脚本语言(例如
Lua
).- 编写您自己的用于评估表达式的算法.
There are many options:
- Google for a ready-to-go expression evaluator in your favourite language, e.g.
result of a quick search for ''expression evaluator in C#''[^].- Embed a script language (e.g.
Lua
) in your application.- Write your own algorithm for evaluating expressions.
这篇关于输入文本分析器,解析字符串以进行计算.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!