-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
168 lines (148 loc) · 5.62 KB
/
mainwindow.cpp
File metadata and controls
168 lines (148 loc) · 5.62 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
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
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "crchelper.h"
#include "crc.h"
#include "huffman.h"
#include <vector>
#include <stdexcept>
#include <QMessageBox>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->huffmanTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_calculateCRCButton_clicked()
{
try
{
const auto data = CRCHelper::tryParceByteArray(ui->originalTextCRC->toPlainText());
QString binText;
for(const auto& i : data)
binText += QString::number(i, 2).rightJustified(CHAR_BIT, '0');
ui->originalTextCRCBin->setPlainText(binText);
const unsigned polynom = CRCHelper::tryParsePolynom(ui->polynomProcess->text());
ui->polynomProcessBin->setText(QString::number(polynom, 2));
CRC crc(polynom);
const auto check = crc.calculate(data.data(), data.size());
ui->resultCRCHex->setText(QString::number(check, 16));
ui->resultCRCBin->setText(QString::number(check, 2).rightJustified(crc.polynomDegree(), '0'));
}
catch (std::runtime_error& ex)
{
QMessageBox::warning(this, "Ошибка вычисления CRC", ex.what());
}
}
void MainWindow::on_checkCRCButton_clicked()
{
try
{
const QString text = ui->checkText->toPlainText().simplified().remove(' ');
const unsigned polynom = CRCHelper::tryParsePolynom(ui->polynomCheck->text());
std::vector<bool> data(text.size());
for(size_t i = 0; i < data.size(); ++i)
{
const QChar& ch = text.at(static_cast<int>(i));
if(ch == '0')
continue;
else if(ch == '1')
data[i] = true;
else
throw std::runtime_error(
QString("Найден недопустимый символ в исходных данных: \"%1\"")
.arg(ch).
toStdString());
}
CRC crc(polynom);
const auto result = crc.calculate(data);
ui->crcCheckResult->setText(QString::number(result, 16));
ui->polynomCheckBin->setText(QString::number(crc.polynom(), 2));
}
catch (std::runtime_error& ex)
{
QMessageBox::warning(this, "Ошибка проверки CRC", ex.what());
}
}
void MainWindow::on_moveRight_clicked()
{
ui->checkText->setPlainText(ui->originalTextCRCBin->toPlainText() + ui->resultCRCBin->text());
ui->polynomCheck->setText(ui->polynomProcess->text());
}
void MainWindow::on_encodeButton_clicked()
{
try
{
const auto text = ui->originalTextHuffman->toPlainText();
if(text.isEmpty())
throw std::runtime_error("Строка для кодирования пуста");
const auto& chars = Huffman::getCharsAndItsCount(text.toStdString());
const auto root = Huffman::generateTree(chars);
auto codes = Huffman::getCodes(root);
ui->huffmanTable->setRowCount(static_cast<int>(chars.size()));
enum Columns { Char, Count, StringCode };
int i = 0;
for(const auto& [ch, count] : chars)
{
for(int j = 0; j < ui->huffmanTable->columnCount(); ++j)
{
switch(j)
{
case Columns::Char:
ui->huffmanTable->setItem(i, j, new QTableWidgetItem(QString(ch)));
break;
case Columns::Count:
ui->huffmanTable->setItem(i, j, new QTableWidgetItem(QString::number(count)));
break;
case Columns::StringCode:
ui->huffmanTable->setItem(i, j, new QTableWidgetItem(codes[ch].c_str()));
break;
default:
assert(false);
}
}
++i;
qDebug().noquote() << QString("%1;%2;%3").arg(ch).arg(QString::number(count)).arg(codes[ch].c_str());
}
ui->encodedText->setText(Huffman::encode(text.toStdString(), codes).c_str());
const auto avgCodeLength = Huffman::avgCodeLenght(text.toStdString(), chars, codes);
ui->avgCodeLength->setText(QString("Средняя длина кода: %1").arg(avgCodeLength));
const auto entropy = Huffman::entropy(text.toStdString(), chars);
ui->entropy->setText(QString("Энтропия: %1").arg(entropy));
}
catch (std::runtime_error& ex)
{
QMessageBox::warning(this, "Ошибка кодирования", ex.what());
}
}
void MainWindow::on_pushButton_clicked()
{
ui->encodedTextPre->setText(ui->encodedText->toPlainText());
}
void MainWindow::on_decodeButton_clicked()
{
try
{
const auto uiTable = ui->huffmanTable;
Huffman::CodesTable table;
for(int i = 0; i < uiTable->rowCount(); ++i)
{
char ch = uiTable->item(i, 0)->text().at(0).toLatin1();
std::string code = uiTable->item(i, 2)->text().toStdString();
table[ch] = code;
}
const std::string encodedText = ui->encodedTextPre->toPlainText().toStdString();
ui->decodedText->setText(Huffman::decode(encodedText, table).c_str());
}
catch (std::runtime_error& ex)
{
QMessageBox::warning(this, "Ошибка декодирования", ex.what());
}
}