This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalcwidget.cpp
More file actions
123 lines (109 loc) · 3.76 KB
/
Copy pathcalcwidget.cpp
File metadata and controls
123 lines (109 loc) · 3.76 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "calcwidget.h"
#include "ui_calcwidget.h"
CalcWidget::CalcWidget(std::shared_ptr<Algorithm> algorithm, QWidget *parent) :
QWidget(parent),
ui(new Ui::CalcWidget),
algorithm(algorithm)
{
ui->setupUi(this);
connect(ui->check, SIGNAL(clicked(bool)), this, SLOT(check()));
connect(ui->calculate, SIGNAL(clicked(bool)), this, SLOT(calc()));
connect(ui->remove, SIGNAL(clicked(bool)), this, SLOT(remove()));
connect(ui->button0, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->button1, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->button2, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->button3, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->button4, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->button5, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->button6, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->button7, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->button8, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->button9, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->divide, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->cos, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->dot, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->ln, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->minus, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->muliply, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->plus, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->power, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->rightBrace, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->sin, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
connect(ui->tg, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
}
CalcWidget::~CalcWidget()
{
delete ui;
}
bool CalcWidget::tryCheck()
{
try
{
if(algorithm->check())
{
ui->result->setText("OK");
return true;
}
else
{
ui->result->setText("Количество скобок не совпало.");
}
}
catch(std::invalid_argument e)
{
ui->result->setText(e.what());
}
catch(...)
{
ui->result->setText("Не удалось посчитать");
}
return false;
}
void CalcWidget::buttonPressed()
{
if(!ui->result->text().isEmpty())
ui->result->setText("");
QPushButton* button = qobject_cast<QPushButton*>(sender());
QString expression = ui->expression->text();
ui->expression->setText(expression + button->text());
}
void CalcWidget::remove()
{
ui->expression->setText("");
ui->result->setText("");
}
void CalcWidget::check()
{
algorithm->setExpression(ui->expression->text());
tryCheck();
}
void CalcWidget::calc()
{
if(ui->expression->text().isEmpty())
{
ui->result->setText("Задайте выражение");
return;
}
algorithm->setExpression(ui->expression->text());
try
{
if(tryCheck())
{
double result = 0.0;
result = algorithm->calculate();
ui->result->setText(QString::number(result));
}
else
{
ui->result->setText("Количество скобок не совпало.");
}
}
catch(std::invalid_argument invalidArgument)
{
ui->result->setText(invalidArgument.what());
}
catch(std::exception e)
{
ui->result->setText("Не удалось посчитать");
}
}