费诺编码
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); }
相关文章推荐
- HDU 1710 二叉树水题
- POJ 1463 Strategic game
- Eclipse代码格式化无效解决方案
- JSTL标准标签库核心标签的使用
- thinkphp 时间戳转为日期时间格式
- 无限制,开放式匿名聊天,陌路人,websocket
- windows下mysql客户端输入中文显示??解决方法
- 调漂技巧总结
- prach定点化处理
- 解决Centos/Redhat,命令不存在
- linux-文件,目录管理
- Android源码解析Window系列第(三)篇---WindowManager基本原理
- opengl中视窗设置函数
- Becoming an WPA Xpert Part 12: Timing User Login Credentials (Sometimes it IS the user)
- leetcode 76 Minimum Window Substring/ 209 Minimum Size Subarray Sum
- hadoop之shuffle------>分区机制Partitioner
- 第十六周项目1-(4)堆排序
- 折腾的aptana
- C 静态库与动态库的区别
- 第十五周项目一字符串处理