-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathparsermainwindow.cpp
More file actions
103 lines (94 loc) · 3.4 KB
/
mathparsermainwindow.cpp
File metadata and controls
103 lines (94 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "mathparsermainwindow.h"
#include "ui_mathparsermainwindow.h"
constexpr double rangeXlower=-10;
constexpr double rangeXupper=10;
constexpr double rangeYlower=-10;
constexpr double rangeYupper=10;
constexpr int nPlotPoints=500;
MathParserMainWindow::MathParserMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MathParserMainWindow),
mathParser(&mathEval)
{
ui->setupUi(this);
ui->plotGraphicsView->setScene(&scene);
for(char c='a';c<='z';++c)
{
varptr[static_cast<size_t>(c-'a')]=mathEval.getVarPtr(c);
*(varptr[static_cast<size_t>(c-'a')])=0.;
ui->variablesComboBox->addItem(QString(QChar(c)));
}
}
MathParserMainWindow::~MathParserMainWindow()
{
delete ui;
}
void MathParserMainWindow::drawCoordinateSystem()
{
double xstep=static_cast<double>(ui->plotGraphicsView->width())/(rangeXupper-rangeXlower);
double ystep=static_cast<double>(ui->plotGraphicsView->height())/(rangeYupper-rangeYlower);
double barlength=20;
scene.addLine(0,ui->plotGraphicsView->height()/2,ui->plotGraphicsView->width(),ui->plotGraphicsView->height()/2);
scene.addLine(ui->plotGraphicsView->width()/2,0,ui->plotGraphicsView->width()/2,ui->plotGraphicsView->height());
for(size_t x=0;x<static_cast<size_t>(rangeXupper-rangeXlower);++x)
scene.addLine(x*xstep,ui->plotGraphicsView->height()/2-barlength/2,x*xstep,ui->plotGraphicsView->height()/2+barlength/2);
for(size_t y=0;y<static_cast<size_t>(rangeYupper-rangeYlower);++y)
scene.addLine(ui->plotGraphicsView->width()/2-barlength/2,y*ystep,ui->plotGraphicsView->width()/2+barlength/2,y*ystep);
}
void MathParserMainWindow::drawPlot()
{
double *xptr=varptr[static_cast<size_t>('x'-'a')];
double xtmp=*xptr;
double xstep=(rangeXupper-rangeXlower)/static_cast<double>(nPlotPoints-1);
double result;
bool first=true;
QPointF lastpoint;
QPointF currentpoint;
for(int i=0;i<nPlotPoints;++i)
{
*xptr=rangeXlower+xstep*i;
mathEval.run();
result=mathEval.result();
if(result==result)
{
currentpoint=QPointF(static_cast<double>(i)/(nPlotPoints-1)*ui->plotGraphicsView->width(),(0.5-(result/(rangeYupper-rangeYlower)))*ui->plotGraphicsView->height());
if(!first)
scene.addLine(QLineF(lastpoint,currentpoint));
lastpoint=currentpoint;
first=false;
}
}
*xptr=xtmp;
}
void MathParserMainWindow::on_calculatePushButton_clicked()
{
mathParser.setString(ui->formulaLineEdit->text());
if(mathParser.parse())
{
mathEval.run();
ui->resultLineEdit->setText(QString::number(mathEval.result()));
scene.clear();
drawCoordinateSystem();
drawPlot();
ui->plotGraphicsView->setSceneRect(0,0,ui->plotGraphicsView->width(),ui->plotGraphicsView->height());
ui->plotGraphicsView->update();
update();
}
else
ui->resultLineEdit->setText("Invalid epression");
}
void MathParserMainWindow::on_variablesLineEdit_textEdited(const QString &arg1)
{
if(ui->variablesComboBox->currentIndex()!=-1)
{
bool ok;
double val;
val=arg1.toDouble(&ok);
if(ok)
*(varptr[ui->variablesComboBox->currentIndex()])=val;
}
}
void MathParserMainWindow::on_variablesComboBox_currentIndexChanged(int index)
{
ui->variablesLineEdit->setText(QString::number(*varptr[index]));
}