易截截图软件、单文件、免安装、纯绿色、仅160KB

c程序实现香农编码

#include <stdio.h>
#include <math.h>
#include <string.h>
int i,j,n,k,b;
float addp;
char bitw[20];
/*初始化结构体其中:s为信源符号;p为信源符号概率;padd为累加概率;
l_f为-log[p(s[i])]即估计码字长度;l为实际需要码字长度;w为码字*/
 struct shan
 {char s[20];
  float p;
  float padd;
  float l_f;
  int l;
  char w[20];
 }data[12];
/*依信源概率对各信源符号排序Moai&Kevin原创!*/
void sequ(struct shan x[],int n)
{
 struct shan temp;
 for(i=0;i<n;i++)
  for(j=i;j<n;j++)
  {if(x[i].p<x[j].p)
   {
  temp=x[j];
  x[j]=x[i];
  x[i]=temp;
   }
  }
}
/*计算累加概率*/
void countpadd(struct shan x[],int n)
{
  addp=0;
  x[0].padd=0;
  for(i=0;i<n;i++)
  {
 addp+=x[i].p;
 x[i+1].padd=addp;
  }
}
/*计算估计码字长度l_f,以及Moai&Kevin原创!*/码字长度l*/
void count_l(struct shan x[],int n)
{
  for(i=0;i<n;i++)
  {
 x[i].l_f=-log(x[i].p)/log(2);
    if((x[i].l_f-(int)x[i].l_f)>0)
  x[i].l=(int)x[i].l_f+1;
 else x[i].l=(int)x[i].l_f;
  }
}
/*二进制转换*/
void covbit(float a,int lc)
{
  for(j=0;j<lc;j++)
  { 
   b=(int)(a*2);
   bitw[j]=b+48;
   a=2*a-int(a*2);
  }
 }
main()
{
 printf("please input the number of symbols of source(n<=10):n=");
 scanf("%d",&n);
 printf("please input the the source symbols and their probabilities\n");
 /*获取信源符号*/
 for(i=0;i<n;i++)
 {
 scanf("%s",data[i].s);
 }
 /*获取信源Moai&Kevin原创!*/概率*/
 for(i=0;i<n;i++)
 {printf("P(%s)=",data[i].s);
  scanf("%f",&data[i].p);
 }
 sequ(data,n);
 countpadd(data,n);
 count_l(data,n);
 /*在结构体中产生码字*/
 for(i=0;i<n;i++)
 {


相关文档:

ARM第一个项目中关于C的总结

1:每一个变量在使用前都得声明,不然在使用的时候就有可能是随机的数字
2:注意头文件中函数声明的时候要在后面加上分号
3:注意串口可以打印变量,就像C中的printf一样
4:DNW中不能打印float型数据
5:注意结构体指针数组 的使用和调用
6: ......

A51: CALLING C FUNCTIONS from ASSEMBLY

以下全文转载自http://www.keil.com/support/docs/697.htm                       Copyright © 2010 Keil™, An ARM® Company.
Information in this article applies to:
C51 Versio ......

C51: CALLING ASSEMBLY ROUTINES from C

以下全文转载自http://www.keil.com/support/docs/50.htm                            Copyright © 2010 Keil™, An ARM® Company.
Information in this ar ......

从C++到JAVA,C++程序员学习JAVA的指南

write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie
讨论新闻组及文件
Technorati 标签: JAVA
,C++
,Python
JAVA is not just another programming language. -- 匿名
学习更新的语言,有助于了解别人对旧语言有哪些不满。 -- 匿名
 
前言

说明本文的行文习惯,文章写作流程以本人阅读《Java Progra ......

MJ评 清华c语言门(一篇C作业可以钓到女友?)

      只是有人牵涉到写程序的,我就说说.女主角如果不出来澄清就一辈子被毁了.校风严谨的清华也有这种滥S的行为有点不可思意.找程序员写作业只是个分手的借口吧.女主角还瞒清秀的.出来讨债的"冤大头"其实也不理智,毁了前女友的幸福很好吗?
      但愿C语言门只是个娱乐 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号