数据结构课程设计哈夫曼编码译码器Word格式文档下载.docx
《数据结构课程设计哈夫曼编码译码器Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计哈夫曼编码译码器Word格式文档下载.docx(28页珍藏版)》请在冰豆网上搜索。
voideditHCode(HTNodeht[],HCodehcd[],CHar&
ch,intn,charbianma[]);
//编码函数
voidprintyima(HTNodeht[],HCodehcd[],intn,charbianma[]);
//译码函数
voidcreatHT(HTNodeht[],intn);
voidCreateHCode(HTNodeht[],HCodehcd[],intn);
voidDispHCode(HTNodeht[],HCodehcd[],intn);
voidinput_key(CHar&
ch);
voidinput_file(CHar&
voidinput_cw(HTNodeht[]);
voidbianma1(HTNodeht[],HCodehcd[],CHar&
voidbianma2(HTNodeht[],HCodehcd[],CHar&
voidyima1(HTNodeht[],HCodehcd[],intn,charbianma[]);
voidyima2(HTNodeht[],HCodehcd[],intn,charbianma[]);
voidcreat_cw();
voidbianmacaidan();
voidyimacaidan();
voidbianmayima();
intcaidan();
//结构体
typedefstruct
{
chardata;
intparent;
intweight;
intlchild;
intrchild;
}HTNode;
charcd[N];
intstart;
}HCode;
chars[N];
intnum;
}CHar;
CHarch;
HTNodeht[M];
HCodehcd[N];
//主函数
intmain()
intxh;
while
(1)
{
system("
color1f"
);
//操作菜单背景颜色
xh=caidan();
//调用菜单函数
switch(xh)//switch语句
{
case1:
system("
cls"
creat_cw();
break;
case2:
creatHT(ht,n);
case3:
CreateHCode(ht,hcd,n);
DispHCode(ht,hcd,n);
case4:
bianmayima();
case0:
printf("
\n\n\n\n\n\n\n\n\n\t\t\t\t感谢使用本系统!
\n\n\n\n\n\n\n\t\t\t"
exit(0);
default:
putchar('
\a'
printf("
\n\t\t输入有误,请重新输入:
\n"
}
}
return0;
}
//菜单函数
intcaidan()//菜单函数模块//
printf("
\n\n\n"
\t\t欢迎使用哈夫曼编码译码系统\n"
\t\t\n"
\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n"
\t\t*==*\n"
\t\t*=*=*=*=*=*=*=*=*=*=*=*==*\n"
\t\t*=1.建立字符权值=*\n"
\t\t*=2.建立并输出哈夫曼树=*\n"
\t\t*=3.生成并查看哈夫曼编码=*\n"
\t\t*=4.编码与译码=*\n"
\t\t*=0.退出系统=*\n"
\n\t\t请输入序号进行选择:
"
scanf("
%d"
&
xh);
returnxh;
//返回从键盘接收的选项
voidbianmayima()
{
\n\n\n\n\n"
\t\t编码与译码\n"
\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n"
\t\t*=1.编码=*\n"
\t\t*=2.译码=*\n"
\t\t*=0.返回上级菜单=*\n"
&
bianmacaidan();
yimacaidan();
return;
voidyimacaidan()
\t\t译码\n"
\t\t*=1.键盘输入编码进行译码=*\n"
\t\t*=2.文件读入编码进行译码=*\n"
yima1(ht,hcd,n,bianma);
yima2(ht,hcd,n,bianma);
voidbianmacaidan()
\t\t编码\n"
\t\t*=1.键盘输入字符集编码=*\n"
\t\t*=2.文件读入文章编码=*\n"
bianma1(ht,hcd,ch,n,bianma);
bianma2(ht,hcd,ch,n,bianma);
voidcreat_cw()
intxh2;
\t\t建立字符权值\n"
\t\t*=1.从键盘输入字符集进行统计=*\n"
\t\t*=2.从文件读入字符集统计=*\n"
\t\t*=3.自定义字符权值=*\n"
xh2);
switch(xh2)//switch语句
input_key(ch);
input_file(ch);
input_cw(ht);
//建立字符权值模块
ch)
inti,j=0;
charst[N];
请输入字符集(以‘#’结束):
for(i=0;
i<
N;
i++)
scanf("
%c"
st[i]);
if(st[i]=='
#'
)
{st[i]='
\0'
;
strcpy(ch.s,st);
ch.num=strlen(st);
count(ch,ht);
按任意键返回!
getch();
system("
return;
inti;
FILE*fp;
charfilename[20];
请输入要打开的文件名(*.txt):
%s"
filename);
if((fp=fopen(filename,"
r"
))==NULL)
printf("
\n\t\t文件打开失败!
!
return;
feof(fp);
fread(&
ch.s[i],sizeof(char),1,fp);
ch.num=strlen(ch.s);
读入成功!
文件中的字符集为:
%s\n"
ch.s);
fclose(fp);
voidinput_cw(HTNodeht[])
inti,w,s,j;
chara;
要输入的字符总个数是?
:
s);
n=s;
请输入字符及其权值:
s;
请输入第%d个字母:
i+1);
a);
ht[i].data=a;
请输入其权值:
w);
ht[i].weight=w;
FILE*fp;
if((fp=fopen("
data.txt"
"
w"
))==0)
\n定义权值成功!
\n\n"
各字符及其权值为:
fprintf(fp,"
字符\t权值"
for(j=0;
j<
i;
j++)
{printf("
%-8c%-8d"
ht[j].data,ht[j].weight);
fprintf(fp,"
%-8c%-8d%"
\n字符权值已输出至文件“data.txt”!
输入完成,按任意键返回!
//统计字符权值函数
ch,HTNodeht[])
inti,j,m=0;
charc[N];
intsum[N]={0};
ch.s[i]!
='
for(j=0;
m;
if(ch.s[i]==c[j]||(c[j]>
a'
&
c[j]<
z'
ch.s[i]+32==c[j]))break;
if(j<
m)
sum[j]++;
else
{
if(ch.s[i]>
A'
ch.s[i]<
Z'
)
c[j]=ch.s[i]+32;
elsec[j]=ch.s[i];
m++;
}
ht[i].data=c[i];
ht[i].weight=sum[i];
n=m;
FILE*fp;
\n统计权值成功!
//构造哈夫曼树
voidcreatHT(HTNodeht[],intn)
哈夫曼树.txt"
inti,j,k,lnode,rnode;
intmin1,min2;
for(i=0;
2*n-1;
i++)
ht[i].parent=ht[i].lchild=ht[i].rchild=-1;
for(i=n;
min1=min2=32767;
lnode=rnode=-1;
for(k=0;
k<
=i-1;
k++)
if(ht[k].parent==-1)
if(ht[k].weight<
min1)
min2=min1;
rnode=lnode;
min1=ht[k].weight;
lnode=k;
}
elseif(ht[k].weight<
min2)
min2=ht[k].weight;
rnode=k;
ht[lnode].parent=i;
ht[rnode].parent=i;
ht[i].weight=ht[lnode].weight+ht[rnode].weight;
ht[i].lchild=lnode;
ht[i].rchild=rnode;
建立huffman树成功!
输出huffman树:
\t字符\t权值\t父节点\t左子节点\t右子节点"
for(j=1;
\t%-8c%-8d%-10d%-14d%-10d"
ht[j].data,ht[j].weight,ht[j].parent,ht[i].lchild,ht[j].rchild);
哈夫曼树已输出至文件“哈夫曼树.txt”!
//生成哈夫曼编码
voidCreateHCode(HTNodeht[],HCodehcd[],intn)
inti,f,c,j=0;
HCodehc;
n;
hc.start=n;
c=i;
hc.cd[hc.start--]='
0'
f=ht[i].parent;
while(f!
=-1)
if(ht[f].lchild==c)
else
1'
c=f;
f=ht[f].parent;
hc.start++;
hc.start;
j