数据结构课程设计哈夫曼编编译器Word下载.docx
《数据结构课程设计哈夫曼编编译器Word下载.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计哈夫曼编编译器Word下载.docx(46页珍藏版)》请在冰豆网上搜索。
G
H
I
J
K
L
M
频度
186
64
13
22
32
103
21
15
47
57
1
5
20
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
63
48
51
80
23
8
18
16
4.实现提示
(1)编码结果以文本方式存储在文件Codefile中。
(2)用户界面可以设计为“菜单”方式:
显示上述功能符号,再加上“Q”,表示退出运行Quit。
请用户键入一个选择功能符。
此功能执行完毕后再显示此菜单,直至某次用户选择了“Q”为止。
(3)在程序的一次执行过程中,第一次执行I,D或C命令之后,赫夫曼树已经在内存了,不必再读入。
每次执行中不一定执行I命令,因为文件hfmTree可能早已建好。
概要设计
1.问题分析哈夫曼树的定义
哈夫曼树节点的数据类型定义为:
typedefstruct{//哈夫曼树的结构体
charch;
intweight;
//权值
intparent,lchild,rchild;
}htnode,*hfmtree;
2.所实现的功能函数如下
1)voidhfmcoding(hfmtree&
HT,hfmcode&
HC,intn)
初始化哈夫曼树,处理InputHuffman(HuffmanHfm)函数得到的数据,按照哈夫曼规则建立2叉树。
此函数块调用了Select()函数。
2)voidSelect(hfmtree&
HT,inta,int*p1,int*p2)
Select函数,选出HT树到a为止,权值最小且parent为0的2个节点
3)intmain()
主函数:
利用已建好的哈夫曼树(如不在内存,则从文件hfmtree.txt中读入)
对文件中的正文进行编码,然后将结果存入文件codefile.txt中。
如果正文中没有要编码的字符,则键盘读入并存储到ToBeTran文件中。
读入ToBeTran中将要编码的内容,将编码好的哈夫曼编码存储到CodeFile中。
3、Encoding
编码功能:
对输入字符进行编码
4、Decoding
译码功能:
利用已建好的哈夫曼树将文件codefile.txt中的代码进行译码,结果存入文件textfile.dat中。
5.主函数的简要说明,主函数主要设计的是一个分支语句,让用户挑选所实现的功能。
使用链树存储,然后分别调用统计频数函数,排序函数,建立哈夫曼函数,编码函数,译码函数来实现功能。
程序流程图
1.hfmcoding(hfmtree&
2.Select(hfmtree&
HT,inta,int*p1,int*p2)
3.intmain()
结构图总括
算法分析
======================构建哈夫曼树的结构体====================
charch;
intweight;
intparent,lchild,rchild;
typedefchar**hfmcode;
=======================Select函数==========================
voidSelect(hfmtree&
HT,inta,int*p1,int*p2)//Select函数,选出HT树到a为止,权值最小且parent为0的2个节点
{
inti,j,x,y;
for(j=1;
j<
=a;
++j){
if(HT[j].parent==0){
x=j;
break;
}
}
for(i=j+1;
i<
++i){
if(HT[i].weight<
HT[x].weight&
&
HT[i].parent==0){
x=i;
//选出最小的节点
++j){
if(HT[j].parent==0&
x!
=j)
{
y=j;
++i)
{
HT[y].weight&
HT[i].parent==0&
=i)
y=i;
//选出次小的节点
if(x>
y){
*p1=y;
*p2=x;
else
*p1=x;
*p2=y;
}
=======================hfmcoding函数==========================
voidhfmcoding(hfmtree&
HC,intn)//构建赫夫曼树HT,并求出n个字符的赫夫曼编码HC
inti,start,c,f,m,w;
intp1,p2;
char*cd,z;
if(n<
=1){
return;
m=2*n-1;
HT=(hfmtree)malloc((m+1)*sizeof(htnode));
for(i=1;
=n;
++i)//初始化n个叶子结点
printf("
请输入第%d字符信息和权值:
"
i);
scanf("
%c%d"
&
z,&
w);
while(getchar()!
='
\n'
)
continue;
HT[i].ch=z;
HT[i].weight=w;
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
for(;
=m;
++i)//初始化其余的结点
HT[i].ch='
0'
;
HT[i].weight=0;
for(i=n+1;
++i)//建立赫夫曼树
Select(HT,i-1,&
p1,&
p2);
HT[p1].parent=i;
HT[p2].parent=i;
HT[i].lchild=p1;
HT[i].rchild=p2;
HT[i].weight=HT[p1].weight+HT[p2].weight;
HC=(hfmcode)malloc((n+1)*sizeof(char*));
cd=(char*)malloc(n*sizeof(char));
cd[n-1]='
\0'
++i)//给n个字符编码
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]='
}
else
1'
HC[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&
cd[start]);
free(cd);
调试分析
哈夫曼树的初始化
创建出的哈夫曼树的结果
输入待编码的字符及其结果
就现存的编码进行译码的结果
重新编写代码(例如输入HELLO的代码)
打印哈夫曼代码
存储哈夫曼树的hmfTree文件
存储哈夫曼编码的CodeFile文件
存储输入字符ToBeTran文件与输出字符Textfile文件
源程序代码
#include<
stdio.h>
stdlib.h>
string.h>
iostream.h>
fstream.h>
typedefstruct{
voidsort(int*a,intn)
inti,j,k,temp;
i++)
k=0;
for(j=1;
=n-i;
j++)
if(a[j]<
a[j+1])
k=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
if(!
k)break;
voidSelect(hfmtree&
if(HT[j].parent==0)
HT[i].parent==0)
y)
voidhfmcoding(hfmtree&
inti,j,start,c,f,m,w,a[100],count=0;
for(i=0;
100;
i++)//初始化a数组
a[i]=0;
=1)
Error!
输入字符不能少于1个。
。
);
exit(0);
for(i=j=1;
i++,j++)
输入第%d字符信息和权值:
"
if(z=='
'
HT[i].ch='
@'
else
HT[i].ch=z;
HT[i].weight=a[j]=w;
printf("
\n--------------------------------哈夫曼树-------------------------------------\n"
第1行:
sort(a,n);
//权值从大到小排序
a[j]!
j++)printf("
%-6d"
a[j]);
\n"
HT[p2].parent=i;
HT[i].rchild=p2;
a[n-count]=0;
count++;
a[n-count]=HT[i].weight;
sort(a,n);
第%d行:
count+1);
\n-------------------------------哈夫曼代码------------------------------------\n"
HC=(hfmcode)malloc((n+1)*sizeof(char*));
cd=(char*)malloc(n*sizeof(char));
intmain()
charcode[100],h[100],hl[100],hk[100],space[100];
intn,i,j,k,l,sta=0;
ifstreaminput_file;
ofstreamoutput_file;
charchoice,str[100],buff,nima;
hfmtreeHT;
hfmcodeHC;
cout<
<
while(choice!
Q'
choice!
q'
cout<
endl;
*********************哈夫曼编码器/译码器***********************\n"
**\n"
*I.初始化*\n"
*E.编码*\n"
*D.译码*\n"
*P.打印*\n"
*Q.退出*\n"
***************************************************************"
输入要操作的步骤:
cin>
>
choice;
if(choice=='
I'
||choice=='
i'
sta=1;
//已经进行建立哈夫曼树的操作,sta置1
cout<
输入字符个数:
cin>
n;
hfmcoding(HT,HC,n);
output_file.open("
hfmTree.txt"
if(!
output_file)
cout<
Can'
topenfile!
return1;
for(i=1;
if(HT[i].ch=='
output_file<
:
HC[i]<
'
else
HT[i].ch<
:
output_file.close();
{
cout<
output_file.open("
space.txt"
if(!
{
cout<
return1;
}
HC[i];
output_file.close();
input_file.open("
input_file)
input_file>
space;
//用space存储空格的编码
input_file.close();
}
哈夫曼树已经创建,并存入hfmTree.txt文件!
elseif(choice=='
E'
e'
charnima;
if(sta==0)
哈夫曼树没有建立,请检查操作!
exit(0);
printf("
输入字符:
gets(str);
ToBeTran.txt"
output_file<
str<
CodeFile.txt"
for(i=0;
strlen(str);
if(str[i]=='
str[i]='
{
for(j=0;
if(HT[j].ch==str[i])
output_file<
HC[j];
break;
完成编码,请查看CodeFile文件\n"
input_file.open("
input_file>
code;
编码结果:
code<
input_file.close();
nima='
while(nima!
N'
||nima!
n'
printf("
\n是否清屏?
(Y/N)\n"
cin>
nima;
if(nima=='
Y'
||nima=='
y'
system("
cls"
break;
printf("
D'
d'
cou