哈夫曼解压缩程序

本文介绍了一个使用C++编写的哈夫曼解压缩程序,通过提供的工程资源链接,读者可以下载并研究该程序的实现细节,了解哈夫曼编码在数据解压中的应用。
摘要由CSDN通过智能技术生成
#include "StdAfx.h"
#include "Hafuman.h"


Hafuman::Hafuman(void)
{
	//哈夫曼树初始化
	HTree=NULL;
	used=0;
	HuffNode htree[512];
	total=0;
	
	//每个叶子节点赋值
    for(int i=0;i<512;i++)
	{
		htree[i].count=0;
		htree[i].c=(unsigned char)i;
		htree[i].lchild=-1;
		htree[i].parent=-1;
		htree[i].rchild=-1;
	}
	HTree=htree;
}


Hafuman::~Hafuman(void)
{
}

HuffNode *Hafuman::GetHTree()
{
	return HTree;
}

int Hafuman::Getused()
{
	return used;
}

int Hafuman::Gettotal()
{
	return total;
}

void Hafuman::Set(char *infile)
{
	cout<<"***将被压缩的文件名提交给哈夫曼树"<<endl;
	infilename=infile;
	cout<<"***提交成功!"<<endl;
}

void Hafuman::Prepare()
{
	cout<<"***构建哈夫曼树之前的准备工作"<<endl;
	int i=0,j=0,k=0;
	HuffNode temp;
	FILE *infile;
	fopen_s(&infile,infilename,"rb");
	while(infile==NULL)
	{
		cout<<"要压缩的文件名有误!"<<endl;
		return;
	}
	//统计各个字符出现次数
	unsigned char c;
	while(!feof(infile))
	{
		fread(&c,1,1,infile);
		HTree[c].count++;
		total++;
	}
	total--;
	cout<<"***压缩前文件大小(即压缩前的文件的总字符个数) :"<<total<<endl;
	HTree[c].count--;

	//count为0的不要,按count从大到小排列
	for(i=0;i<255;i++)
	{
		for(j=i+1;j<256;j++)
		{
			if(HTree[i].count<HTree[j].count)
			{
				temp=HTree[i];
				HTree[i]=HTree[j];
				HTree[j]=temp;
			}
		}
	}

	//有用字符数
	for(i=0;i<512;i++)
	{
		if(HTree[i].count==0)
			break;
	}
	used=i;		//使用的字符数目,即可用的叶节点数目
	cout<<"***出现的字符种类:  "<<used<<endl;
	for(i=0;i<used;i++)
	{
		cout<<(unsigned int)HTree[i].c<<"号字符"<<"的权值(即出现次数)为:"<<HTree[i].count<<endl;
	}
	fclose(infile);
	cout<<"***准备成功!"<<endl;

}

//构建哈夫曼树
void Hafuman::Great()
{
	cout<<"***创建哈夫曼树"<<endl;
	int i=0;
	int j=0;
	unsigned long min;
	int m=2*used-1;
	int pt=0;
	//每次从没有父节点的结点中找出权值最小的结点
	//连接到父节点上,父节点的权值位两节点的权值和
	//父节点的左子节点为权值最小的结点&#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值