Results 1 to 6 of 6

Thread: Parser for mathematic expression from QString

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Mar 2016
    Posts
    22
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Parser for mathematic expression from QString

    I have to correct a mistake (if I had "2^(6/3)", the code returns a nan), in a second time I'll create a pow() function to have a more clear code...
    For the problem abs(-1)->abs1, or cos(0) -> cos0, a very rough solution: I change, for example, abs(-1) into math.abs{-1), and then, before processing expression, replace again "{" with "(".. Very ugly, but it's enough to avoid the transforming into math.abs-1...
    So, the code is this:
    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. Expression_calc::Expression_calc(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::Expression_calc)
    13. {
    14. ui->setupUi(this);
    15.  
    16. }
    17.  
    18. Expression_calc::~Expression_calc()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void Expression_calc::on_CalcButton_clicked()
    24. {
    25. QString expression= ui->exp->text();
    26. QString exp1,exp2;
    27.  
    28. expression.replace("pi","Math.PI");
    29. expression.replace("abs(","Math.abs{");
    30. expression.replace("sin(","Math.sin{");
    31. expression.replace("cos(","Math.cos{");
    32. expression.replace("tan","Math.tan");
    33. //.... and so on for trigonometrical functions)
    34.  
    35. expression.replace("sqrt","Math.sqrt");
    36. expression.replace("(","[");
    37. expression.replace(")","]");
    38. expression.replace("^","POW");
    39. expression.replace("+","PLUS");
    40. expression.replace("-","MINUS");
    41. expression.replace("*","MULT");
    42. expression.replace("/","DIV");
    43.  
    44. while (expression.contains("[")) //START SOLVING EXPRESSIONS BETWEEN BRACKETS
    45. {
    46.  
    47. QRegularExpression rep("\\[([^\\]]+)\\]");
    48. QRegularExpressionMatch matchp = rep.match(expression);
    49. QString expp_w_brackets = matchp.captured(0); //original expression with brakets
    50. QString expp = matchp.captured(1);//expression without brakets
    51.  
    52. while (expp.contains("POW")) //SOLVE POE EXPRESSION
    53. {
    54. QRegularExpression re1("(\\d+.\\d+POW)|(\\d+POW)");
    55. QRegularExpressionMatch match1 = re1.match(expp);
    56.  
    57. QRegularExpression re2("(POW\\d+.\\d+)|(POW\\d+)");
    58. QRegularExpressionMatch match2 = re2.match(expp);
    59.  
    60. if (match1.hasMatch())
    61. {
    62. exp1 = match1.captured(0);
    63. exp1.replace("POW","");
    64. }
    65.  
    66. if (match2.hasMatch())
    67. {
    68. exp2 = match2.captured(0);
    69. exp2.replace("POW","");
    70. }
    71.  
    72. expp.replace (""+exp1+"" "POW"""+exp2+"" , "Math.pow(" ""+exp1+"" "," ""+exp2+"" ")");
    73. }
    74.  
    75.  
    76. expp.replace ("PLUS","+");
    77. expp.replace ("MINUS","-");
    78. expp.replace("MULT","*");
    79. expp.replace("DIV","/");
    80.  
    81. QJSEngine parsexpressionp;
    82. double resultp=parsexpressionp.evaluate(expp).toNumber();
    83. QString BraketResult=(QString::number(resultp));
    84. expression.replace(""+expp_w_brackets+"" ,""+BraketResult+"");
    85. }
    86.  
    87. while (expression.contains("POW")) //SOLVE POE EXPRESSION
    88. {
    89. QRegularExpression re1("(\\d+.\\d+POW)|(\\d+POW)");
    90. QRegularExpressionMatch match1 = re1.match(expression);
    91.  
    92. QRegularExpression re2("(POW\\d+.\\d+)|(POW\\d+)");
    93. QRegularExpressionMatch match2 = re2.match(expression);
    94.  
    95.  
    96. if (match1.hasMatch())
    97. {
    98. exp1 = match1.captured(0);
    99. exp1.replace("POW","");
    100. }
    101.  
    102. if (match2.hasMatch())
    103. {
    104. exp2 = match2.captured(0);
    105. exp2.replace("POW","");
    106. }
    107.  
    108.  
    109. expression.replace (""+exp1+"" "POW"""+exp2+"" , "Math.pow(" ""+exp1+"" "," ""+exp2+"" ")");
    110. }
    111.  
    112. expression.replace("{","(");
    113. expression.replace("[","(");
    114. expression.replace("]",")");
    115. expression.replace ("PLUS","+");
    116. expression.replace ("MINUS","-");
    117. expression.replace("MULT","*");
    118. expression.replace("DIV","/");
    119. QJSEngine parsexpression;
    120. double result=parsexpression.evaluate(expression).toNumber();
    121. ui->Result->setText(QString::number(result));
    122. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sr1s; 8th May 2018 at 16:37.

Similar Threads

  1. parser
    By mickey in forum General Discussion
    Replies: 2
    Last Post: 23rd September 2009, 12:18
  2. Parser
    By ComaWhite in forum General Programming
    Replies: 4
    Last Post: 28th October 2008, 23:22
  3. Mathematic surfers
    By Colx007 in forum Qt Programming
    Replies: 1
    Last Post: 20th May 2008, 21:57
  4. How to get a QString from a file (use regular expression)
    By fengtian.we in forum Qt Programming
    Replies: 16
    Last Post: 31st May 2007, 11:06
  5. Parser in Qt.... HELP!
    By deepusrp in forum Newbie
    Replies: 6
    Last Post: 23rd January 2007, 21:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.