-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
104 lines (84 loc) · 2.62 KB
/
mainwindow.cpp
File metadata and controls
104 lines (84 loc) · 2.62 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
104
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "graph/graphwidget.h"
#include "library/exprtk.hpp"
#include <QMessageBox>
#include <memory>
#include <QString>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::handlePushButtonClicked);
setupGraphWidget();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setupGraphWidget()
{
graphWidget = new GraphWidget(this);
ui->graphLayout->addWidget(graphWidget);
}
void MainWindow::handlePushButtonClicked()
{
QString exprStr = ui->lineEdit->text().trimmed();
bool isX = isXofYGraph(exprStr);
QString preparedExpr = prepareExpression(exprStr);
if (!graphWidget)
return;
setupGraphFunction(preparedExpr, isX);
}
bool MainWindow::isXofYGraph(const QString &expr) const
{
return expr.startsWith("x=", Qt::CaseInsensitive) || expr.startsWith("x =", Qt::CaseInsensitive);
}
QString MainWindow::prepareExpression(const QString &expr) const
{
int index = expr.indexOf('=');
if (index == -1)
return expr;
return expr.mid(index + 1).trimmed();
}
void MainWindow::setupGraphFunction(const QString &expr, bool isX)
{
using T = double;
auto variablePtr = std::make_shared<T>(0.0);
QString varName = (isX ? "y" : "x");
auto exprPtr = compileExpression(expr, varName, variablePtr);
if (!exprPtr)
{
QMessageBox::warning(this, "Ошибка", "Неверное выражение!");
return;
}
std::function<double(double)> func = createFunction(exprPtr, variablePtr);
graphWidget->setGraphType(isX ? GraphWidget::XofY : GraphWidget::YofX);
graphWidget->setFunction(func);
}
std::shared_ptr<exprtk::expression<double>> MainWindow::compileExpression(
const QString &expr,
const QString &varName,
std::shared_ptr<double> varPtr) const
{
exprtk::symbol_table<double> symbolTable;
symbolTable.add_variable(varName.toStdString(), *varPtr);
symbolTable.add_constants();
auto expressionPtr = std::make_shared<exprtk::expression<double>>();
expressionPtr->register_symbol_table(symbolTable);
exprtk::parser<double> parser;
if (!parser.compile(expr.toStdString(), *expressionPtr))
return nullptr;
return expressionPtr;
}
std::function<double(double)> MainWindow::createFunction(
std::shared_ptr<exprtk::expression<double>> exprPtr,
std::shared_ptr<double> varPtr) const
{
return [exprPtr, varPtr](double val) mutable -> double
{
*varPtr = val;
return exprPtr->value();
};
}