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
| #include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<windows.h>
#include<ctype.h>
int attribution ( char tableau[] , int ptabl, char bit[])
{
int x =0 ;
for (x=0;bit[x]!='\0';x++ )
{
if ((bit[x]!='0')&&(bit[x]!='1'))
{
cout<<"erreur dans la chaine de bit"<<endl;
getchar();
exit(0);
}
tableau[ptabl] = bit[x];
ptabl++;
}
tableau[ptabl] = ' ';
ptabl++;
return ptabl ;
}
int main()
{ for(;;){
system("cls");
cout<<"\tComparaison entre Huffman 32 et ASCII 5 bits\n\n";
const int max = 20000;
char bascii[10000]={'\0'};
char bhuf[10000]={'\0'};
char bgene[10000]={'\0'};
int p, phuf = -1 , pascii = -1 , pgene = -1 ;
pgene = attribution(bgene,pgene,"1");
char texte[max];
int count=0 , bits=0 , ascii , debug=0 ;
char choix;
float gain=0;
cout<<"Entrez une phrase pour connaitre lequels des codes est le plus avantageux"<<endl;
cin.ignore(cin.rdbuf()->in_avail());
cin.getline(texte, max, '\n');
for (int x=0;texte[x]!='\0';x++)
{
texte[x]=tolower(texte[x]) ;
}
boucle:
debug=0;
while (texte[count] == 'a')
{
count++;
debug++;
//cout<<"1100";
phuf = attribution(bhuf,phuf,"0100");
pgene = attribution(bgene,pgene,"011001");
pascii = attribution(bascii,pascii,"00001");
}
if (texte[count] == '\0')
{debug=0;
goto end;}
if (debug == 0)
{count++;}
goto boucle ;
end:
int bitascii =pascii+1-count;
int bitgene =pgene+1-count-count;
int bithuf =phuf+1-count;
cout<<"\nCette chaine contient "<<count<<" de caracteres"<<endl;
if ((bitascii<bithuf) && (bitascii<bitgene))
{
cout<<"Cette chaine est plus avantageuse a coder en ascii"<<endl;
cout<<"La chaine prend "<<(bithuf-bitascii)*100/bitascii<<"% de moins par raport a huffman soi "<<(bithuf-bitascii)<<" bits"<<endl;
cout<<"La chaine prend "<<(bitgene-bitascii)*100/bitascii<<"% de moins par raport au code genetique soi "<<(bitgene-bitascii)<<" bits"<<endl;
}
if ((bithuf<bitascii) && (bithuf<bitgene))
{
cout<<"Cette chaine est plus avantageuse a coder en Huffman"<<endl;
cout<<"La chaine prend "<<(bitascii-bithuf)*100/bithuf<<"% de moins par raport a ascii soi "<<(bitascii-bithuf)<<" bits"<<endl;
cout<<"La chaine prend "<<(bitgene-bithuf)*100/bithuf<<"% de moins par raport au code genetique soi "<<(bitgene-bithuf)<<" bits"<<endl;
}
if ((bitgene<bithuf) && (bitgene<bitascii))
{
cout<<"Cette chaine est plus avantageuse a coder avec le codage genetique"<<endl;
cout<<"La chaine prend "<<(bitascii-bitgene)*100/bitgene<<"% de moins par raport a ascii soi "<<(bitascii-bitgene)<<" bits"<<endl;
cout<<"La chaine prend "<<(bithuf-bitgene)*100/bitgene<<"% de moins par raport a huffman soi "<<(bithuf-bitgene)<<" bits"<<endl;
}
choix:
cout<<"\n\nQue voulez-vous faire :\n\t1 - Recommencer\n\t2 - Voire les chaine de bits\n\t3 - Quitter\n";
cin>>choix;
if (!isdigit(choix))
{
system ("cls");
goto choix;
}
switch (choix)
{ case '1' :
//cin.ignore(cin.rdbuf()->in_avail());
break;
case '2' :
cout<<"\nVoici la phrase coder en Huffman, elle comporte "<<bithuf<<" bits"<<endl;
for (int x=-1;x<phuf;x++)
{
cout<<bhuf[x];
}
cout<<"\nVoici la phrase avec le codage genetique, elle comporte "<<bitgene<<" bits"<<endl;
for (int x=-1;x<pgene;x++)
{
cout<<bgene[x];
}
cout<<"\nVoici la phrase coder en Ascii, elle comporte "<<bitascii<<" bits"<<endl;
for (int x=-1;x<pascii;x++)
{
cout<<bascii[x];
}
goto choix;
break;
case '3' : return 0;
break;
default : goto choix;
}
}
} |
Partager