免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
从文件中读取下列二元编码00001110010101100001110001110001111010,实现其游程编码,然后再对游程序列进行哈弗曼编码。结果保存在out.dat中。程序代码:#include#include#includestruct nodeint id;int num;double probability;typedef structdouble weight;int parent,lchild,rchild;HuffmanTree;void Select(HuffmanTree *HT,int i,int *s1,int *s2)int n,T=0,T1;for(n=1;ni;n+)if(HTn.weight=HTT.weight)&HTn.parent=0)T=n;*s1=T;T1=T;T=0;for(n=1;ni;n+)if(n=T1) continue;if(HTn.weight=HTT.weight)&HTn.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;HT0.weight=10000;w+;for(p=HT+1,i=1;iweight=*w;p-lchild=0;p-rchild=0;p-parent=0;for(;iweight=0;p-lchild=0;p-rchild=0;p-parent=0;for(i=n+1;i=m;+i)Select(HT,i,&s1,&s2);HTs1.parent=i;HTs2.parent=i;HTi.lchild=s1;HTi.rchild=s2;HTi.weight=HTs1.weight+HTs2.weight;cd=malloc(n*sizeof(char);cdn-1=0;for(i=1;i=n;+i)start=n-1;for(c=i,f=HTi.parent;f!=0;c=f,f=HTf.parent)if(HTf.lchild=c)cd-start=1;elsecd-start=0;HCi=(char *)malloc(n-start)*sizeof(char);strcpy(HCi,&cdstart);free(cd);void initstruct(struct node p,int T)int i;for(i=1;i=T;i+)pi.id=0;pi.num=0;bability=0.0;int getI(struct node p,int e,int T)int i;for(i=1;i=T;i+)if(pi.id=e)return i;return -1;void count(struct node p,int T)int i;for(i=1;i=T;i+)pgetI(p,pi.id,T).num+;int countevent(struct node p,int T)int i,n=0;for(i=1;i=T;i+)if(pi.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(pi.num!=0)p2n+=pi;void calculateprobability(struct node p2,int T)int i,sum=0;for(i=1;i=T;i+)sum=sum+p2i.num;for(i=1;i=T;i+)bability=p2i.num/1.0/sum;void main()int n=0,m=0,i=1,t;char ch1,ch2=#;char *HC;FILE *fp1,*fp2;struct node *p,*p2;HuffmanTree *HT;double *w;if(fp1=fopen(in.dat,rb)=NULL)printf(不能打开文件!n);exit(1);if(fp2=fopen(out.dat,a)=NULL)printf(不能打开文件!n);exit(1);fprintf(fp2,n对于二元序列:);doch1=fgetc(fp1);if(ch1!=EOF)fprintf(fp2,%c,ch1);elsefprintf(fp2,n);n+;if(ch1!=ch2)m+;ch2=ch1;while(ch1!=EOF);fclose(fp1);if(fp1=fopen(in.dat,rb)=NULL)printf(不能打开文件!n);exit(1);t=m-1;p=(struct node *)malloc(m)*sizeof(struct node);initstruct(p,t);n=m=0;doch1=fgetc(fp1);n+;if(ch1!=ch2)if(n!=1)pi+.id=n-m;m=n;ch2=ch1;while(ch1!=EOF);fclose(fp1);fprintf(fp2,游程序列为:);for(i=1;i=t;i+)fprintf(fp2,%d,pi.id);fprintf(fp2,n);count(p,t);p2=(struct node *)malloc(countevent(p,t)+1)*sizeof(struct node);order(p,p2,t);calculateprobability(p2,countevent(p,t);HT=malloc(countevent(p,t)*sizeof(HuffmanTree);HC=malloc(countevent(p,t)+1)*sizeof(char *);w=(double *)malloc(countevent(p,t)+1)*sizeof(double);t=countevent(p,t);for(i=1;i=t;i+)wi=bability;for(i=1;i=t;i+)pi=p2i;HuffmanCoding(HT,HC,w,t);fprintf(fp2,该信源的Huffma
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025企业用工劳动合同(软件开发)
- 2025家具采购安装合同范本 办公场所转让格式
- 行政公文撰写规范与格式化文档
- 企业合作合规协定签订承诺函7篇
- 企业合规审计风险识别清单模版
- 一次难忘的同学生日聚会记事作文7篇范文
- 校园服务诚信保证承诺书4篇范文
- 演讲稿:阅读的力量书籍如何改变我们的生活15篇范文
- 企业年度计划制定与目标分解模板
- 培训项目课程计划执行标准模板
- 急性心肌梗死护理管理指南
- 企业信息安全培训课件
- 铝板板材外墙施工技术交底
- 2025下半年四川省自然资源投资集团社会招聘考试笔试参考题库附答案解析
- 2025山东省教育厅直属事业单位省教育发展服务中心第二批招聘9人考试笔试模拟试题及答案解析
- 2025年入党积极分子发展对象考试通关试卷提供答案解析含完整答案详解【典优】
- 《祖国啊我亲爱的祖国》课件
- 腾讯手机行业消费趋势洞察报告(2025年版)-腾讯营销洞察x益普索-202510
- 公会主播停播合同范本
- 金属行业入门知识培训课件
- 2025至2030全球及中国高光谱传感器行业发展趋势分析与未来投资战略咨询研究报告
评论
0/150
提交评论