-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainwindow.cpp
396 lines (360 loc) · 11.4 KB
/
mainwindow.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableWidget>
#include <QMap>
#include <QMessageBox>
#include <QMovie>
#include <qmath.h>
int varCount = 3;
int needOnes = 0;
int minLen = 1000000000;
int varCountP2 = pow(2, varCount);
QString currentFunction = "00000000";
QString varLbl[6] = {"a","b","c","d","e","f"};
QMap <int, QStringList> headMap;
QMap <int, QStringList> banned;
QMap <int, QMap <QString, QList<int> > > accepted; // col, set, rows
QMap <int, int> selectedOnes; //row, bool
QMap <int, QMap <QString, bool> > selectedSets; // col, str, bool
QMap <QString, int> assoc;
QMap <int, QList <QStringList> >answers;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList tmpList;
tmpList << "a";
headMap[1] = tmpList;
tmpList.clear();
tmpList << "a" << "b" << "ab";
headMap[2] = tmpList;
tmpList.clear();
tmpList << "a" << "b" << "c" << "ab" << "ac" << "bc" << "abc";
headMap[3] = tmpList;
tmpList.clear();
tmpList << "a" << "b" << "c" << "d" << "ab" << "ac" << "ad" <<"bc" <<"bd" << "cd" << "abc" << "abd" << "acd" << "bcd" << "abcd";
headMap[4] = tmpList;
tmpList.clear();
tmpList << "a" << "b" << "c" << "d" << "e" << "ab" << "ac" << "ad" << "ae" << "bc" << "bd" << "be" << "cd" << "ce" << "de" << "abc" << "abd" << "abe" << "acd" << "ace" << "ade" <<"bcd" << "bce" <<"bde" << "cde" << "abcd" <<"abce" << "abde" << "acde" << "bcde" << "abcde";
headMap[5] = tmpList;
tmpList.clear();
tmpList << "a" << "b" << "c" << "d" << "e" << "f" << "ab" << "ac" << "ad" <<"ae" << "af" << "bc" <<"bd" << "be" << "bf" << "cd" << "ce" << "cf" << "de" << "df" << "ef" << "abc" << "abd" << "abe" << "abf" << "acd" << "ace" << "acf" << "ade" << "adf" << "aef" << "bcd" << "bce" << "bcf" << "bde" << "bdf" << "bef" << "cde" << "cdf" << "cef" << "def" << "abcd" << "abce" << "abcf" <<"abde" << "abdf" << "abef" << "acde" <<"acdf" << "acef" << "adef" << "bcde" << "bcdf" << "bcef" << "bdef" << "cdef" << "abcde" << "abcdf" << "abcef" << "abdef" << "acdef" << "bcdef" << "abcdef";
headMap[6] = tmpList;
tmpList.clear();
this->showMaximized();
ui->r1->setChecked(true);
updateVarCount(varCount);
ui->spinBoxVarCount->setValue(varCount);
QObject :: connect(ui->mainTable, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(changeCellValue(int, int)));
MDNF();
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::isBanned(int x, QString s){
if (banned[x].indexOf(s) != -1) {
return true;
}
return false;
}
bool MainWindow::isOverlapped (int col, QString set){
for (int i = 0; i < accepted[col][set].length(); ++i){
if (!selectedOnes[accepted[col][set][i]]){
return false;
}
}
return true;
}
void MainWindow::setOriginalState(){
minLen = 1000000000;
answers.clear();
needOnes = 0;
selectedOnes.clear();
selectedSets.clear();
accepted.clear();
banned.clear();
}
void MainWindow::MDNF(){
buildTable();
certainMDNF();
}
QString MainWindow::reverse(QString s){
QString ans = "";
for (int i = s.length()-1; i>=0; --i){
ans+=s[i];
}
return ans;
}
bool MainWindow::checkDuplicates(QStringList list){
int flag;
for (int i = 0; i < answers[minLen].length(); ++i){
flag = 1;
for (int j = 0; j < list.length(); ++j){
if (answers[minLen][i][j] != list[j]){
flag = 0;
}
}
if (flag) return false;
}
return true;
}
QString MainWindow::getTerm(QString head, QString val){
QString ans;
for (int i = 0; i < val.length(); ++i){
if(val[i] == "1"){
ans+=head[i];
} else {
ans+="!"+head[i];
}
}
return ans;
}
void MainWindow::updateVarCount(int x) {
varCount = x;
varCountP2 = pow(2,x);
for(int i = 0; i < x; ++i){
assoc[varLbl[i]] = x-i-1;
}
}
QStringList MainWindow::getSimpleColumn(QString s){
int x = assoc[s];
QStringList ans;
while(ans.length() < varCountP2){
for (int i = 0; i < pow(2,x); ++i){
ans<<"0";
}
for(int i = 0; i < pow(2,x); ++i){
ans<<"1";
}
}
return ans;
}
QStringList MainWindow::getColumn(QString s){
QStringList ans;
for (int i = 0; i < varCountP2; ++i){
ans << "";
}
for(int i = 0; i < s.length(); ++i){
QStringList tmpColumn = getSimpleColumn(QString(s[i]));
for (int j = 0; j < varCountP2; ++j){
ans[j]+=tmpColumn[j];
}
}
return ans;
}
QString MainWindow::get2from10(QString s){
long long num = s.toLongLong();
QString ans ="";
while (num!=0){
QString s1;
s1.setNum(num%2);
ans = s1+ans;
num/=2;
}
return ans;
}
QString MainWindow::get10from2 (QString s){
long long ansNum = 0;
for (int i = s.length()-1; i>=0; --i){
ansNum+=(QString(s[i]).toInt())*pow(2,i);
}
QString ansStr;
ansStr.setNum(ansNum);
return ansStr;
}
void MainWindow::changeCellValue(int row, int col){
if (col!=0)return;
QTableWidgetItem * item = ui->mainTable->item(row,col);
if (item->text() == "0"){
item->setText("1");
} else {
item->setText("0");
}
}
void MainWindow::buildTable(){
QTableWidget * table = ui->mainTable;
int tmpColumnCount = table->columnCount();
for(int i = 0; i < tmpColumnCount; ++i){
table->removeColumn(0);
table->removeRow(0);
}
for (int i = 0; i < varCountP2; ++i){
table->insertRow(i);
table->insertColumn(i);
}
QTableWidgetItem * item = new QTableWidgetItem();
item->setText("f(x)");
table->setHorizontalHeaderItem(0,item);
for(int i = 0; i < varCountP2; ++i){
QTableWidgetItem * itm = new QTableWidgetItem();
itm->setText(QString(currentFunction[i]));
itm->setFlags(Qt::ItemIsEnabled);
itm->setTextAlignment(Qt::AlignCenter);
table->setItem(i,0,itm);
}
for (int i = 1; i < varCountP2; ++i){
QTableWidgetItem * itm = new QTableWidgetItem();
itm->setText(headMap[varCount][i-1]);
table->setHorizontalHeaderItem(i,itm);
}
for (int j = 1; j < varCountP2; ++j){
QString hed = headMap[varCount][j-1];
QStringList column = getColumn(hed);
for (int i = 0; i < column.length(); ++i){
QCoreApplication::processEvents();
QTableWidgetItem * itm = new QTableWidgetItem();
itm->setText(column[i]);
itm->setFlags(Qt::ItemIsEnabled);
itm->setTextAlignment(Qt::AlignCenter);
table->setItem(i,j,itm);
}
}
for (int i =0 ; i <varCountP2; ++i){
QTableWidgetItem * itm = table->item(i,0);
if (itm->text() == "0"){
for (int j = 1; j < varCountP2; ++j){
QCoreApplication::processEvents();
QTableWidgetItem * tmpItm = table->item(i,j);
tmpItm->setBackground(QColor(255, 0, 0, 140));
if (!isBanned(j, tmpItm->text())){
banned[j] << tmpItm->text();
}
}
}
}
for (int i =0 ; i <varCountP2; ++i){
QTableWidgetItem * itm = table->item(i,0);
if (itm->text() == "1"){
++needOnes;
selectedOnes[i+1] = false;
for (int j = 1; j < varCountP2; ++j){
QCoreApplication::processEvents();
QTableWidgetItem * tmpItm = table->item(i,j);
if(isBanned(j,tmpItm->text())){
tmpItm->setBackground(QColor(0,0,225,127));
} else {
tmpItm->setBackground(QColor(0,225,0,127));
accepted[j][tmpItm->text()] << i+1;
}
}
}
}
}
void MainWindow::findMDNF(int curLen, QStringList curDNF, int idx, int ones){
if(ones == needOnes) {
if (curLen <= minLen){
minLen = curLen;
curDNF.sort();
if (checkDuplicates(curDNF)){
answers[curLen] << curDNF;
}
}
return;
}
QCoreApplication::processEvents();
int tmpIdx = 0;
for (int i = 1; i < varCountP2; ++i){ //col
foreach (QString key, accepted[i].keys()){
if(tmpIdx++ < idx) continue;
if(isOverlapped(i,key)) continue;
if(curLen+key.length() > minLen){
return;
}
bool * ssik = &selectedSets[i][key];
QList<int> * aik = &accepted[i][key];
if (!*ssik){
*ssik = true;
for (int j = 0; j <(*aik).length(); ++j){
if (selectedOnes[(*aik)[j]]==0) ++ones;
selectedOnes[(*aik)[j]] +=1;
}
QString hed = headMap[varCount][i-1];
curDNF << getTerm(hed,key);
findMDNF(curLen+key.length(), curDNF, idx+1, ones);
curDNF.removeLast();
*ssik = false;
for (int j = 0; j < (*aik).length(); ++j){
selectedOnes[(*aik)[j]] -=1;
if (selectedOnes[(*aik)[j]] == 0){
--ones;
}
}
}
}
}
}
void MainWindow::certainMDNF(){
QTextBrowser * lbl = ui->lblAns;
QLabel * lblGif = ui->lblGif;
QMovie * mov = new QMovie(":img/loading.gif");
lblGif->setMovie(mov);
mov->start();
lbl->setText("Loading...");
QStringList garbageList;
findMDNF(0, garbageList,0, 0);
QString s1, s2;
s1.setNum(minLen);
s2.setNum(answers[minLen].length());
if (minLen == 0){
s2.setNum(0);
}
QString strr = "Length: " + s1 + " Count: "+s2+"\n";
for (int i = 0; i < answers[minLen].length(); ++i){
strr += answers[minLen][i].join(" v ") + "\n";
}
lbl->setText(strr);
lblGif->clear();
}
void MainWindow::on_btnApply_clicked()
{
int tmpVarCount = ui->spinBoxVarCount->value();
QString stringInput;
if(ui->r1->isChecked()){
stringInput = ui->funcNumberInput1->text();
if (!QRegExp("[10]*").exactMatch(stringInput)){
QMessageBox::about(this, "Warning!", "Incorrect value");
return;
}
for (int i = 0; pow(2,tmpVarCount)-stringInput.length()>0; ++i){
stringInput = "0"+stringInput;
}
ui->funcNumberInput2->setText("");
} else if (ui->r2->isChecked()){
stringInput = get2from10(ui->funcNumberInput2->text());
if (!QRegExp("[0-9]*").exactMatch(stringInput)){
QMessageBox::about(this, "Warning!", "Incorrect value");
return;
}
for (int i = 0; pow(2,tmpVarCount)-stringInput.length()>0; ++i){
stringInput = "0"+stringInput;
}
ui->funcNumberInput1->setText("");
}
if(stringInput == currentFunction){
return;
}
if (stringInput.length() > pow(2,tmpVarCount)){
QMessageBox::about(this, "Warning!", "Incorrect value");
return;
}
updateVarCount(tmpVarCount);
setOriginalState();
currentFunction = stringInput;
MDNF();
}
void MainWindow::on_btnApply2_clicked()
{
QString str = "";
for (int i = 0; i < varCountP2; ++i){
QString s = ui->mainTable->item(i,0)->text();
str+=s;
}
if (str == currentFunction){
return;
}
setOriginalState();
currentFunction = str;
MDNF();
}