输入文本分析器,解析字符串以进行计算. [英] Input text analisator, parse string to function and calculate that.

查看:120
本文介绍了输入文本分析器,解析字符串以进行计算.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如文本分析器,我们有输入字符串:"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?

推荐答案

有很多选择:
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆