数据结构课程设计哈夫曼编码译码器.docx
《数据结构课程设计哈夫曼编码译码器.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计哈夫曼编码译码器.docx(28页珍藏版)》请在冰豆网上搜索。
数据结构课程设计哈夫曼编码译码器
题目一:
哈夫曼编码与译码
一、任务
设计一个利用哈夫曼算法的编码和译码系统,重复地显示并处理以下项目,直到选择退出为止。
要求:
1) 将权值数据存放在数据文件(文件名为data.txt,位于执行程序的当前目录中);
2) 初始化:
键盘输入字符集统计字符权值、自定义26个字符和26个权值、统计文件中一篇英文文章中26个字母,建立哈夫曼树;
3) 编码:
利用建好的哈夫曼树生成哈夫曼编码;
4) 输出编码(首先实现屏幕输出,然后实现文件输出);
5)译码(键盘接收编码进行译码、文件读入编码进行译码);
6) 界面优化设计。
二、流程图
三、代码分解
//头文件
#include
#include
#include
#include
#defineN1000
#defineM2*N-1
#defineMAXcode6000
//函数声明
voidcount(CHar&ch,HTNodeht[]);
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&ch);
voidinput_cw(HTNodeht[]);
voidbianma1(HTNodeht[],HCodehcd[],CHar&ch,intn,charbianma[]);
voidbianma2(HTNodeht[],HCodehcd[],CHar&ch,intn,charbianma[]);
voidyima1(HTNodeht[],HCodehcd[],intn,charbianma[]);
voidyima2(HTNodeht[],HCodehcd[],intn,charbianma[]);
voidcreat_cw();
voidbianmacaidan();
voidyimacaidan();
voidbianmayima();
intcaidan();
//结构体
typedefstruct
{
chardata;
intparent;
intweight;
intlchild;
intrchild;
}HTNode;
typedefstruct
{
charcd[N];
intstart;
}HCode;
typedefstruct
{
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:
system("cls");creatHT(ht,n);break;
case3:
system("cls");CreateHCode(ht,hcd,n);DispHCode(ht,hcd,n);break;
case4:
system("cls");bianmayima();break;
case0:
system("cls");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:
system("cls");putchar('\a');
printf("\n\t\t输入有误,请重新输入:
\n");break;
}
}
return0;
}
//菜单函数
intcaidan()//菜单函数模块//
{
intxh;
printf("\n\n\n");
printf("\t\t欢迎使用哈夫曼编码译码系统\n");
printf("\t\t\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\t\t*==*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*==*\n");
printf("\t\t*=1.建立字符权值=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*==*\n");
printf("\t\t*=2.建立并输出哈夫曼树=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*==*\n");
printf("\t\t*=3.生成并查看哈夫曼编码=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*==*\n");
printf("\t\t*=4.编码与译码=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*==*\n");
printf("\t\t*=0.退出系统=*\n");
printf("\t\t*==*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\n\t\t请输入序号进行选择:
");
scanf("%d",&xh);
returnxh;//返回从键盘接收的选项
}
voidbianmayima()
{
intxh;
while
(1)
{
printf("\n\n\n\n\n");
printf("\t\t编码与译码\n");
printf("\t\t\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\t\t*=1.编码=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\t\t*=2.译码=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\t\t*=0.返回上级菜单=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\n\t\t请输入序号进行选择:
");
scanf("%d",&xh);
switch(xh)//switch语句
{
case1:
system("cls");bianmacaidan();break;
case2:
system("cls");yimacaidan();break;
case0:
system("cls");return;
default:
system("cls");putchar('\a');
printf("\n\t\t输入有误,请重新输入:
\n");break;
}
}
}
voidyimacaidan()
{
intxh;
while
(1)
{
printf("\n\n\n\n\n");
printf("\t\t译码\n");
printf("\t\t\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\t\t*=1.键盘输入编码进行译码=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\t\t*=2.文件读入编码进行译码=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\t\t*=0.返回上级菜单=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\n\t\t请输入序号进行选择:
");
scanf("%d",&xh);
switch(xh)//switch语句
{
case1:
system("cls");yima1(ht,hcd,n,bianma);break;
case2:
system("cls");yima2(ht,hcd,n,bianma);break;
case0:
system("cls");return;
default:
system("cls");putchar('\a');
printf("\n\t\t输入有误,请重新输入:
\n");break;
}
}
}
voidbianmacaidan()
{
intxh;
while
(1)
{
printf("\n\n\n\n\n");
printf("\t\t编码\n");
printf("\t\t\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\t\t*=1.键盘输入字符集编码=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\t\t*=2.文件读入文章编码=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\t\t*=0.返回上级菜单=*\n");
printf("\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("\n\t\t请输入序号进行选择:
");
scanf("%d",&xh);
switch(xh)//switch语句
{
case1:
system("cls");bianma1(ht,hcd,ch,n,bianma);break;
case2:
system("cls");bianma2(ht,hcd,ch,n,bianma);break;
case0:
system("cls");return;
default:
system("cls");putchar('\a')