Professional Documents
Culture Documents
2 5233528202634203340
2 5233528202634203340
Лабораторна робота №2
з дисципліни “Кодування та захист інформації ”
застосування словникових методів
Виконала:
студентка ВНТУ групи 2ПІ-18б
Богач К.С
Перевірив:
Майданюк В.П
Вінниця 2021
2
Лабораторна робота №2
МЕТА:Отримати практичні навики застосування словникових методів
ХІД РОБОТИ
LZW метод
Лістинг
// ConsoleApplication36.cpp
//
#include "stdafx.h"
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <sys/stat.h>
#include <math.h>
using namespace std;
template <typename Iterator>
Iterator compress(const std::string &uncompressed, Iterator result) {//зжаття
int dictSize = 256;
int maxSize = 4096;
std::map<std::string, int> dictionary;
for (int i = 0; i < 256; i++)
dictionary[std::string(1, i)] = i;
std::string w;
for (std::string::const_iterator it = uncompressed.begin(); it !=
uncompressed.end(); ++it) {
char c = *it;
std::string wc = w + c;
if (dictionary.count(wc))
w = wc;
else {
*result++ = dictionary[w];
if (!w.empty())
*result++ = dictionary[w];
return result;
}
entry = dictionary[k];
else if (k == dictSize)
entry = w + w[0];
else
throw "Bad compressed k";
result += entry;
int binaryString2Int(std::string p) {
int code = 0;
if (p.size()>0) {
if (p.at(0) == '1')
code = 1;
p = p.substr(1);
while (p.size()>0) {
code = code << 1;
if (p.at(0) == '1')
code++;
p = p.substr(1);
}
}
return code;
}
}
if (temp.length() != 0) {
}
return result;
}
bcode += p;
}
std::ofstream myfile;
myfile.open(file, std::ios::binary);
int b;
for (unsigned int i = 0; i < bcode.size(); i += 8) {
b = 1;
for (int j = 0; j < 8; j++) {
b = b << 1;
if (bcode.at(i + j) == '1')
b += 1;
}
char c = (char)(b & 255);
myfile.write(&c, 1);
}
cout <<endl<< bcode; //бінарний код рядок
myfile.close();
}
char c2[1226];
myfile2.read(c2, fsize);
std::string s = "";
long count = 0;
while (count<fsize) {
unsigned char uc = (unsigned char)c2[count];
std::string p = "";
6
int main() {
std::vector<int> compressed;
double Pi[11];
Pi[0] = 0.07;
Pi[1] = 0.13;
Pi[2] = 0.13;
Pi[3] = 0.07;
Pi[4] = 0.13;
Pi[5] = 0.07;
Pi[6] = 0.13;
Pi[7] = 0.07;
Pi[8] = 0.07;
Pi[9] = 0.07;
Pi[10] = 0.07;
double logar=0;
for (int i = 0; i < 11; i++)
{
logar += -(Pi[i])*(log(Pi[i]));
}
cout << "Entropia";//ентропія
cout << logar;
compress(message, std::back_inserter(compressed));
writeCompressedToFile(compressed, cstrNewFilename);
_gettch();
return 0;
}
ЗНІМКИ ЕКРАНУ
Рисунок 5 – Ентропія
ВИСНОВКИ