c++qtexprtk

Evaluate a math expression in Qt


I'm trying to create a Qt application and I need a math expression evaluator to evaluate things like this e.g. (4+5)*2-9/3. I included the .hpp file of this library (http://www.partow.net/programming/exprtk/) to my project in the Qt Creator and tried to launch the following example of code:

#include <cstdio>
#include <string>
#include "exprtk.hpp"

int main()
{
   typedef exprtk::expression<double> expression_t;
   typedef exprtk::parser<double>         parser_t;

   std::string expression_string = "3 + sqrt(5) + pow(3,2) + log(5)";

   expression_t expression;

   parser_t parser;

   if (parser.compile(expression_string,expression))
   {
     double result = expression.value();

     printf("Result: %19.15\n",result);
   }
   else
     printf("Error in expression\n.");

   return 0;
}

When I try to compile and run it I get the following output:

 debug\main.o:-1: error: too many sections (62303)

What could be the problem?


Solution

  • Using just pure Qt you can do something like this:

    QString expression_string("3 + Math.sqrt(5) + Math.pow(3,2) + Math.log(5)");
    QScriptEngine expression;
    double my_val=expression.evaluate(expression_string).toNumber();
    

    you can do much more, see HERE and HERE