zahra6688
چهارشنبه 26 دی 1386, 18:01 عصر
من به کد هافمن احتیاج دارم.می خوام ساده و قابل فهم باشه
shankimout
پنج شنبه 27 دی 1386, 23:21 عصر
http://en.wikipedia.org/wiki/Huffman_coding
saeid.yavary
چهارشنبه 03 بهمن 1386, 12:46 عصر
#include<string.h>
#include<iostream.h>
#include<conio.h>
main(){
clrscr();
char convert[100][100]={'\0'},arry1[255]={'\0'},
text[255]={'\0'},temp;
int arry2[255]={0},n,i,j,count=0,k,sw=0,s;
//********* evaluate text ****************
cout<<endl<<"enter a text:";
cin.get(text,255);
k=strlen(text)-1;
cout<<endl<<"------------------------------------"<<endl;
//************ search ********************
for(i=0;i<=k;i++)
{ for(j=0;j<=k;j++)
{ if(text[i]==arry1[j])
{sw=1;
break;}
}
if(sw==0)
{arry1[count]=text[i];
arry2[count]++;
count++;}
if(sw==1)
{arry2[j]++;
sw=0;}
}
//******** sort of ary1 & ary2 **********
for(i=0;i<=k;i++)
for(j=0;j<k;j++)
{if(arry2[j]<arry2[j+1])
{temp=arry1[j];
arry1[j]=arry1[j+1];
arry1[j+1]=temp;
n=arry2[j];
arry2[j]=arry2[j+1];
arry2[j+1]=n;}
}
//********* moshakhs kardan kod***********
for(i=0;i<count;i++)
{ for(j=0;j<=i;j++)
{
convert[i][j]='1';
}
if(i!=count-1)
convert[i][i+1]='0';
}
//***** show code of any character *******
cout<<endl<<"code of any character is:"<<endl<<endl;
for(i=0;i<count;i++)
{cout<<arry1[i]<<" = ";
for(j=0;j<=i+1;j++)
{cout<<convert[i][j];}
cout<<endl<<endl;
}
cout<<endl<<"------------------------------------";
//***** repeat of any character is *******
cout<<endl;
cout<<endl<<"repeat of any character is : "<<endl<<endl;
for(i=0;i<count;i++)
{cout<<"character : "<<arry1[i];
cout<<" \trepeat:"<<arry2[i]<<endl<<endl;
}
cout<<"------------------------------------"<<endl;
//***** show of the convert text *********
cout<<endl;
cout<<"convert of text: ";
for(i=0;i<=k;i++)
for(j=0;j<=count;j++)
{ if(text[i]==arry1[j])
{s=0;
while(convert[j][s])
{cout<<convert[j][s];
s++;}
}
}
getch();
return 0;
}
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.