




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、数据结构课程设计报告专业班级:信息0802姓名:赵思宇学号:0909081029指导老师:李登日期:2010年7月一、实验内容 哈夫曼编/译码器利用哈夫曼编码进行信息通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本。但是,这要求在发送端通过一个编码系统对待传数据预先编码,在接收端将传来的数据进行译码(复原)。对于双工信道(即可以双向传输信息的信道),每端都需要一个完整的编/译码系统。试为这样的信息收发站写一个哈夫曼编/译码系统。一个完整的系统应具有以下功能:(1)i:初始化(initialization)。从终端读入字符集大小n,以及n个字符和n个权值,建立哈夫曼树,并将它存于文件h
2、fmtree中。(2)e:编码(encoding)。利用已建好的哈夫曼树(如不在内存,则从文件htmtree中读入),对文件tobetran中的正文进行编码,然后将结果存入文件codefile中。(3)d:译码(decoding)。利用已建好的哈夫曼树将文件codefile中的代码进行译码,结果存入文件textfile中。(4)p:印代码文件(print)。将文件codefile以紧凑格式显示在终端上,每行50个代码。同时将此字符形式的编码写入文件codeprint中。(5)t:印哈夫曼树(tree printing)。将已在内存中的哈夫曼树以直观的方式(树或凹入表形式)显示在终端上,同时将此
3、字符形式的哈夫曼树写入文件treeprint中。二、实验目的 学习数据结构与算法的最终目的是解决实际的应用问题,特别是非数值计算类型的应用问题。本课程设计要求同学独立完成一个较为完整的应用需求分析,在完成设计和编程大型作业的过程中,深化对数据结构与算法课程中基本概念、理论和方法的理解;训练综合运用所学知识处理实际问题的能力,使同学的程序设计与调试水平有一个明显的提高。三、实验思想及分析一个完整的系统应具有以下功能:n i:初始化(initialization)。从终端读入字符集大小n,以及n个字符和n个权值,建立赫夫曼树,并将它存于文件hfmtree中。 对赫夫曼树初始化。 根据书本算法6.1
4、2,对树进行从叶子到根的逆向求每个字符的赫夫曼编码。 更新赫夫曼树,并存到hfmtree.txt中。算法6.12流程图如下:开始传入参数:结点个数ni<=n? ny动态分配内存,声明哈夫曼树ht,并对其值进行初始化建哈夫曼树,依次在ht1.i-1中select parent为0且weight最小的两个结点分配n个字符编码的头指针向量和求编码的工作区间从叶子到根逆向逐个字符求哈夫曼编码释放工作空间结束图1 算法6.12流程图n e:编码(encoding)。利用已建好的哈夫曼树,对文件tobetran中的正文进行编码,然后将结果存入文件codefile中。 将终端输入须要编码的语句逐字在已
5、建好的赫夫曼树中查找。 当在树中找到相匹配字符时,将该字符对应的赫夫曼编码用strcat()统一存到code数组。最后将code数组中的编码在终端输出并存储到codefile.txt中。n d:译码(decoding)。利用已建好的哈夫曼树将文件codefile中的代码进行译码,结果存入文件textfile中。 从codefile.txt中获取须要译码的编码组。 将编码逐一读入,并在赫夫曼中根据左0右1去查找字符。 将译好的语句在终端输出,并存至textfile.txt中。n p:打印表(treeprint)。将已建立好的赫夫曼树的存储情况在终端以表格的形式罗列出来,使树的调用看起来更直观。n
6、 t:打印图(treeprint)。将已建好的赫夫曼树以直观的图形在终端输出。 根据树的先序遍历算法,依次访问各个结点。 根据p打印出来的表,分析其所在的层次。 根据层次的大小,在终端输出相应长度的长条,来完成凹入表的输出。四、程序代码:#include<stdio.h> #include<stdlib.h>#include<string.h>typedef struct char elem; int weight; int parent,lchild,rchild; htnode,*huffmantree; /动态分配数组存储赫夫曼树typedef cha
7、r *huffmancode; /动态分配数组存储赫夫曼树编码表void initialization();void encoding();void decoding();void huffmancoding(int); void select(int,int *,int *); void printhufmfigue();void putout(int,int);void turn(int,void(* visit)(int,int);void treeprint();/-全局函数- int ipt=1,n,q,p=0,code_num=0,templen=0,diamonds,lay;in
8、t w1=100000,w2=100000,w3=100000;huffmancode hc=null; huffmantree ht;int main()char key;system("color fc");printf(" n");printf(" 赫夫曼编/译码器 n");printf(" n");printf(" 08信息 <2> 赵思宇 n");printf(" n");doprintf("nnn");printf(" n
9、");printf(" 操作菜单 n");printf(" n");printf(" i:初始化 (initialization ) n");printf(" e:编 码 (encoding ) n");printf(" d:译 码 (decoding ) n");printf(" p:打印表 (printhufmfigue ) n");printf(" t:打印图 (treeprint ) n");printf(" q:退 出 (in
10、itialization ) n");printf(" n");printf(" n");printf("nn");printf("please enter a key of the operation:");scanf("%c",&key);getchar();switch(key)case 'i':case 'i':initialization();break;case 'e':case 'e':encodin
11、g();break;case 'd':case 'd':decoding();break;case 'p':case 'p':printhufmfigue();break;case 't':case 't':treeprint();while(key!='q'&&key!='q');return 1;/* 函数功能:建立赫夫曼树并对其进行初始化。函数参数:file *fhfmtreep,将赫夫曼树的相关信息保存于此hfmtree.txt中。函数实现:实
12、现了字符集和频度的实际统计,并可将信息保存到hfmtree.txt中。函数返回值:无*/void initialization()int i,m;char style=' ','0'printf("please input the number of node:" ); scanf("%d",&n); getchar();m=2*n-1; ht=(huffmantree)malloc(m+1)*sizeof(htnode); /0号单元未用for(i=1;i<=n;i+) printf("input
13、 characters and weight(just like a 30enter):"); scanf("%c%d",&hti.elem,&hti.weight);getchar();hti.parent=hti.lchild=hti.rchild=0; huffmancoding(n);file* fhfmtreep=null;if(null=(fhfmtreep=fopen("e:hfmtree.txt","w")printf("open hfmtree.txt failed!n"
14、);elsefor(i=1;i<=n;i+) fprintf(fhfmtreep,"%c",hti.elem);fputs(hci,fhfmtreep); fputs(style,fhfmtreep); fclose(fhfmtreep);printf("every charactor has been coded and puted into e:hfmtree.txt!n");return;/-p147 算法6.12-void huffmancoding(int n) int i,m,s1,s2,start,c,f; /*weight存放n个字
15、符的权值(均char *cd; >0),构造赫夫曼树ht,并求出m=2*n-1; n个字符的赫夫曼编码hc。*/for(i=n+1;i<=m;+i) hti.elem='0' hti.weight=hti.parent=hti.lchild=hti.rchild=0; for(i=n+1;i<=m;+i) /建赫夫曼树select(i-1,&s1,&s2); /*在ht1.i-1选择parent为0hts1.parent=i;hts2.parent=i; 且weight最小的两个结点,其.hti.lchild=s1;hti.rchild=s2;
16、 序号分别为s1,s2*/hti.weight=hts1.weight+hts2.weight; /-从叶子到根逆向求每个字符的赫夫曼编码-hc=(huffmancode)malloc(n+1)*sizeof(char*); /分配n个字符编码的头指针向量cd=(char *)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(ht
17、f.lchild=c) cd-start='0' else cd-start='1' hci=(char *)malloc(n-start)*sizeof(char); /为第i个字符编码分配空间strcpy(hci,&cdstart);/从cd复制编码(串)到hcfree(cd);/释放工作空间return;/huffancoding/*函数功能:选出当前字符集中,两个最小值s1,s2.函数参数:*s1,*s2,用于存储选出的两个最小值。函数返回值:无*/void select(int n,int *s1,int *s2) int i; (*s1)=(
18、*s2)=0; for(i=1;i<=n;i+) if(hti.weight<ht(*s2).weight&&hti.parent=0&&(*s2)!=0) if(hti.weight<ht(*s1).weight) (*s2)=(*s1); (*s1)=i; else (*s2)=i; if(*s1)=0|(*s2)=0)&&hti.parent=0) if(*s1)=0) (*s1)=i; else if(*s2)=0) if(hti.weight<ht(*s1).weight) (*s2)=(*s1); (*s1)=
19、i; else (*s2)=i; if(*s1)>(*s2) i=(*s1); (*s1)=(*s2); (*s2)=i; return; /*函数功能:将终端输入的字符串编译成用0/1表示的编码,并将相应信息存入txt文档。 函数参数:temp1000,用于临时存储终端输入的字符串。 code1000,用于存储0/1编码。 tobetran.txt,用于存储终端输入的字符串。 codefile.txt,用于存储编译后的0/1编码。函数返回值:无*/void encoding()int i,j,all;char temp1000,code10000;printf("please
20、 put in the sentence you want to encoding:");/scanf("%s",temp);getchar();gets(temp);code0='0'file* ftobetranp=null;if(null=(ftobetranp=fopen("e:tobetran.txt","w")printf("open tobetran.txt failed!n");elsefputs(temp,ftobetranp);fclose(ftobetranp);te
21、mplen=strlen(temp);for(i=0;i<templen;i+)all=0;for(j=1;j<=n;j+)if(tempi=htj.elem)strcat(code,hcj);all=1;if(all=0)printf("some charactor in the sentence are not matching!");code_num=strlen(code);printf("codes of the sentence are:n%sn",code);file* fcodefilep=null;if(null=(fcod
22、efilep=fopen("e:codefile.txt","w")printf("open codefile.txt failed!n");elsefputs(code,fcodefilep);fclose(fcodefilep);printf("and have put into e:codefile.txt!n");return; /-decoding-void decoding()int m,i,p=0;char q,*decode,*sentence;file* fdecodep=null;if(null
23、=(fdecodep=fopen("e:codefile.txt","r")printf("open e:codefile.txt failed!n");elsefile *txtfile=null;if(null=(txtfile=fopen("e:txtfile.txt","w")printf("open e:txtfile.txt failed!n");elsesentence=(char*)malloc(templen*sizeof(char);decode=(cha
24、r*)malloc(code_num*sizeof(char);fgets(decode,code_num+1,fdecodep);m=2*n-1;for(i=0;decodei-1!='0'i+) q=decodei; if(htm.lchild=0) sentencep=htm.elem; p+; m=2*n-1; i-; else if(q='0') m=htm.lchild; else if(q='1') m=htm.rchild; sentencep='0'fputs(sentence,txtfile);printf(&
25、quot;codes have been encoded and get a sentence:n%sn",sentence);printf("and have been put into txtfile.txt!n");/* 函数功能:输出赫夫曼树中各结点的数据存储情况。 函数参数:无。函数返回值:无。*/void printhufmfigue()int i,m;char end='','0'printf("");printf("numberelementweightparentslchildrchi
26、ldhuffmancode ");printf("");for(i=1;i<=n;i+)printf("%6d%8c%6d%8d%8d%8d%20s",i,hti.elem,hti.weight,hti.parent,hti.lchild,hti.rchild,hci);printf("");m=2*n-1;for(i=n+1;i<=m-1;i+)printf("%6d%8c%6d%8d%8d%8d%20s",i,hti.elem,hti.weight,hti.parent,hti.lchil
27、d,hti.rchild,end);printf("");printf("%6d%8c%6d%8d%8d%8d%20s",m,htm.elem,htm.weight,htm.parent,htm.lchild,htm.rchild,end);printf("");return;/*函数功能:将赫夫曼树以凹入表的形式在终端输出。函数参数: 无。函数返回值:无。*/void treeprint()int maxcode,maxi=1,floor,numb,i;numb=2*n-1;maxcode=strlen(hc1);for(i=1;i<=n;i+)if(strlen(hci)>maxcode)maxcode=strlen(hci);maxi=i;floor=maxcod
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年普通话考试常用句型总结及试题及答案
- 2024年工程材料分析试题及答案
- 基金从业资格考试高效备考小组活动安排试题及答案
- 2024年计算机综合能力试题及答案
- 国际物流服务标准化试题及答案
- 2025陕西省建筑安全员《A证》考试题库及答案
- 注册会计师考试经济学基础与试题及答案
- 明晰知识:人力资源管理师试题及答案
- 中职电子商务市场趋势研究试题及答案
- 农业生产与地理环境的关系试题及答案
- 外墙脚手架施工方案完整版
- 《驾驶室固定矩形窗》
- 境外工程项目安全生产管理规定
- 特殊作业安全管理监护人专项培训课件
- 2022年青海公务员考试申论试题(县乡卷)
- 电梯日管控、周排查、月调度内容表格
- 风电场项目可行性研究报告
- 临床医学专业医学影像学习题集
- 演唱会招商方案
- 冀人版六年级科学下册全册单元提升测试卷含答案
- 马工程《文学理论》
评论
0/150
提交评论