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++)
{
相关文档:
经过验证可以实现,先将实现代码贴出,以备以后使用。
本文默认你的java开发环境已经安装成功,并且对于java语言和c++语言有过了解。
编写测试用类:Demo.java
代码如下:
public class Demo
{
public static int COUNT = 8;
private String msg;
private int[] counts;
......
1:每一个变量在使用前都得声明,不然在使用的时候就有可能是随机的数字
2:注意头文件中函数声明的时候要在后面加上分号
3:注意串口可以打印变量,就像C中的printf一样
4:DNW中不能打印float型数据
5:注意结构体指针数组 的使用和调用
6: ......
write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie
讨论新闻组及文件
Technorati 标签: JAVA
,C++
,Python
JAVA is not just another programming language. -- 匿名
学习更新的语言,有助于了解别人对旧语言有哪些不满。 -- 匿名
前言
先
说明本文的行文习惯,文章写作流程以本人阅读《Java Progra ......
什么是好的程序员?是不是懂得很多技术细节?还是懂底层编程?还是编程速度比较快?
我觉得都不是。对于一些技术细节来说和底层的技术,只要看帮助,查资料就能找到,对
于速度快,只要编得多也就熟能生巧了。
我认为好的程序员应该有以下几方面的素质:
1、有专研精神,勤学善问、举一反 ......
21、goto语句的使用
—————————
N年前,软件开发的一代宗师——迪杰斯特拉(Dijkstra)说过:“goto statment is
harmful !!”,并建议取消goto语句。因为goto语句不利于程序代码的维护性。
这里我也强烈建议不要使用go ......