附录一:哈夫曼编码分析与实现C语言源程序#includestdio.h#includemath.h#definew10floata[w],b[w][w]={0},f[w]={0};inti,c[w][w][w],d[w]={0},m;xiaoxi(){intn;floatP=0;printf(\n请分别输入消息概率\n\a);for(n=0;nm;n++){scanf(%f,&a[n]);if(a[n]=1||a[n]=0){printf(出错,概率应在[0,1]范围内\n);return(0);break;}P+=a[n];}if(P!=1){printf(出错,概率和应为1\n);return(0);}elsereturn(1);}ordination(intf,float*e){intg,j;floatk;for(g=0;gf-1;g++)for(j=g+1;jf;j++)if(e[g]e[j]){k=e[g];e[g]=e[j];e[j]=k;}}coding(){intj,k,t,r;i=m-3;r=0;c[i+1][0][0]=0;c[i+1][1][0]=1;for(;i=0;i--){t=0;for(k=m-2-i;k=0;k--){if((f[i]==b[i+1][k])&&(t==0)){t=1;for(r=0;c[i+1][k][r]!=2;r++){c[i][m-i-2][r]=c[i+1][k][r];c[i][m-i-1][r]=c[i+1][k][r];}c[i][m-i-2][r]=0;c[i][m-i-1][r]=1;}}for(j=m-i-3;j=0;j--)for(k=m-2-i;k=0;k--)if(b[i][j]==b[i+1][k])for(r=0;c[i+1][k][r]!=2;r++)c[i][j][r]=c[i+1][k][r];}}add(){intj;for(i=0;im;i++)b[0][i]=a[i];for(i=1;im;i++){b[i][m-i-1]=b[i-1][m-i-1]+b[i-1][m-i];f[i-1]=b[i][m-i-1];for(j=0;jm-i-1;j++)b[i][j]=b[i-1][j];ordination(m-i,b[i]);}}main(){intn,x,y;floatK=0,H=0;for(n=0;nw;n++)for(x=0;xw;x++)for(y=0;yw;y++)c[n][x][y]=2;printf(\n请输入消息个数\n\n\a);scanf(%d,&m);printf(\n);y=xiaoxi();if(y=1);{ordination(m,a);add();coding();printf(\n编码过程\n);for(n=0;nm;n++){printf(\n第%d列:,n+1);for(x=0;xm;x++){if(b[n][x]==0)break;printf(\t%5.4f,b[n][x]);}printf(\n);}printf(\n);for(n=0;nm;n++){printf(概率为%5.4f的符号编码后码字为:\t,a[n]);for(x=0;xm;x++){if(c[0][n][x]==2)break;printf(%d,c[0][n][x]);d[n]++;}K+=a[n]*d[n];H+=(-a[n]*log10l(a[n]))/log10l(2);printf(\t其码长为:%d\n,d[n]);}printf(\n平均码长K=);printf(%3.2f,K);printf(\n信源熵=%3.2f,H);printf(\n编码效率η=(H/K)=%3.2f%%,H*100/K);printf(\n冗余度γ=(1-η)=%3.2f\n,1-H/K);}}