@@ -1,127 +1,87 @@
|
|
1 |
#include <QProgressBar>
|
2 |
-
#include <QTime>
|
3 |
-
#include <QtMath>
|
4 |
|
|
|
5 |
#include "MainWindow.h"
|
6 |
#include "ui_MainWindow.h"
|
7 |
|
8 |
MainWindow::MainWindow(QWidget *parent)
|
9 |
: QMainWindow(parent)
|
10 |
, ui(new Ui::MainWindow)
|
11 |
{
|
12 |
ui->setupUi(this);
|
13 |
|
14 |
this->progressBar = new QProgressBar();
|
15 |
this->ui->statusBar->addPermanentWidget(progressBar);
|
16 |
this->ui->statusBar->showMessage(tr("Ready"));
|
17 |
}
|
18 |
|
19 |
MainWindow::~MainWindow()
|
20 |
{
|
21 |
delete ui;
|
22 |
}
|
23 |
|
24 |
#include <iostream>
|
25 |
void MainWindow::on_lineEditNumber_textEdited(const QString &arg1)
|
26 |
{
|
27 |
// std::cout << qPrintable(arg1) << std::endl;
|
28 |
bool enable = arg1.trimmed().length() > 0;
|
29 |
this->ui->pushButtonCalculate->setEnabled(enable);
|
30 |
}
|
31 |
|
32 |
void MainWindow::on_pushButtonStop_clicked()
|
33 |
{
|
34 |
this->userStopped = true;
|
35 |
}
|
36 |
|
37 |
void MainWindow::on_pushButtonCalculate_clicked()
|
38 |
{
|
39 |
bool isValid = true;
|
40 |
const QString& text = this->ui->lineEditNumber->text();
|
41 |
|
42 |
long long int number = text.toLongLong(&isValid);
|
43 |
|
44 |
if ( isValid )
|
45 |
{
|
46 |
this->ui->plainTextEditResults->clear();
|
47 |
this->progressBar->reset();
|
48 |
|
49 |
this->ui->pushButtonCalculate->setEnabled(false);
|
50 |
this->ui->pushButtonStop->setEnabled(true);
|
51 |
this->userStopped = false;
|
52 |
ui->statusBar->showMessage( tr("Calculating...") );
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
this->ui->pushButtonCalculate->setEnabled(true);
|
60 |
this->ui->pushButtonStop->setEnabled(false);
|
61 |
-
ui->statusBar->showMessage( tr("%1 sums found in %2 seconds").arg(sumCount).arg(seconds) );
|
62 |
}
|
63 |
else
|
64 |
{
|
65 |
ui->statusBar->showMessage( tr("Invalid number: %1").arg(text) );
|
66 |
}
|
67 |
}
|
68 |
|
69 |
-
|
70 |
-
long long MainWindow::calculate(long long number)
|
71 |
-
{
|
72 |
-
if ( number < 4 || number == 5 ) return 0;
|
73 |
-
return number % 2 == 0 ? calculateEvenGoldbach(number) : calculateOddGoldbach(number);
|
74 |
-
}
|
75 |
-
|
76 |
-
long long MainWindow::calculateEvenGoldbach(long long number)
|
77 |
-
{
|
78 |
-
long long results = 0;
|
79 |
-
for ( long long a = 2; a < number && this->userStopped == false; ++a )
|
80 |
{
|
81 |
-
|
82 |
-
|
83 |
-
if ( ! isPrime(a) ) continue;
|
84 |
-
long long b = number - a;
|
85 |
-
if ( b >= a && isPrime(b) )
|
86 |
-
this->ui->plainTextEditResults->appendPlainText( tr("%1: %2 + %3").arg(++results).arg(a).arg(b) );
|
87 |
-
|
88 |
-
QApplication::processEvents();
|
89 |
-
}
|
90 |
-
return results;
|
91 |
}
|
92 |
|
93 |
-
|
94 |
{
|
95 |
-
|
96 |
-
|
97 |
-
{
|
98 |
-
this->progressBar->setValue( static_cast<int>(100LL * a / (number - 1)) );
|
99 |
-
|
100 |
-
if ( ! isPrime(a) ) continue;
|
101 |
-
for ( long long b = a; b < number; ++b )
|
102 |
-
{
|
103 |
-
if ( this->userStopped )
|
104 |
-
return results;
|
105 |
-
|
106 |
-
if ( ! isPrime(b) ) continue;
|
107 |
-
long long c = number - a - b;
|
108 |
-
if ( c >= b && isPrime(c) )
|
109 |
-
this->ui->plainTextEditResults->appendPlainText( tr("%1: %2 + %3 + %4").arg(++results).arg(a).arg(b).arg(c) );
|
110 |
-
|
111 |
-
QApplication::processEvents();
|
112 |
-
}
|
113 |
-
}
|
114 |
-
return results;
|
115 |
}
|
116 |
|
117 |
-
|
118 |
{
|
119 |
-
|
120 |
-
|
121 |
-
long long last = static_cast<long long>( qSqrt( number) );
|
122 |
-
for ( long long i = 2; i <= last; ++i )
|
123 |
-
if ( number % i == 0 )
|
124 |
-
return false;
|
125 |
-
|
126 |
-
return true;
|
127 |
}
|
1 |
#include <QProgressBar>
|
|
|
|
|
2 |
|
3 |
+
#include "GoldbachWorker.h"
|
4 |
#include "MainWindow.h"
|
5 |
#include "ui_MainWindow.h"
|
6 |
|
7 |
MainWindow::MainWindow(QWidget *parent)
|
8 |
: QMainWindow(parent)
|
9 |
, ui(new Ui::MainWindow)
|
10 |
{
|
11 |
ui->setupUi(this);
|
12 |
|
13 |
this->progressBar = new QProgressBar();
|
14 |
this->ui->statusBar->addPermanentWidget(progressBar);
|
15 |
this->ui->statusBar->showMessage(tr("Ready"));
|
16 |
}
|
17 |
|
18 |
MainWindow::~MainWindow()
|
19 |
{
|
20 |
delete ui;
|
21 |
}
|
22 |
|
23 |
#include <iostream>
|
24 |
void MainWindow::on_lineEditNumber_textEdited(const QString &arg1)
|
25 |
{
|
26 |
// std::cout << qPrintable(arg1) << std::endl;
|
27 |
bool enable = arg1.trimmed().length() > 0;
|
28 |
this->ui->pushButtonCalculate->setEnabled(enable);
|
29 |
}
|
30 |
|
31 |
void MainWindow::on_pushButtonStop_clicked()
|
32 |
{
|
33 |
this->userStopped = true;
|
34 |
}
|
35 |
|
36 |
void MainWindow::on_pushButtonCalculate_clicked()
|
37 |
{
|
38 |
bool isValid = true;
|
39 |
const QString& text = this->ui->lineEditNumber->text();
|
40 |
|
41 |
long long int number = text.toLongLong(&isValid);
|
42 |
|
43 |
if ( isValid )
|
44 |
{
|
45 |
this->ui->plainTextEditResults->clear();
|
46 |
this->progressBar->reset();
|
47 |
|
48 |
this->ui->pushButtonCalculate->setEnabled(false);
|
49 |
this->ui->pushButtonStop->setEnabled(true);
|
50 |
this->userStopped = false;
|
51 |
ui->statusBar->showMessage( tr("Calculating...") );
|
52 |
|
53 |
+
this->time.start();
|
54 |
+
// todo: fix the memory leak
|
55 |
+
GoldbachWorker* worker = new GoldbachWorker{number};
|
56 |
+
|
57 |
+
this->connect( worker, &GoldbachWorker::sumFound, this, &MainWindow::appendResult );
|
58 |
+
this->connect( worker, &GoldbachWorker::calculationDone, this, &MainWindow::calculationDone );
|
59 |
+
this->connect( worker, &GoldbachWorker::progressUpdated, this, &MainWindow::updateProgressBar );
|
60 |
+
|
61 |
+
//long long sumCount = this->calculate(number);
|
62 |
+
worker->start();
|
63 |
|
64 |
this->ui->pushButtonCalculate->setEnabled(true);
|
65 |
this->ui->pushButtonStop->setEnabled(false);
|
|
|
66 |
}
|
67 |
else
|
68 |
{
|
69 |
ui->statusBar->showMessage( tr("Invalid number: %1").arg(text) );
|
70 |
}
|
71 |
}
|
72 |
|
73 |
+
void MainWindow::appendResult(const QString& result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
{
|
75 |
+
this->ui->plainTextEditResults->appendPlainText(result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
}
|
77 |
|
78 |
+
void MainWindow::calculationDone(long long sumCount)
|
79 |
{
|
80 |
+
double seconds = this->time.elapsed() / 1000.0;
|
81 |
+
ui->statusBar->showMessage( tr("%1 sums found in %2 seconds").arg(sumCount).arg(seconds) );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
}
|
83 |
|
84 |
+
void MainWindow::updateProgressBar(int percent)
|
85 |
{
|
86 |
+
this->progressBar->setValue(percent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
}
|