如何将CString作为代码行执行。 [英] How can I execute CString as a code line.

查看:95
本文介绍了如何将CString作为代码行执行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数学表达式。我想将它保存在CString中。之后我想将CString作为代码行执行。



数学表达式就像= 3 *(var1 + 10)+ 100

如果var1> 100则var1 = 1否则如果var1< 100 var1 = 2.



var1的值取自某些CSV /文本文件。



假设如果var1 = 90

那么CString strExp = 3 *(2 + 10)+ 100;

然后我想执行这个CString,结果将保存在另一个字符串中。



double结果= XXXX strExp XXXXX //无论什么操作/解析

//结果= 136

解决方案

你不能以任何简单的方式做到这一点。在某些解释性语言中,甚至在某些编译语言中,这是一个基本功能,但C ++不是其中之一。



您将需要创建一些编译器或解释器来解析和执行你的字符串。你可以从这里开始: http://www.codeproject.com/search.aspx?q=C%2b%2b+expression+parser&doctypeid=1%3b2%3b3%3b13%3b14%3b5 [ ^ ]。



替代方式是根本不使用字符串。您可以开发表示表达式树的节点和树本身的特殊类型,您可以使用它们在C ++代码中创建表达式。换句话说,可以开发类似于.NET lambda表达式的东西。但我想我会写一整本书来解释它。你的问题强烈暗示你尚未准备好进行此类工作。



-SA


作为替代方案,您可以在应用程序中嵌入脚本语言,如 Lua ,然后让脚本引擎为您计算表达式(以及更复杂的构造)。我们有一篇关于这个主题的文章:将Lua集成到C ++代码项目 [ ^ ]。 />
另请参阅在堆栈溢出中使用Lua评估数学表达式 [ ^ ]。

 template <   typename     T  >  
void trig_function( )
{
typedef exprtk :: symbol_table < T > symbol_table_t;
typedef exprtk :: expression < T > expression_t;
typedef exprtk :: parser < T > parser_t;

std :: string expression_string =clamp(-1.0,sin(2 * pi * x)+ cos(x / 2 * pi),+ 1.0);

T x;

symbol_table_t symbol_table;
symbol_table.add_variable(x,x);
symbol_table.add_constants();

expression_t表达式;
expression.register_symbol_table(symbol_table);

parser_t解析器;
parser.compile(expression_string,expression);

for(x = T(-5); x < = T(+5); x < span class =code-attribute> + = T(0.001))

{

T ÿ <跨度class =code-attribute> = expression.value();

printf(%19.15f \ t%19.15f \ n,x,y);

}



}


I have a mathematical expression. I want to save it in CString. And after that I want to execute that CString as a code line.

Mathematical expression is like = 3 * (var1 + 10) + 100
if var1 > 100 then var1 = 1 else if var1 < 100 var1 = 2.

value of var1 is taken from some CSV / text file.

Suppose if var1 = 90
then CString strExp = 3 * (2 + 10) + 100;
and then I want to execute this CString and result would be saved in another string.

double Result = XXXX strExp XXXXX //whatever operation / parsing
// result = 136

解决方案

You cannot do it in any simple way. In some interpretive languages, and even in some compiling languages, this is a fundamental feature, but C++ is not one of them.

You will need to create some compiler or interpreter to parse and execute your string. You can start here: http://www.codeproject.com/search.aspx?q=C%2b%2b+expression+parser&doctypeid=1%3b2%3b3%3b13%3b14%3b5[^].

Alternative way would be not using strings at all. You can develop special types representing nodes of expression tree and the tree itself, which your could use to create expressions right in C++ code. In other words, it's possible to develop something similar to .NET lambda expressions. But I guess it would take me writing a whole book to explain it. Your question strongly suggests that you are not ready for such works yet.

—SA


As an alternative, you may embed a script language, like Lua in your application and then let the script engine evaluate expressions (and more complex constructs) for you. We have an article on this very topic: "Integrating Lua into C++" here at Code Project[^].
See also "Evaluating Mathematical Expressions using Lua" at Stack Overflow[^].


template <typename T>
void trig_function()
{
   typedef exprtk::symbol_table<T> symbol_table_t;
   typedef exprtk::expression<T>     expression_t;
   typedef exprtk::parser<T>             parser_t;

   std::string expression_string = "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";

   T x;

   symbol_table_t symbol_table;
   symbol_table.add_variable("x",x);
   symbol_table.add_constants();

   expression_t expression;
   expression.register_symbol_table(symbol_table);

   parser_t parser;
   parser.compile(expression_string,expression);

   for (x = T(-5); x <= T(+5); x += T(0.001))

   {

      T y = expression.value();

      printf("%19.15f\t%19.15f\n",x,y);

   }


}


这篇关于如何将CString作为代码行执行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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