Huffman

totomus Messages postés 1 Date d'inscription jeudi 6 janvier 2005 Statut Membre Dernière intervention 25 janvier 2005 - 25 janv. 2005 à 16:07
coucou747 Messages postés 12303 Date d'inscription mardi 10 février 2004 Statut Membre Dernière intervention 30 juillet 2012 - 25 janv. 2005 à 18:02
Salut tou tle monde



J'ai trouvé un petit programme sur le site à propos de la compression
de huffmann, mais malheureusement, comme je suis débutant dans la
programmation j'ai un peu de mal à tout comprendre.

C'est pourquoi j'aimerai savoir s'il y a quelqun qui pourrai m'aider en me mettant des commentaires sur le programme..



"



struct arbre

{

unsigned char m;

unsigned long z;

struct arbre *pere, *g, *d;

};



struct arbre ar[512], *deb;







void EDIFICE();

void CMP(FILE *e, struct arbre *h, struct arbre *fils);

void ECRIRE(FILE *e, int b);

char oct;

int oct1, p;







//------------------------------------------------------------------------------------



void EDIFICE()

{

int i, n1 = 256;

while(1)

{

struct arbre *u NULL, *v NULL;

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

if (ar + i != u)


if ((ar[i].z > 0) && (ar[i].pere == NULL))


if ((u == NULL) || (ar[i].z < u->z))

{


if ((v == NULL) || (u->z < v->z))


v = u;

u = ar + i;

}


else if ((v == NULL) || (ar[i].z < v->z))


v = ar + i;

if (v == NULL)

{

deb = u;

break;

}

u->pere = ar + n1;

v->pere = ar + n1;

ar[n1].z = u->z + v->z;

ar[n1].d = u;

ar[n1].g = v;

n1++;

}

}





//------------------------------------------------------------------------------------





void CMP(FILE *e, struct arbre *h, struct arbre *fils)

{

if (h->pere != NULL)

CMP(e, h->pere, h);

if (fils)

{

if (fils == h->d)

ECRIRE(e, 0);

else if (fils == h->g)

ECRIRE(e, 1);

}

}





//------------------------------------------------------------------------------------





void ECRIRE(FILE *e, int b)

{

if ((p 8) || (b -1))

{

fputc(oct, e);

p = 0;

}

oct = (oct << 1) | b;

p++;

}





//------------------------------------------------------------------------------------



int DCMP(FILE *s, struct arbre *h)

{

while (h->d != NULL)

if (LIRE(s))

h = h->g;

else

h = h->d;

return( h->m );

}





//------------------------------------------------------------------------------------





int LIRE(FILE *s)

{

int r;

if (p == 8)

{

oct1 = fgetc(s);

p = 0;

}

r = oct1 & 0x80;

oct1 <<= 1;

p++;

return(r);

}





//------------------------------------------------------------------------------------





int compresse ( char entree[255], char sortie[255] )

{

FILE *s, *e;

int a, c, f = 0;

unsigned char z;

unsigned long n = 0;



if ( ( (s = fopen(entree,"rb") )== NULL))

{

return 601;

}

if (((e fopen(sortie,"wb")) NULL))

{

return 602;

}



while (((c = fgetc(s)) != EOF))

{

c &= 255;

if (ar[c].z == 0)

{

ar[c].m=c;

f++;

}

ar[c].z++;

n++;

}

fwrite(&n, sizeof(n), 1, e);

fwrite(&f, sizeof(f), 1, e);

for (c=0; c<256; c++)

{

if (ar[c].z > 0)

{


fwrite(&ar[c].m, sizeof(char), 1, e);


fwrite(&ar[c].z, sizeof(unsigned long), 1, e);

}

}

EDIFICE();

fseek(s, 0L, 0);

while ((c=fgetc(s)) != EOF)

CMP(e, ar+(c&255), NULL);

ECRIRE(e, -1);



fclose (e);

fclose(s);

return 100;



}



//------------------------------------------------------------------------------------





int decompresse ( char entree[255], char sortie[255] )

{

FILE *s, *e;

int a, c, f = 0;

unsigned char z;

unsigned long n = 0;



if ( ( (s = fopen(entree,"rb") )== NULL))

{

return 701;

}

if (((e fopen(sortie,"wb")) NULL))

{

return 702;

}

p = 8;

fread(&n, sizeof(n), 1, s);

fread(&f, sizeof(f), 1, s);

while(f--)

{

fread(&z, sizeof(char), 1, s);

ar[z].m = z;

fread(&ar[z].z, sizeof(unsigned long), 1, s);

}

EDIFICE();

while (n--)

fputc(DCMP(s, deb), e);

fclose (e);

fclose(s);

return 100;

}





//------------------------------------------------------------------------------------





main ()

{

int choix;

char entree[255],sortie[255];



do {

printf ("Compression de Huffman\n"

"======================\n\n"

"\t1 - Compresser\n"

"\t2 - Decompresser\n"

"\t3 - Quitter\n"

"Entrez votre choix : ");

scanf ("%d",&choix);

switch (choix)

{

case 1 : printf ("Entrez le nom du fichier source : ");

scanf ("%s",entree);

sprintf (sortie,"%s.huf",entree);

compresse (entree,sortie);

break;



case 2 : printf ("Entrez le nom du fichier compressé : ");

scanf ("%s",entree);

sprintf (sortie,"%s",entree);

sortie[strlen(sortie) - 4] = '\0';

decompresse (entree,sortie);

break;

}

}

while (choix != 3 );

}





"

Merci d'avance

++

Troylen

1 réponse

coucou747 Messages postés 12303 Date d'inscription mardi 10 février 2004 Statut Membre Dernière intervention 30 juillet 2012 44
25 janv. 2005 à 18:02
euh... je ne connais pas ce programme, mieux vaut que u demandes à l'auteur...

Tu veux comprendre quoi ? comment fonctionne huffman ? (t'as un numéro de login la dessus, si je le retreouve, je t'indiquerais le n° et les liens...) ou comment fonctionne ce programme ?

In a dream, I saw me, drop dead... U was here, U cried... It was just a deam, if I die, U won't cry, maybe, U'll be happyhttp://coucou747.hopto.org
0
Rejoignez-nous