-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
162 lines (133 loc) · 3.77 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include "arvhuff.h"
/*
int simbol;
char string[MAXSTRING];
int freq;
char cod_huff[CODHUFF];
struct infos *prox;
*/
void GeraCodigos(Tree*root, Tabela**T ,char codhuff[CODHUFF]){ // algoritimo fiz em pre_ordem para salvar os codigos de huff na tabela
if(root!=NULL){
if(strcmp(root->string,"%null")!=0){
Tabela *aux=*T;
while(aux!=NULL && strcmp(aux->string,root->string)!=0){
aux=aux->prox;
}
if(aux!=NULL && strcmp(aux->string,root->string)==0){
printf("\nINFO SENDO CODIFICADA: %s",aux->string);
strcpy(aux->cod_huff,codhuff);
}
}
else{
char codhuffesq[CODHUFF], codhuffdir[CODHUFF];
strcpy(codhuffesq,codhuff);
strcpy(codhuffdir,codhuff);
strcat(codhuffesq,"0");
GeraCodigos(root->esq, T , codhuffesq);
strcat(codhuffdir,"1");
GeraCodigos(root->dir, T , codhuffdir);
}
}
}
void printar(Tabela *t){
while(t!=NULL){
printf("----\n");
printf("SIMBOL: %d\tSTR: %s\tfreq: %d\tcodHuff: %s\n",t->simbol,t->string,t->freq,t->cod_huff);
t=t->prox;
}
} // apenas para teste
void printarL(ListaGen *L){
while(L!=NULL){
printf("----\n");
printf("STR: %s\tfreq: %d\n",L->no->string,L->no->freqArv);
L=L->prox;
}
} // apenas para teste
char *procurarStringTabela(char string[], Tabela*t){
while(t!=NULL && strcmp(t->string,string)!=0){
t=t->prox;
}
if(t!=NULL && strcmp(t->string,string)==0)
return t->cod_huff;
return "";
}
void GerarStringCompilada(char filename[MAXSTRING], Tabela *t){
FILE *arq = fopen(filename,"r");
FILE *out = fopen("strCompilada.txt","w");
int tamArq = getFileSize(arq);
char stringCompilada[tamArq];
stringCompilada[0] = '\0';
char linha[tamArq];
if(fgets(linha,tamArq,arq)){
int i=0, contStr=0;
char string[MAXSTRING];
printf("\n%s",linha);
while(linha[i]!='\0'){
if (linha[i] == ' ') {
string[contStr] = '\0';
strcat(stringCompilada,procurarStringTabela(string,t));
strcat(stringCompilada,procurarStringTabela(" ",t));
contStr = 0;
//printf("\n%s",string); //fiz esses prints pra testar
//printf("\n%s",stringCompilada);
}
else{
string[contStr] = linha[i];
contStr++;
}
i++;
}
fprintf(out,"%s" ,stringCompilada);
}
else
printf("Nao foi possivel compressar a string: \n[READ ERROR: GerarStringCompiladaTabela()]");
fclose(out);
}
void GravarTabela(Tabela *T){
FILE *F = fopen("tabela.tabela","wb");
while(T!=NULL){
//printaTab no Arquivo
printf("SIMBOL: %d\tSTR: %s\tfreq: %d\tcodHuff: %s\n",T->simbol,T->string,T->freq,T->cod_huff);
fwrite(T, sizeof(Tabela),1,F);
T=T->prox;
}
fclose(F);
}
void printarArvore(Tree *raiz){ //funcao pra printar a arvore, mas nao em formato de arvore
if(raiz!=NULL){
if(raiz->esq == NULL && raiz->dir == NULL){
printf("\nINFO: %s -- FREQ: %d",raiz->string,raiz->freqArv);
}
printarArvore(raiz->esq);
printarArvore(raiz->dir);
}
}
void printarArvoreFormatada(Tree *raiz, int nivel) { //funcao pra printar a arvore em formato de arvore
if (raiz == NULL) {
return;
}
// Primeiro, imprime o nó da direita (subárvore direita)
printarArvoreFormatada(raiz->dir, nivel + 1);
// Imprime o nó atual com o deslocamento correspondente ao nível
for (int i = 0; i < nivel; i++) {
printf("\t"); // Tabulação para deslocar o nó
}
printf("(%s, %d)\n", raiz->string, raiz->freqArv);
// Depois, imprime o nó da esquerda (subárvore esquerda)
printarArvoreFormatada(raiz->esq, nivel + 1);
}
int main(void){
ListaGen *LG;
Tabela *tabela;
tabela = gerarTabela("message.txt");
LG = gerarLG(tabela);
GerarArvore(&LG);
GeraCodigos(LG->no, &tabela , "");
printar(tabela);
printarArvoreFormatada(LG->no,0);
GravarTabela(tabela);
GerarStringCompilada("message.txt",tabela);
return 0;
}