




免费预览已结束,剩余3页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
无损压缩编码实验实验报告一、 实验题目:无损压缩编码实验二、 实验要求:任选一种无损编码方式,通过C+编程实现。(1) 字符串的输入是手工输入的。(2) 通过程序实现编码,最终在屏幕上现实编码结果。三,实验分析 采用霍夫曼编码实现无损压缩编码,从键盘输入若干字符及每个字符出现的频率,将字符出现的频率作为结点的权值,建立哈夫曼树,然后对各个字符进行哈夫曼编码,最后打印输出字符及对应的哈夫曼编码,并算出平均码长。四,主要编码原理五,源程序代码#include using namespace std;class HuffmanTree/霍夫曼树结构public: unsigned int Weight, Parent, lChild, rChild;typedef char *HuffmanCode;void Select(HuffmanTree* HT,int Count,int *s2,int *s1)/从结点集合中选出权值最小的两个结点 unsigned int temp1=0; unsigned int temp2=0; unsigned int temp3; for(int i=1;i=Count;i+) if(HTi.Parent=0)if(temp1=0) temp1=HTi.Weight; (*s1)=i; else if(temp2=0)temp2=HTi.Weight;(*s2)=i; if(temp2temp1)temp3=temp2; temp2=temp1; temp1=temp3;temp3=(*s2); (*s2)=(*s1); (*s1)=temp3; elseif(HTi.Weighttemp1&HTi.Weighttemp2)temp2=HTi.Weight; (*s2)=i; void HuffmanCoding(HuffmanTree * HT, HuffmanCode * HC,int *Weight,int Count)/霍夫曼编码函数int i;int s1,s2;int TotalLength; char* cd; unsigned int c;unsigned int f;int start;if(Count=1) return; TotalLength=Count*2-1;HT = new HuffmanTree(TotalLength+1)*sizeof(HuffmanTree);for(i=1;i=Count;i+)HTi.Parent=0;/父节点HTi.rChild=0;/左孩子HTi.lChild=0;/右孩子 HTi.Weight=(*Weight); Weight+; for(i=Count+1;i=TotalLength;i+) HTi.Weight=0; HTi.Parent=0; HTi.lChild=0; HTi.rChild=0; /建造霍夫曼树 for(i=Count+1;i=TotalLength;+i)Select(HT, i-1, &s1, &s2);HTs1.Parent = i;HTs2.Parent = i;HTi.lChild = s1;HTi.rChild = s2;HTi.Weight = HTs1.Weight + HTs2.Weight; /输出霍夫曼编码 (*HC)=(HuffmanCode)malloc(Count+1)*sizeof(char*); cd = new charCount*sizeof(char); cdCount-1=0; for(i=1;i=Count;+i) start=Count-1; for(c = i,f = HTi.Parent; f != 0; c = f, f = HTf.Parent) if(HTf.lChild = c) cd-start=0; else cd-start=1; (*HC)i = new char (Count-start)*sizeof(char); strcpy(*HC)i, &cdstart); delete HT; delete cd; int LookFor(char *str, char letter, int count)/在字符串中查找某个字符,如果找到,则返回其位置 int i; for(i=0;icount;i+) if(stri=letter) return i; return -1;void Quanzhi(char *Data,int Length,char *WhatLetter,int *Weight,int *Count)/计算权值并输出 int i; char* Letter = new charLength; int* LetterCount = new intLength; int AllCount=0; int Index; int Sum=0; float Persent=0; for(i=0;iLength;i+) if(i=0) Letter0=Datai; LetterCount0=1; AllCount+; else Index=LookFor(Letter,Datai,AllCount); if(Index=-1) LetterAllCount=Datai; LetterCountAllCount=1; AllCount+; elseLetterCountIndex+; for(i=0;iAllCount;i+)/计算平均码长 Sum=Sum+LetterCounti; (*Weight) = new intAllCount; (*WhatLetter) = new charAllCount; for(i=0;iAllCount;i+) Persent=(float)LetterCounti/(float)Sum; (*Weight)i=(int)(100*Persent); (*WhatLetter)i=Letteri; (*Count)=AllCount;delete Letter;delete LetterCount;int main()/主函数调用HuffmanTree * HT = NULL;HuffmanCode HC; char Data100;/储存输入的字符串char *Letter;int *Weight;int Count;cout*欢迎使用霍夫曼编码器*endl;cout请输入一行字符串:Data;coutendl;Quanzhi(Data,strlen(Data),&Letter, &Weight,&Count);HuffmanCoding(HT, &HC, Weight, Count);cout字符出现频率和编码结果endl;double K=0;double L;char P100;for(int i = 0; iCount; i+)coutLetteri ;/字符coutWeighti%t;/出现频率coutHCi+1endl;/霍夫曼码strcpy(P,HCi+1);L=strlen(P);K=K+L*Weighti/100;cout平均码长K;coutendl;system(pause);retu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 第1节 化学键与化学反应说课稿-2025-2026学年高中化学鲁科版必修2-鲁科版2004
- 走访营销方案
- 汉阳钢结构楼梯施工方案
- 客户咨询营销活动方案
- 花灯营销方案
- 国际本科咨询留学方案
- §6 余弦函数的图像与性质教学设计高中数学北师大版2011必修4-北师大版2006
- 1 土壤的成分说课稿-2025-2026学年小学科学三年级上册(2024)青岛版(五四制2024)
- 浙教版科学八年级上册 2.3《大气的压强(第3课时)》教学设计
- 网络信息安全保护措施方案
- 骨科围手术期压疮预防护理
- 2024年事业单位考试四川省甘孜藏族自治州A类《职业能力倾向测验》全真模拟试题含解析
- 温岭市建房用地管理办法
- 第一章勾股定理问题解决策略反思课件北师大版数学八年级上册
- (2025)辅警考试公安基础知识考试真题库及答案
- 2025年上海市中考语文试卷真题(含答案及解析)
- 布依山歌教学课件
- T/CRACM 0003-2021脂20科学减脂技术服务规范
- 2025年铁路客运值班员(高级)职业技能鉴定参考试题库(含答案)
- 解码国家安全知到智慧树章节测试课后答案2024年秋国际关系学院
- 输液反应处理及报告流程
评论
0/150
提交评论