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++)
{
相关文档:
关于C的思考
Cong Wang
May, 2006
Network Engineering Department
Institute of Post and Telecommunications, Xi'an, P.R.China
引言
C语言结合了汇编的所有威力,它的抽象程度碰巧既满足了程序员的要求, 又容易实现。因其独特的灵活性和强大的可移植性,系统程序员和黑客们更是对它钟爱 ......
以下全文转载自http://www.keil.com/support/docs/1671.htm Copyright © 2010 Keil™, An ARM® Company.
Information in this article applies to:
C51 Version 6.02
&mi ......
C/C++是最主要的编程语言。这里列出了50名优秀网站和网页清单,这些网站提供c/c++源代码。这份清单提供了源代码的链接以及它们的小说明。我已尽力包括最佳的C/C++源代码的网站。这不是一个完整的清单,您有建议可以联系我,我将欢迎您的建议,以进一步加强这方面的清单。
1、http://snippets.dzone.com/tag/c/ --数以千计 ......
write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie
讨论新闻组及文件
Technorati 标签: JAVA
,C++
,Python
JAVA is not just another programming language. -- 匿名
学习更新的语言,有助于了解别人对旧语言有哪些不满。 -- 匿名
前言
先
说明本文的行文习惯,文章写作流程以本人阅读《Java Progra ......