-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_Huffman.cpp
126 lines (96 loc) · 3.49 KB
/
code_Huffman.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
/*---------------------------------------------------------
* name : Make Binery Compress with Huffman Algoritm(Coded)
* date : th,Dec,26,2019
* write by : Reza Tanaki zade
*---------------------------------------------------------*/
#include <iostream>
#include <string>
#include <sstream>
#include <bitset>
#include <fstream>
using namespace std;
//Redefine Functions;
void findNumberOfEachLetter();
int returnNumberOfLetter(string test,const char letter);
//Global Variables;
string text = ""; //Text that we must to Compress it;
char letter[] = {'a','b','c','d'}; //All the english letter;
int n_letter[4]; //number of each letter;
string bin[4] = {"00","01","10","111"}; //the binery code for most and least repeated words;
string bin_letter[4] = {"110","110","110","110"}; //binery Code with its defualt;
string tobineryletter = ""; //binery code of text fine;
//main Function;
int main(){
//read TEXT fine that we need to compressed
fstream myfile1;
myfile1.open ("text.txt");
myfile1 >> text;
myfile1.close();
//find number of each letter by this function that defined after main function ;
findNumberOfEachLetter();
//this variable and loop seved max repeated letter;
int max[2] = {n_letter[0],0};
for (int i = 1; i < 4; i++){
if (n_letter[i] > max[0]){
max[0] = n_letter[i];
max[1] = i;
}}
//print max
cout << "MAX OF ARRAY is : " << max[0] << " and ID is : " << max[1] << endl;
//find best binery code for each letter;
int b = 0;
for(int c = 0; c < max[0]; c++){
for(int i = 0; i < 4; i++){
if(n_letter[i] == (max[0]-c)){
bin_letter[i] = bin[b];
b++;
cout << i <<" " << letter[i] << " " << bin_letter[i] << endl;
}}}
//last binery code with decode Help
for(int i = 0; i < 4; i++){
tobineryletter += bin_letter[i];
}
//fine binery code for the text;
for(int i = 0; i < text.length(); i++){
char l = text[i];
for(int b = 0; b < 4; b++){
if(l == letter[b]){
tobineryletter += bin_letter[b];
}}}
tobineryletter += "10000000";
cout << tobineryletter << endl;
cout << text <<endl;
//make programm for save to Huffman.bin
fstream myfile;
string data = tobineryletter;
stringstream sstream(data);
string output;
while(sstream.good()){
bitset<8> bits;
sstream >> bits;
char c = char(bits.to_ulong());
output += c;
}
myfile.open ("Huffman.bin",ios::out | ios::binary);
myfile << output;
myfile.close();
return 0;
}
//this Function find repreated number of each letter ;
int returnNumberOfLetter(string test,const char letter){
int count = 0;
int l_number = test.length();
for (int i = 0; i < l_number; i++)
{
if (test[i] == letter)
count++;
}
return count;
}
//this function saved repeated number of each letter in n_letter array;
void findNumberOfEachLetter(){
for (int i = 0; i < sizeof(letter); i++){
n_letter[i] = returnNumberOfLetter(text,letter[i]);
cout << n_letter[i] << endl;
}
}