从Exprtk表达式中检索字符串结果

huangapple go评论69阅读模式
英文:

Retrieving the string result from an Exprtk expression

问题

以下是您要翻译的内容:

"Is it possible to get the returned strings from an Exprtk expression? The code below returns NaN as answer, which is to be expected. But getting the string, in this case 'One', only seems possible using a return[] statement or by using an explicit string variable. But a simple std::string expression->value_() would be a great help.
Stepping through the code shows a control_block_ with a string value_ private property with the correct result. But how is it accessible?

In the code there are two strings to evaluate, but both show the identical behavior.

Thanks for any thoughts.
...

#include "exprtk.hpp"

int main()

{
typedef exprtk::symbol_table symbol_table_t;
typedef exprtk::expression expression_t;
typedef exprtk::parser parser_t;

const std::string expression_string =
//"if (1 > 0) {'One';} else {'Two';}";
"if (1 > 0,'One','Two')";

expression_t expression;
parser_t parser;

if (parser.compile(expression_string, expression))
{
double result = expression.value();
// How can the string result be retrieved at this point?
}
return 0;
}"

英文:

Is it possible to get the returned strings from an Exprtk expression? The code below returns NaN as answer, which is to be expected. But getting the string, in this case 'One', only seems possible using a return[] statement or by using an explicit string variable. But a simple std::string expression->value_() would be a great help.
Stepping through the code shows a control_block_ with a string value_ private property with the correct result. But how is it accessible?

In the code there are two strings to evaluate, but both show the identical behavior.

Thanks for any thoughts.
...

#include "exprtk.hpp"

int main()

{
   typedef exprtk::symbol_table<double> symbol_table_t;
   typedef exprtk::expression<double>   expression_t;
   typedef exprtk::parser<double>       parser_t;

   const std::string expression_string =
                     //"if (1 > 0) {'One';} else {'Two';}";
                        "if (1 > 0,'One','Two')";

   expression_t expression;
   parser_t parser;

   if (parser.compile(expression_string, expression))
   {
       double result = expression.value();
       //  How can the string result be retrieved at this point?
   }
   return 0;
}

...

答案1

得分: 1

I share how I solved it since the documentation was also quite confusing for me.

我分享一下我是如何解决的,因为文档对我来说也很令人困惑。

I use return in the expression for the return string, and then get the results of the expression.

在表达式中使用 return 来返回字符串,然后获取表达式的结果。

#include "exprtk.hpp"
#include <iostream>
#include <string>
int main()
{

    typedef exprtk::symbol_table<double> symbol_table_t;
    typedef exprtk::expression<double> expression_t;
    typedef exprtk::parser<double> parser_t;

    symbol_table_t symbol_table;
    expression_t expression;
    parser_t parser;


    if(!parser.compile("return [1, 2, 'return-call 1234']", expression)){
        std::cout << "Error parsing expression \n";
    }

    double result = expression.value();
    if(std::isnan(result)){
        std::cout << "Result is NaN, check results list\n";
    }

    if (expression.results().count())
    {
        typedef exprtk::results_context<double> results_context_t;
        typedef typename results_context_t::type_store_t type_t;
        typedef typename type_t::scalar_view scalar_t;
        typedef typename type_t::string_view string_t;

        const results_context_t &results = expression.results();

        for (std::size_t i = 0; i < results.count(); ++i)
        {
            type_t t = results[i];

            switch (t.type)
            {
            case type_t::e_scalar: {
                scalar_t value(t);
                std::cout << "Scalar value:" << value() << "\n";
                break;
            }
            case type_t::e_string: {
                string_t value_t(t);
                std::string value = to_str(value_t).c_str();
                std::cout << "String value Len: " << value.size() << " Value: '" << value << "'\n";
                break;
            }
            default: {
                continue;
            }
            }
        }
    }
    return 0;
}

Hope this helps someone, cheers!

希望这对某人有帮助,祝好运!

英文:

I share how I solved it since the documentation was also quite confusing for me.

I use return in the expression for the return string, and then get the results of the expression.

#include &quot;exprtk.hpp&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
int main()
{

    typedef exprtk::symbol_table&lt;double&gt; symbol_table_t;
    typedef exprtk::expression&lt;double&gt; expression_t;
    typedef exprtk::parser&lt;double&gt; parser_t;

    symbol_table_t symbol_table;
    expression_t expression;
    parser_t parser;


    if(!parser.compile(&quot;return [1, 2, &#39;return-call 1234&#39;]&quot;, expression)){
        std::cout &lt;&lt; &quot;Error parsing expression \n&quot;;
    }

    double result = expression.value();
    if(std::isnan(result)){
        std::cout &lt;&lt; &quot;Result is NaN, check results list\n&quot;;
    }

    if (expression.results().count())
    {
        typedef exprtk::results_context&lt;double&gt; results_context_t;
        typedef typename results_context_t::type_store_t type_t;
        typedef typename type_t::scalar_view scalar_t;
        typedef typename type_t::string_view string_t;

        const results_context_t &amp;results = expression.results();

        for (std::size_t i = 0; i &lt; results.count(); ++i)
        {
            type_t t = results[i];

            switch (t.type)
            {
            case type_t::e_scalar: {
                scalar_t value(t);
                std::cout &lt;&lt; &quot;Scalar value:&quot; &lt;&lt; value() &lt;&lt; &quot;\n&quot;;
                break;
            }
            case type_t::e_string: {
                string_t value_t(t);
                std::string value = to_str(value_t).c_str();
                std::cout &lt;&lt; &quot;String value Len: &quot; &lt;&lt; value.size() &lt;&lt; &quot; Value: &#39;&quot; &lt;&lt; value &lt;&lt; &quot;&#39;\n&quot;;
                break;
            }
            default: {
                continue;
            }
            }
        }
    }
    return 0;
}

Hope this helps someone, cheers!

huangapple
  • 本文由 发表于 2023年6月30日 02:25:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定