您的位置:首页 > 其它

费诺编码

2016-12-13 21:48 197 查看
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
static char p[11][10];
static int flag1=0;
static int ncount1;

//...............main........................

void main()
{
void Fano(int m,int n,float y[10]);
int i,j,flag,ncount;
float a[11],temp,s=0.0,code_length=0.0;
flag=0;
do
{
printf("Note: Quit the program when ncount=0 !!!\n");
printf("Please input the amount of probabilities,ncount:\n");
scanf("%d",&ncount);
if(ncount==0) exit(0);
while(ncount<0)
{
printf("Please input the positive integer! Try again!\n");
printf("Note: Quit the program when ncount=0 !!!\n\n");
printf("Please input the amount of probabilities,ncount:\n");
scanf("%d",&ncount);
if(ncount==0) exit(0);
}
while(flag==0)
{
flag=1;
printf("Please all probability!\n");
for(i=1;i<ncount+1;i++)
{
scanf("%f",&a[i]);
if((a[i]<1e-10)||(a[i]-1>1e-10))
{printf("Please input the number which is between 0 and 1\n");flag=0;}
else s=s+a[i];
if(s-1>1e-10)
{ printf("Please make sure that the sum of all numbers is 1 !!\n");flag=0;s=0;}

}
}
for(i=1;i<ncount;i++)
for(j=i+1;j<ncount+1;j++)
if(a[i]<a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}
Fano(1,ncount,a);
printf("\n\nThe fano code is:\n\n");
printf("Probability\t\tFano code\t\tcode length\n\n");
for(i=1;i<ncount+1;i++)
{

printf("  %.3f\t\t\t",a[i]);
printf("%s",p[i]);
printf(" \t\t\t %d\n",strlen(p[i]));
code_length=code_length+strlen(p[i]);
}
printf("\n The average code length is :%.3f \n",code_length/ncount);
printf("\n\n");
flag1=0;
code_length=0.0;

}while(1);
exit(0);
}

//..............Fano sub-program..............
void Fano(int m,int n,float y[11])
{
int j,k;
float sum=0.0,s=0.0,s1,z[11];
flag1++;
if(flag1==1)
ncount1=n;
if(m==n)
{return;}
for(j=1;j<ncount1+1;j++)
z[j]=y[j];
for(j=m;j<=n;j++)
sum=sum+z[j];
k=m;
do{ s1=s;
s=s+y[k++];
}while(s<=sum-s);
if((sum-2*s1)<=(2*s-sum))k--;
for(j=m;j<k;j++)
strcat(p[j],"0");
for(j=k;j<=n;j++)
strcat(p[j],"1");
Fano(m,k-1,y);
Fano(k,n,y);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: