-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshannon.h
More file actions
69 lines (46 loc) · 1.68 KB
/
shannon.h
File metadata and controls
69 lines (46 loc) · 1.68 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
#include <iostream>
#include <fstream>
#include <map>
#include <bitset>
#include <stdlib.h>
using namespace std;
class shannonF{
struct Node
{
char symbol;
float probability;
};
int tsize;
Node *ptable;
map<char, string> bits;
public:
void Encode(const char *text, const char *output){
char symbol;
float ftot;
int i, total = 0;
ifstream inputData;
inputData.open(text);
if (inputData.is_open()){
map<char, int> frequencyMap;
while (inputData >> noskipws >> symbol){
frequencyMap[symbol]++;
total++;
}
//------------------------------------------------------------------------------------------------------
tsize = (int)frequencyMap.size(); // // cantidad de signos distintos en el texto
ptable = new Node[tsize]; // crea los nodos con la cantidad de signos que hay en el texto
ftot = float(total);
map<char, int>::iterator fi;
for (fi = frequencyMap.begin(), i = 0; fi != frequencyMap.end(); ++fi, ++i)
{
ptable[i].symbol = (*fi).first; // agrega el simbolo al node
ptable[i].probability = float((*fi).second) / ftot; //agrega con que frecuencia esta .
}
for (i = 0; i < tsize; i++)
{
cout << ptable[i].symbol << " " << ptable[i].probability << endl;
}
}else
cout << "Error" << endl;
}
};