-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolvedSystemDialog.cpp
206 lines (195 loc) · 6.89 KB
/
SolvedSystemDialog.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "SolvedSystemDialog.h"
#include "ui_SolvedSystemDialog.h"
SolvedSystemDialog::SolvedSystemDialog(QWidget *parent) :
QWidget(parent),
ui(new Ui::SolvedSystemDialog)
{
ui->setupUi(this);
m_doubleValidator = new QDoubleValidator;
}
SolvedSystemDialog::~SolvedSystemDialog()
{
delete ui;
}
void SolvedSystemDialog::setCondition(double *genExprArray, double **varArray, double *constArray, quint8 row, quint8 col)
{
m_row = row;
m_col = col;
freeMemory();
SystemLinearEquations solve(genExprArray, varArray, constArray, col, row);
m_solvedArray = solve.Solve();
for(quint8 i = 0; i <= m_row; i++)
for(quint8 j = 0; j < 3; j++)
m_solvedArray[i][j] = _round(m_solvedArray[i][j]);
if(!m_solvedArray)
{
QMessageBox::information(this, QString::fromLocal8Bit("Ошибки!"),
QString::fromLocal8Bit("Эту систему нельзя решить!"),
QMessageBox::Ok);
emit back();
return;
}
allocateMemory();
placeWidgets();
}
void SolvedSystemDialog::freeMemory()
{
QLayoutItem *item;
while((item = ui->functionLayout->takeAt(0)) != 0)
{
delete item->widget();
}
for(quint8 i = 0; i < m_layouts.size(); i++)
{
while((item = m_layouts.value(i)->takeAt(0)) != 0)
delete item->widget();
}
while((item = ui->exprLayout->takeAt(0)) != 0)
delete item->layout();
for(quint8 i = 0; i < m_wArray.size(); i++)
{
delete m_wArray.value(i);
}
m_wArray.clear();
m_layouts.clear();
}
void SolvedSystemDialog::allocateMemory()
{
QLineEdit *lineEdit;
for(quint8 i = 0; i < m_row + 1; i++)
{
m_wArray.append(new QVector<QLineEdit*>); //lineedits for coefficents
for(quint8 j = 0; j < 3; j++)
{
lineEdit = new QLineEdit();
lineEdit->setAlignment(Qt::AlignRight);
lineEdit->setValidator(m_doubleValidator);
m_wArray[i]->append(lineEdit);
}
}
for(quint8 i = 0; i < m_row; i++) //layouts
{
m_layouts.append(new QHBoxLayout());
}
}
void SolvedSystemDialog::placeWidgets()
{
QLabel *label;
ui->functionLayout->addWidget(new QLabel("Z = "));
for(quint8 i = 0; i < 2; i++)
{
label = new QLabel("X<span style=\" vertical-align:sub;\">" + QString::number(i + 1)+ "</span>");
label->setMaximumWidth(22);
ui->functionLayout->addWidget(m_wArray.value(m_row)->value(i));
ui->functionLayout->addWidget(label);
label = new QLabel(" + ");
label->setMaximumWidth(22);
ui->functionLayout->addWidget(label);
}
ui->functionLayout->addWidget(m_wArray.value(m_row)->value(2));
for(quint8 i = 0; i < m_row; i++)
{
label = new QLabel("X<span style=\" vertical-align:sub;\">" + QString::number(i + 3)+ "</span> = ");
label->setMaximumWidth(22);
m_layouts.value(i)->addWidget(label);
for(quint8 j = 0; j < 2; j++)
{
label = new QLabel("X<span style=\" vertical-align:sub;\">" + QString::number(j + 1)+ "</span>");
label->setMaximumWidth(22);
m_layouts.value(i)->addWidget(m_wArray.value(i)->value(j));
m_layouts.value(i)->addWidget(label);
label = new QLabel(" + ");
label->setMaximumWidth(22);
m_layouts.value(i)->addWidget(label);
}
m_layouts.value(i)->addWidget(m_wArray.value(i)->value(2));
ui->exprLayout->addLayout(m_layouts.value(i));
}
}
bool SolvedSystemDialog::check()
{
QString result;
for(quint8 i = 0; i < 3; i++)
if(m_wArray.value(m_row)->value(i)->text().toDouble() != m_solvedArray[m_row][i])
{
if(i != 2)
result += QString::fromLocal8Bit("Ошибка в %1 в %2%3")
.arg(QString::fromLocal8Bit("функции"))
.arg("X")
.arg("<span style=\" vertical-align:sub;\">" + QString::number(i + 1)+ "</span><br />");
else
result += QString::fromLocal8Bit("Ошибка в %1 в %2%3")
.arg(QString::fromLocal8Bit("функции"))
.arg(QString::fromLocal8Bit("свободном члене"))
.arg("<br />");
emit userError();
}
for(quint8 i = 0; i < m_row; i++)
for(quint8 j = 0; j < 3; j++)
{
if(m_wArray.value(i)->value(j)->text().toDouble() != m_solvedArray[i][j])
{
if(j != 2)
result += QString::fromLocal8Bit("Ошибка в %1 в %2%3")
.arg(QString::fromLocal8Bit("уравнении ") + QString::number(i + 1))
.arg("X")
.arg("<span style=\" vertical-align:sub;\">" + QString::number(j + 1)+ "</span><br />");
else
result += QString::fromLocal8Bit("Ошибка в %1 в %2%3")
.arg(QString::fromLocal8Bit("уравнении ") + QString::number(i + 1))
.arg(QString::fromLocal8Bit("свободном члене"))
.arg("<br />");
emit userError();
}
}
if(result.isNull())
return true;
QMessageBox::information(this, QString::fromLocal8Bit("Ошибки!"), result, QMessageBox::Ok);
return false;
}
double SolvedSystemDialog::_round(double n)
{
n = n * 100;
if(fmod(n, 1) * 10 > 4)
return ceil(n) / 100;
else
return floor(n) / 100;
}
void SolvedSystemDialog::showHint()
{
QString str;
str += QString::fromLocal8Bit("Функция:<br />");
for(quint8 j = 0; j < 3; j++)
if(j != 2)
str += QString("%1X<span style=\" vertical-align:sub;\">%2</span> ")
.arg(QString::number(m_solvedArray[m_row][j]))
.arg(QString::number(j + 1));
else
str += QString::number(m_solvedArray[m_row][j]) + "C";
str += "<br />";
str += QString::fromLocal8Bit("Уравнения:<br />");
for(quint8 i = 0; i < m_row; i++)
{
for(quint8 j = 0; j < 3; j++)
if(j != 2)
str += QString("%1X<span style=\" vertical-align:sub;\">%2</span> ")
.arg(QString::number(m_solvedArray[i][j]))
.arg(QString::number(j + 1));
else
str += QString::number(m_solvedArray[i][j]) + "C";
str += "<br />";
}
QMessageBox::information(this, QString::fromLocal8Bit("Правильный ответ"), str, QMessageBox::Ok);
}
void SolvedSystemDialog::on_backButton_clicked()
{
emit back();
}
void SolvedSystemDialog::on_nextButton_clicked()
{
if(check())
{
emit result(m_solvedArray, m_row + 1);
emit next();
}
}