Compréssion et décompression d'huffman [URGENT]

tekbright717 Messages postés 9 Date d'inscription samedi 7 janvier 2006 Statut Membre Dernière intervention 5 juin 2006 - 10 janv. 2006 à 23:04
BunoCS Messages postés 15476 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 3 mai 2024 - 11 janv. 2006 à 11:53
J'ai un comme sujet de faire la compréssion et décompression d'huffman.

J'ai avancé le sujet mais je suis bloquer dans ma fonction de création de l'arbre. Si quelqu'un pouvais m'aider sa serais cool!

Des pistes pour la suite et la décompréssion seraient les bien venuent elles aussi!



Je galère vraiment pour une petite érreur je pense.



Merci par avance voila mon code :



#include <stdio.h>

#include <string.h>

#include <stdlib.h>



typedef struct node_

{

int bin;

char elt;

struct node_ *left;

struct node_ *right;

}node;



typedef node* tree;



typedef struct stat_

{

int nbr;

tree t;

}stat;



/*typedef struct cell_

{

int elt;

struct cel_ *next;

}cell;

typedef cell* list;

*/



// allocation



void AllocateStat(stat *i)

{

i=(stat*)malloc(sizeof(stat)*256);

}



node* AllocateNode(char elt)

{

node* n;

n=(node*)malloc(sizeof(node));

n->elt=elt;

n->left=NULL;

n->right=NULL;

return (n);

}





void AllocateNode1(tree t)

{

t=(node*)malloc(sizeof(node));

t->left=NULL;

t->right=NULL;

}





//ETAPE 1 & 2

//initialisation, argument et comptage (occurence).

void comptage(char *filename,stat *T)

{

int i;

char bufferLine[1024];

char *s;

int gh;

//initialisation



memset(bufferLine, 0,sizeof(bufferLine));



for(i=0;i<=255;i++)

{

T[i].nbr=0;

}

//fin initialisation

FILE *file;

file=fopen(filename,"r");

AllocateStat(T);



while(fgets(bufferLine,1024,file))

{

s=bufferLine;

gh=strlen(s);

for(i=0;i<=gh;i++)

{

T[(int)bufferLine[i]].nbr++;//voiir si les caractere sont bien entré.

T[(int)bufferLine[i]].t=AllocateNode(bufferLine[i]);

}

}

}





//ETAPE 3

//tri de mon tableau de stats

int compare(const void *a,const void *b){



stat *j=(stat*) a ;

stat *k=(stat*) b ;



return((k->nbr)-(j->nbr));

}



void Tri(stat *T)

{

qsort(T,256,sizeof(stat),compare);

}





//ETAPE 4

//éllaboration de l'arbre d'huffman

tree AlgoHuff(stat *T, tree Huff)

{

int i=0;

int tmp=0;

int tmp2=0;



while(T[1].nbr!=0)

{

for(i=0;i<=255;i++)

{

if (T[i].nbr!=0)tmp++;

}



AllocateNode1(Huff);



Huff->right=T[tmp].t;

Huff->left=T[tmp-1].t;

tmp2=T[tmp].nbr+T[tmp-1].nbr;

T[tmp-1].nbr=tmp2;

T[tmp].nbr=0;

T[tmp-1].t=Huff;

Tri(T);

}

return (Huff);

}



//ETAPE 5

//édition du tableau de code (les 0 et les 1, le dictionnaire)



//ETAPE6

//entête du fichier compressé



//ETAPE7

//compréssion







int main(int argc,char **argv)

{

stat T[256];

int i;

tree Huff=NULL;

printf("nombre d argument : %d\n",argc);



argv[0]="./huffman";

argv[1]="data.txt";

argv[2]="data.z";



if (argc==1)

{

printf("\n Ceci est un programme
de compression\n Pour compresser un fichier taper : ./myCompress
<nom du fichier sources> <nom du fichier de destination>\n
Bonne compression!!!!\n");

}



comptage(argv[1],T);

Tri(T);



for(i=0;i<=255;i++)

{ printf("%d ",T[i].nbr);}



AlgoHuff(T,Huff);



return (0);

}

3 réponses

ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 7
11 janv. 2006 à 00:45
------------------------------------------------------------------
void AllocateStat(stat *i)
{
i=(stat*)malloc(sizeof(stat)*256);
}

=> marche pas (il faut un niveau de pointeur de plus pour modifier l'address du pointeur

void AllocateStat(stat **i)
{
*i=(stat*)malloc(sizeof(stat)*256);
}

ou

stat* AllocateStat()
{
return (stat*)malloc(sizeof(stat)*256);
}

----------------------------------------------------
void AllocateNode1(tree t)
{
t=(node*)malloc(sizeof(node));
t->left=NULL;
t->right=NULL;
}

=> même chose, il faut retourner t ou mettre un pointeur de plus
0
tekbright717 Messages postés 9 Date d'inscription samedi 7 janvier 2006 Statut Membre Dernière intervention 5 juin 2006
11 janv. 2006 à 01:30
J'ai un autre problème maintenent!!

J'ai résolu mon problème précédent encore autrement, merci a toi ymca2003.



voila mon nouveau code, j'ai l'impression que ma fonction algohuff ne fait rien ce qui m'étonne fortement.



Merci de m'aider.



le code :



#include <stdio.h>

#include <string.h>

#include <stdlib.h>



typedef struct node_

{

int bin;

char elt;

struct node_ *left;

struct node_ *right;

}node;



typedef node* tree;



typedef struct stat_

{

int nbr;

tree t;

}stat;







// allocation



void AllocateStat(stat *i)

{

i=(stat*)malloc(sizeof(stat)*256);

}



node* AllocateNode(char elt)

{

node* n;

n=(node*)malloc(sizeof(node));

n->elt=elt;

n->left=NULL;

n->right=NULL;

return (n);

}







//ETAPE 1 & 2

void comptage(char *filename,stat *T)

{

int i;

char bufferLine[1024];

char *s;

int gh;

//initialisation



memset(bufferLine, 0,sizeof(bufferLine));



for(i=0;i<=255;i++)

{

T[i].nbr=0;

}

//fin initialisation

FILE *file;

file=fopen(filename,"r");

AllocateStat(T);



while(fgets(bufferLine,1024,file))

{

s=bufferLine;

gh=strlen(s);

for(i=0;i<=gh;i++)

{

T[(int)bufferLine[i]].nbr++;

T[(int)bufferLine[i]].t=AllocateNode(bufferLine[i]);

}

}

}





//ETAPE 3

int compare(const void *a,const void *b){



stat *j=(stat*) a ;

stat *k=(stat*) b ;



return((k->nbr)-(j->nbr));

}



void Tri(stat *T)

{

qsort(T,256,sizeof(stat),compare);

}





//ETAPE 4

tree AlgoHuff(stat *T, tree Huff)

{

int i=0;



while(T[1].nbr!=0)

{

while(T[i].nbr!=0)i++;

i=i-1;



Huff=(tree)malloc(sizeof(node));



Huff->right=T[i].t;

Huff->left=T[i-1].t;



T[i-1].nbr=T[i].nbr+T[i-1].nbr;

T[i].nbr=0;



T[i-1].t=Huff;



Tri(T);

}

return (Huff);

}







//ETAPE 5

//creation du code



void Code(tree t)

{

if(t==NULL)return;

if(t->left==NULL && t->right==NULL){

}return;

if(t->left!=NULL)

t->left->bin=0;

t->right->bin=1;

Code(t->left);

Code(t->right);

}



void Print(tree t)

{

int bin;

printf("\ncoucou\n");

if(t!=NULL)

{

Print(t->left);

printf("\n");

printf("%c(%d) ",(char)t->elt,bin);

printf("\ncoucou\n");

Print(t->right);

}

}







int main(int argc,char **argv)

{

stat T[256];

int i;

tree Huff=NULL;

printf("nombre d argument : %d\n",argc);



argv[0]="./huffman";

argv[1]="data.txt";

argv[2]="data.z";



if (argc==1)

{

printf("\n Ceci est un programme
de compression\n Pour compresser un fichier taper : ./myCompress
<nom du fichier sources> <nom du fichier de destination>\n
Bonne compression!!!!\n");

}



comptage(argv[1],T);

Tri(T);



for(i=0;i<=255;i++)

{ printf("%d ",T[i].nbr);}



AlgoHuff(T,Huff);

Code(Huff);

Print(Huff);



return (0);

}
0
BunoCS Messages postés 15476 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 3 mai 2024 103
11 janv. 2006 à 11:53
Si tu veux des exemples de (dé)compression Huffman, tu peux en trouver dans les sources de Vecchio56

Buno
----------------------------------------
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai...
0
Rejoignez-nous