-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (91 loc) · 3.36 KB
/
main.cpp
File metadata and controls
105 lines (91 loc) · 3.36 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
#include <iostream>
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QFile>
#include <QThread>
#include "int64filereader.h"
#include "int64worker.h"
#include "verylonginteger.h"
Int64Worker* createWorker(Int64Worker::Operation operation)
{
Int64Worker* worker = new Int64Worker(operation);
QThread* thread = new QThread;
worker->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
return worker;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QCommandLineParser argsParser;
auto helpOption = argsParser.addHelpOption();
argsParser.addPositionalArgument("source", "source file with 64-bit signed integer numbers divided space or new line char");
argsParser.process(a);
if (argsParser.isSet(helpOption))
argsParser.showHelp(0);
else if (argsParser.positionalArguments().isEmpty())
argsParser.showHelp(-1);
else
{
QFile file(argsParser.positionalArguments()[0]);
if (!file.open(QFile::ReadOnly))
{
std::cout << "Reading file error" << std::endl;
return -1;
}
Int64FileReader reader(file);
QObject::connect(&reader, &Int64FileReader::completePercentChanged, [](int percent){ std::cout << percent << "% completed" << std::endl;});
qint64 firstNumber;
bool firstNumberFilled = false;
qint64 xorResult;
VeryLongInteger sumResult;
QScopedPointer<Int64Worker> xorWorker(createWorker([&xorResult](const qint64& right) { xorResult ^= right; }));
QScopedPointer<Int64Worker> sumWorker(createWorker([&sumResult](const qint64& right) { sumResult += right; }));
while (!reader.isFinished())
{
bool ok;
qint64 result = reader.readNextNumber(&ok);
if (ok)
{
if (!firstNumberFilled)
{
firstNumberFilled = true;
firstNumber = result;
xorResult = result;
}
else
{
xorWorker->addNumber(result);
sumWorker->addNumber(result);
}
}
}
if (!firstNumberFilled)
{
std::cout << "Reading file error" << std::endl;
return -1;
}
else
{
sumWorker->stop();
xorWorker->stop();
while (!sumWorker->isFinished())
QThread::msleep(1);
VeryLongInteger sumRes(firstNumber);
sumRes += sumResult;
VeryLongInteger subResult(firstNumber);
subResult -= sumResult;
std::cout << "sum result:" << sumRes.toString().toStdString() << std::endl;
std::cout << "sub result:" << subResult.toString().toStdString() << std::endl;
while (!xorWorker->isFinished())
QThread::msleep(1);
std::cout << "xor result:" << xorResult << std::endl;
return 0;
}
return a.exec();
}
return a.exec();
}