1、数据结构课程设计 哈弗曼编译数据结构课 程 设 计 报 告 学院(系): 班 级: 学生学号: 姓名: 指导教师: 2012年 6 月 17 日 一、需求分析3二、概要设计3 设计分析3 流程图3三、详细设计3四、调试分析12五、用户手册13六、测试结果13 运行过程13 运行结果17七、设计心得18八、附录18 参考文献18 代码19一、需求分析哈夫曼树,又称最优树,是一类带权路径长度最短的树,有着广泛的应用。利用哈夫曼编码进行通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本。但是,这要求在发送端将传来的数据进行译码(复原),对于双工信道(即可以双向传输信息的信道),每端都需要一个
2、完整的编/译码系统。本程序将为这样的信息收发站写一个哈夫曼的编译码系统。二、概要设计本系统将由如下模块组成:1、 定义。2、 I:初始化(Initialization)函数 从终端输入字符集大小n,以及n个字符和n个权值,建立哈夫曼树,并将它存于文件hfmTree中3、 E:编码(Encoding)函数 利用已建好的哈夫曼书(如不在内存,则从文件hfmTree中读入),对文件ToBeTran中的正文进行编码,然后将结果存入文件CodeFile中。4、 D:译码(Decoding)函数 利用已经建好的哈夫曼树将文件CodeFile中的代码进行译码,结果存入文件TextFile中。5、 P:打印代
3、码文件(Print)函数。 将文件CodeFile以紧凑格式显示在终端,每行50个代码。同时将此字符形式的编码文件写入文件CodePrin中。6、T:打印哈夫曼树(TreePrinting). 将已在内存中的哈夫曼树以直观的方式(树或凹入表形式)显示在终端上,同时将此字符形式的哈夫曼树写入文件TreePrinting .7、函数调用图如下: MainInitialization Encoding Decoding Print TreePrintSelect InputCode Codeprining CoprintMin (main调用第二行所有函数,其余列的首字母对应的上一行函数调用下一行函
4、数。)三、详细设计哈夫曼编码是一种编码方式,它是根据每一个字符出现的概率而建立起来的。哈夫曼编码借助树形结构构造,算法实现时使用链表或静态链表结构,空间的每个结点内有左子树、右子树、双亲指针。在构成哈夫曼树之后,为求编码需从叶子结点出发走一条从叶子到根的路径;而为译码需从根出发走一条从根到叶子的路径。要实现如上概要所说功能,需要进行如下步骤。1、哈夫曼树及哈夫曼编码的存储表示typedef struct int weight; int parent,lchild,rchild; HTNode,* HuffmanTree; /动态分配数组存储哈夫曼树 typedef char *HuffmanC
5、ode;/ 动态分配数组存储哈夫曼编码表2、初始化 HuffmanTree HT; HuffmanCode HC;int *w,i,j; const int n=26; char *z; int flag=0; int numb=0;3、定义min功能函数int min(HuffmanTree t,int i) int j,flag; int k=UINT_MAX; for(j=1;j=i;j+) if(tj.weights2) j=s1; s1=s2; s2=j; 5、编写求哈夫曼编码的算法。void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC
6、,int *w,int n) int m,i,s1,s2,start; int c,f; HuffmanTree p; char *cd; if(n=1) return; m=2*n-1; HT=(HuffmanTree)malloc(m+1)*sizeof(HTNode); for(p=HT+1,i=1;iweight=*w; p-parent=0; p-lchild=0; p-rchild=0; for(;iparent=0; for(i=n+1;i=m;+i) select(HT,i-1,s1,s2); HTs1.parent=HTs2.parent=i; HTi.lchild=s1;
7、HTi.rchild=s2; HTi.weight=HTs1.weight+HTs2.weight; HC=(HuffmanCode)malloc(n+1)*sizeof(char*); 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(HTf.lchild=c) cd-start=0; else cd-start=1; HCi=(char*)malloc(n-start)*sizeof(char); strcpy
8、(HCi,&cdstart); free(cd); 6、在“我的课程设计”文件夹中新建“abc.txt”文件,文件内容如下:字符ABCDEFGHIJKLM频度1866413223210321154757153220字符NOPQRSTUVWXYZ频度57631514851802381811617、编写初始化(Initialization)函数void Initialization() flag=1; int num2; cout下面初始化哈夫曼链表endl; w=(int*)malloc(n*sizeof(int); z=(char*)malloc(n*sizeof(char); coutn依次
9、显示n个字符与其权值和编码nendl; char base2;ifstream fin(abc.txt); for(i=0;ibase; *(z+i)=*base; finnum2; *(w+i)=num2; HuffmanCoding(HT,HC,w,n); cout字符setw(6)权值setw(11)编码endl; for(i=1;i=n;i+) coutsetw(3)*(z+i-1); coutsetw(6)*(w+i-1)setw(12)HCiendl; cout下面将哈夫曼编码写入文件endl.endl; FILE *htmTree; char r= ,0; if(htmTree=
10、fopen(htmTree.txt,w)=NULL) cout不能打开文件 endl; return; for(i=0;in;i+) fputc(*(z+i),htmTree); fputs(r,htmTree); for(i=0;in;i+) fprintf(htmTree,%6d,*(w+i); fputs(r,htmTree); for(i=1;i=n;i+) fputs(HCi,htmTree); fputs(r,htmTree); fclose(htmTree); cout已将字符与对应编码写入根目录下文件htmTree.txt中endlendl; 8、编写编码(Encoding)函
11、数void InputCode() FILE *tobetran; char str100; if(tobetran=fopen(tobetran.txt,w)=NULL) cout不能打开文件endl; return; cout请输入你想要编码的字符endl; gets(str); fputs(str,tobetran); cout获取报文成功endl; fclose(tobetran); cout.endl报文存入根目录下的tobetran.txt文件中endl; void Encoding() cout下面对目录下文件tobetran.txt中的字符进行编码endl; FILE *tob
12、etran,*codefile; if(tobetran=fopen(tobetran.txt,rb)=NULL) cout不能打开文件endl; if(codefile=fopen(codefile.txt,wb)=NULL) cout不能打开文件endl; char *tran; i=99; tran=(char*)malloc(100*sizeof(char); while(i=99) if(fgets(tran,100,tobetran)=NULL) cout不能打开文件endl; break; for(i=0;*(tran+i)!=0;i+) for(j=0;jn) cout字符错误
13、,无法编码!endl; break; cout编码完成endl; cout编码写入目录下的codefile.txt中endlendl; fclose(tobetran); fclose(codefile); free(tran); 9、编写译码(Decoding)函数void Decoding() cout下面对根目录下文件codefile.txt中的字符进行译码endl; FILE *codef,*txtfile; if(txtfile=fopen(Textfile.txt,w)=NULL) cout不能打开文件endl; txtfile=fopen(Textfile.txt,w); if
14、(codef=fopen(codefile.txt,r)=NULL) cout不能打开文件endl; codef=fopen(codefile.txt,r); char *work,*work2,i2; int i4=0,i,i3; unsigned long length=10000; work=(char*)malloc(length*sizeof(char); fgets(work,length,codef); work2=(char*)malloc(length*sizeof(char); i3=2*n-1; for(i=0;*(work+i-1)!=0;i+) i2=*(work+i
15、); if(HTi3.lchild=0) *(work2+i4)=*(z+i3-1); i4+; i3=2*n-1; i-; else if(i2=0) i3=HTi3.lchild; else if(i2=1) i3=HTi3.rchild; *(work2+i4)=0; fputs(work2,txtfile); cout译码完成endl; cout内容写入根目录下的文件textfile.txt中endlendl; free(work); free(work2); fclose(txtfile); fclose(codef); 10.编写打印代码文件函数(Print)void Code_p
16、rinting() cout下面打印根目录下文件CodePrin.txt中编码字符endl; FILE * CodePrin,* codefile; if(CodePrin=fopen(CodePrin.txt,w)=NULL) cout不能打开文件endl; return; if(codefile=fopen(codefile.txt,r)=NULL) cout不能打开文件endl; return; char *work3; work3=(char*)malloc(51*sizeof(char); if(fgets(work3,51,codefile)=NULL) cout不能读取文件end
17、l; else do fputs(work3,CodePrin); puts(work3); while(strlen(work3)=50&fgets(work3,51,codefile)!=NULL); free(work3); cout打印结束endlendl; fclose(CodePrin); fclose(codefile); 11、编写打印哈夫曼树(TreePrinting)函数void coprint(HuffmanTree start,HuffmanTree HT) if(start!=HT) FILE * TreePrint; if(TreePrint=fopen(TreeP
18、rint.txt,a)=NULL) cout创建文件失败rchild,HT); coutsetw(5*numb)weightweight); coprint(HT+start-lchild,HT); numb-; fclose(TreePrint); void Tree_printing(HuffmanTree HT,int w) HuffmanTree p; p=HT+w; cout下面打印哈夫曼树endl; coprint(p,HT); cout打印工作结束endl; 12、编写主函数void main() coutendl; cout此程序实现哈夫曼编码解码功能endl; char ch
19、oice; while(choice!=q) coutn*endl; cout哈夫曼编码解码 endl; cout* endl; cout(i)初始化哈夫曼表 endl; cout(w)输入待编码的字符 endl; cout(e)进行编码、译码、打印编码endl;cout(t)打印哈夫曼树 endl; cout(q) 离开endl;if(flag=0) coutn请先初始化哈夫曼链表,输入iendl; cout(程序将从根目录下的abc.txt文件中读出26个字母及其权值并对字母进 行编码)choice; switch(choice) case i: Initialization(); bre
20、ak; case w: InputCode(); break; case e: Inputcoding(); Decoding(); Code_printing(); break; case t: Tree_printing(HT,2*n-1); break; case q: break; default: cout输入命令错误 weight must point to class/struct/unionE:我的课程设计课程设计aa.cpp(43) : error C2227: left of -parent must point to class/struct/unionE:我的课程设计课
21、程设计aa.cpp(44) : error C2227: left of -lchild must point to class/struct/unionE:我的课程设计课程设计aa.cpp(44) : error C2227: left of -rchild must point to class/struct/unionE:我的课程设计课程设计aa.cpp(46) : error C2227: left of -parent must point to class/struct/unionE:我的课程设计课程设计aa.cpp(48) : error C2065: s1 : undeclar
22、ed identifierE:我的课程设计课程设计aa.cpp(48) : error C2065: s2 : undeclared identifierE:我的课程设计课程设计aa.cpp(55) : error C2065: cd : undeclared identifierE:我的课程设计课程设计aa.cpp(55) : error C2440: = : cannot convert from char * to int This conversion requires a reinterpret_cast, a C-style cast or function-style castE
23、:我的课程设计课程设计aa.cpp(56) : error C2109: subscript requires array or pointer typeE:我的课程设计课程设计aa.cpp(56) : error C2106: = : left operand must be l-valueE:我的课程设计课程设计aa.cpp(59) : error C2065: start : undeclared identifierE:我的课程设计课程设计aa.cpp(60) : error C2065: c : undeclared identifierE:我的课程设计课程设计aa.cpp(60) : error C2065: f : undeclared identifierE:我的课程设计课程设计aa.cpp(63) : error C2109: subscript requires array or pointer typeE:我的课程设计课程设计aa.cpp(63) : error C2106: = : left operand must be l-valueE:我的课程设计课程设计aa.cpp(6
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1