Hello!
I try to use a parser for simple math expressions from a QString, as 3+2.(5^2)/9....and so on (no variables to calculate, just numbers). I started with a recursive-descent parser, but then I found a simpler solution with QJSEngine:
Qt Code:
  1. #include "math.h"
  2. #include "expression_calc.h"
  3. #include "ui_expression_calc.h"
  4. #include "QDebug"
  5. #include <QApplication>
  6. #include <QJSEngine>
  7. #include <QJSValue>
  8. #include <QObject>
  9.  
  10.  
  11. Expression_calc::Expression_calc(QWidget *parent) :
  12. QMainWindow(parent),
  13. ui(new Ui::Expression_calc)
  14. {
  15. ui->setupUi(this);
  16.  
  17. }
  18.  
  19. Expression_calc::~Expression_calc()
  20. {
  21. delete ui;
  22. }
  23.  
  24. void Expression_calc::on_CalcButton_clicked()
  25. {
  26. QString expression= ui->exp->text();
  27. QJSEngine parsexpression;
  28. double result=parsexpression.evaluate(expression).toNumber();
  29. ui->Result->setText(QString::number(result));
  30. }
To copy to clipboard, switch view to plain text mode 
Ok, very simply, ...but this method can interpret just +,-,*,/,()...
I decide to manipulate the main QString expression with a
Qt Code:
  1. expression.replace("sqrt","Math.sqrt");
  2. expression.replace("abs","Math.abs");
  3. expression.replace("cos","Math.cos");
  4. ......
To copy to clipboard, switch view to plain text mode 
Not very elegant, but a fast solution. In add, as I have to store the expressions, I can save the corrected versions.
All it works, except for the damned "^". Math requires "pow(3,2)" instead of common expression 3^2, does someone have a fast solution to workaround this, or you suggest to return to a recursive-descent parser?
Thanks!