#include "math.h"
#include "expression_calc.h"
#include "ui_expression_calc.h"
#include "QDebug"
#include <QApplication>
#include <QJSEngine>
#include <QJSValue>
#include <QObject>
Expression_calc
::Expression_calc(QWidget *parent
) : ui(new Ui::Expression_calc)
{
ui->setupUi(this);
}
Expression_calc::~Expression_calc()
{
delete ui;
}
void Expression_calc::on_CalcButton_clicked()
{
QString expression
= ui
->exp
->text
();
expression.replace("pi","Math.PI");
expression.replace("abs(","Math.abs{");
expression.replace("sin(","Math.sin{");
expression.replace("cos(","Math.cos{");
expression.replace("tan","Math.tan");
//.... and so on for trigonometrical functions)
expression.replace("sqrt","Math.sqrt");
expression.replace("(","[");
expression.replace(")","]");
expression.replace("^","POW");
expression.replace("+","PLUS");
expression.replace("-","MINUS");
expression.replace("*","MULT");
expression.replace("/","DIV");
while (expression.contains("[")) //START SOLVING EXPRESSIONS BETWEEN BRACKETS
{
QRegularExpression rep("\\[([^\\]]+)\\]");
QRegularExpressionMatch matchp = rep.match(expression);
QString expp_w_brackets
= matchp.
captured(0);
//original expression with brakets QString expp
= matchp.
captured(1);
//expression without brakets
while (expp.contains("POW")) //SOLVE POE EXPRESSION
{
QRegularExpression re1("(\\d+.\\d+POW)|(\\d+POW)");
QRegularExpressionMatch match1 = re1.match(expp);
QRegularExpression re2("(POW\\d+.\\d+)|(POW\\d+)");
QRegularExpressionMatch match2 = re2.match(expp);
if (match1.hasMatch())
{
exp1 = match1.captured(0);
exp1.replace("POW","");
}
if (match2.hasMatch())
{
exp2 = match2.captured(0);
exp2.replace("POW","");
}
expp.replace (""+exp1+"" "POW"""+exp2+"" , "Math.pow(" ""+exp1+"" "," ""+exp2+"" ")");
}
expp.replace ("PLUS","+");
expp.replace ("MINUS","-");
expp.replace("MULT","*");
expp.replace("DIV","/");
QJSEngine parsexpressionp;
double resultp=parsexpressionp.evaluate(expp).toNumber();
expression.replace(""+expp_w_brackets+"" ,""+BraketResult+"");
}
while (expression.contains("POW")) //SOLVE POE EXPRESSION
{
QRegularExpression re1("(\\d+.\\d+POW)|(\\d+POW)");
QRegularExpressionMatch match1 = re1.match(expression);
QRegularExpression re2("(POW\\d+.\\d+)|(POW\\d+)");
QRegularExpressionMatch match2 = re2.match(expression);
if (match1.hasMatch())
{
exp1 = match1.captured(0);
exp1.replace("POW","");
}
if (match2.hasMatch())
{
exp2 = match2.captured(0);
exp2.replace("POW","");
}
expression.replace (""+exp1+"" "POW"""+exp2+"" , "Math.pow(" ""+exp1+"" "," ""+exp2+"" ")");
}
expression.replace("{","(");
expression.replace("[","(");
expression.replace("]",")");
expression.replace ("PLUS","+");
expression.replace ("MINUS","-");
expression.replace("MULT","*");
expression.replace("DIV","/");
QJSEngine parsexpression;
double result=parsexpression.evaluate(expression).toNumber();
ui
->Result
->setText
(QString::number(result
));
}
#include "math.h"
#include "expression_calc.h"
#include "ui_expression_calc.h"
#include "QDebug"
#include <QApplication>
#include <QJSEngine>
#include <QJSValue>
#include <QObject>
Expression_calc::Expression_calc(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Expression_calc)
{
ui->setupUi(this);
}
Expression_calc::~Expression_calc()
{
delete ui;
}
void Expression_calc::on_CalcButton_clicked()
{
QString expression= ui->exp->text();
QString exp1,exp2;
expression.replace("pi","Math.PI");
expression.replace("abs(","Math.abs{");
expression.replace("sin(","Math.sin{");
expression.replace("cos(","Math.cos{");
expression.replace("tan","Math.tan");
//.... and so on for trigonometrical functions)
expression.replace("sqrt","Math.sqrt");
expression.replace("(","[");
expression.replace(")","]");
expression.replace("^","POW");
expression.replace("+","PLUS");
expression.replace("-","MINUS");
expression.replace("*","MULT");
expression.replace("/","DIV");
while (expression.contains("[")) //START SOLVING EXPRESSIONS BETWEEN BRACKETS
{
QRegularExpression rep("\\[([^\\]]+)\\]");
QRegularExpressionMatch matchp = rep.match(expression);
QString expp_w_brackets = matchp.captured(0); //original expression with brakets
QString expp = matchp.captured(1);//expression without brakets
while (expp.contains("POW")) //SOLVE POE EXPRESSION
{
QRegularExpression re1("(\\d+.\\d+POW)|(\\d+POW)");
QRegularExpressionMatch match1 = re1.match(expp);
QRegularExpression re2("(POW\\d+.\\d+)|(POW\\d+)");
QRegularExpressionMatch match2 = re2.match(expp);
if (match1.hasMatch())
{
exp1 = match1.captured(0);
exp1.replace("POW","");
}
if (match2.hasMatch())
{
exp2 = match2.captured(0);
exp2.replace("POW","");
}
expp.replace (""+exp1+"" "POW"""+exp2+"" , "Math.pow(" ""+exp1+"" "," ""+exp2+"" ")");
}
expp.replace ("PLUS","+");
expp.replace ("MINUS","-");
expp.replace("MULT","*");
expp.replace("DIV","/");
QJSEngine parsexpressionp;
double resultp=parsexpressionp.evaluate(expp).toNumber();
QString BraketResult=(QString::number(resultp));
expression.replace(""+expp_w_brackets+"" ,""+BraketResult+"");
}
while (expression.contains("POW")) //SOLVE POE EXPRESSION
{
QRegularExpression re1("(\\d+.\\d+POW)|(\\d+POW)");
QRegularExpressionMatch match1 = re1.match(expression);
QRegularExpression re2("(POW\\d+.\\d+)|(POW\\d+)");
QRegularExpressionMatch match2 = re2.match(expression);
if (match1.hasMatch())
{
exp1 = match1.captured(0);
exp1.replace("POW","");
}
if (match2.hasMatch())
{
exp2 = match2.captured(0);
exp2.replace("POW","");
}
expression.replace (""+exp1+"" "POW"""+exp2+"" , "Math.pow(" ""+exp1+"" "," ""+exp2+"" ")");
}
expression.replace("{","(");
expression.replace("[","(");
expression.replace("]",")");
expression.replace ("PLUS","+");
expression.replace ("MINUS","-");
expression.replace("MULT","*");
expression.replace("DIV","/");
QJSEngine parsexpression;
double result=parsexpression.evaluate(expression).toNumber();
ui->Result->setText(QString::number(result));
}
To copy to clipboard, switch view to plain text mode
Bookmarks