- 1、本文档共11页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
1.问题描述
哈弗曼树的编码与译码
— 功能:实现对任何类型文件的压缩与解码
— 输入:源文件,压缩文件
— 输出:解码正确性判定,统计压缩率、编码与解码速度
— 要求: 使用边编码边统计符号概率的方法(自适应 Huffman 编码) 和
事先统计概率的方法(静态 Huffman 编码)
2.1 程序清单
程序书签:
1.
2.
3.
4.
5.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
struct node{
long weight; //权值
unsigned char ch;//字符
int parent,lchild,rchild;
char code[256];//编码的位数最多为 256 位
int CodeLength;//编码长度
}hfmnode[512];
void compress();
void uncompress();
//主函数
void main()
{
int choice;
printf("请选择 1~3:\n");
printf("1.压缩文件\n");
printf("2.解压文件\n");
printf("3.退出!\n");
scanf("%d",&choice);
if(choice==1)compress();
else if(choice==2)uncompress();
else if(choice==3)return;
else printf("输入错误!");
}
//压缩函数
void compress()
{
int i,j;
char infile[20],outfile[20];
FILE *ifp,*ofp;
unsigned char c;//
long FileLength,filelength=0;
int n,m;//叶子数和结点数
int s1,s2; //权值最小的两个结点的标号
char codes[256];
long sumlength=0;
float rate,speed;
int count=0;
clock_t start1, start2,finish1,finish2;
double duration1,duration2;
void encode(struct node *nodep,int n);//编码函数
int select(struct node *nodep,int pose);//用于建哈弗曼树中选择权值最小的结
点的函数
printf("请输入要压缩的文件名:");
scanf("%s",infile);
ifp=fopen(infile,"rb");
if(ifp==NULL)
{
printf("文件名输入错误,文件不存在!\n");
return;
}
printf("请输入目标文件名:");
scanf("%s",outfile);
ofp=fopen(outfile,"wb");
if(ofp==NULL)
{
printf("文件名输入错误,文件不存在!\n");
return;
}
start1=clock() ;//开始计时 1
//统计文件中字符的种类以及各类字符的个数
//先用字符的 ASCII 码值代替结点下标
FileLength=0;
while(!feof(ifp))
文档评论(0)