c语言 游程编码,游程编码C语言.doc

游程编码C语言

从文件中读取下列二元编码00001110010101100001110001110001111010,实现其游程编码,然后再对游程序列进行哈弗曼编码。

结果保存在out.dat中。

程序代码:

#include

#include

#include

struct node{

int id;

int num;

double probability;

};

typedef struct{

double weight;

int parent,lchild,rchild;

}HuffmanTree;

void Select(HuffmanTree *HT,int i,int *s1,int *s2)

{

int n,T=0,T1;

for(n=1;n

if((HT[n].weight<=HT[T].weight)&&HT[n].parent==0)

T=n;

*s1=T;

T1=T;

T=0;

for(n=1;n

{

if(n==T1) continue;

if((HT[n].weight<=HT[T].weight)&&HT[n].parent==0)

T=n;

}

*s2=T;

}

void HuffmanCoding(HuffmanTree *HT,char **HC,double *w,int n)

{

int m,i,start;

int s1,s2,f,c;

char *cd;

HuffmanTree *p;

if(n<=1) return;

m=2*n-1;

HT[0].weight=10000;

w++;

for(p=HT+1,i=1;i<=n;++i,++p,++w)

{

p->weight=*w;

p->lchild=0;

p->rchild=0;

p->parent=0;

}

for(;i<=m;++i,++p)

{

p->weight=0;

p->lchild=0;

p->rchild=0;

p->parent=0;

}

for(i=n+1;i<=m;++i)

{

Select(HT,i,&s1,&s2);

HT[s1].parent=i;

HT[s2].parent=i;

HT[i].lchild=s1;

HT[i].rchild=s2;

HT[i].weight=HT[s1].weight+HT[s2].weight;

}

cd=malloc(n*sizeof(char));

cd[n-1]='\0';

for(i=1;i<=n;++i)

{

start=n-1;

for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)

if(HT[f].lchild==c)

cd[--start]='1';

else

cd[--start]='0';

HC[i]=(char *)malloc((n-start)*sizeof(char));

strcpy(HC[i],&cd[start]);

}

free(cd);

}

void initstruct(struct node p[],int T)

{

int i;

for(i=1;i<=T;i++)

{

p[i].id=0;

p[i].num=0;

p[i].probability=0.0;

}

}

int getI(struct node p[],int e,int T)

{

int i;

for(i=1;i<=T;i++)

{

if(p[i].id==e)

return i;

}

return -1;

}

void count(struct node p[],int T)

{

int i;

for(i=1;i<=T;i++)

{

p[getI(p,p[i].id,T)].num++;

}

}

int countevent(struct node p[],int T)

{

int i,n=0;

for(i=1;i<=T;i++)

{

if(p[i].num!=0)

n++;

}

return n;

}

void order(struct node p[],struct node p2[],int T)

{

int i,n=1;

for(i=1;i<=T;i++)

{

if(p[i].num!=0)

p2[n++]=p[i];

}

}

void

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值