⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hafu.cpp

📁 这是一个用于的压缩与解压的源代码
💻 CPP
字号:
#include"head.h"
#include"head2.h"
#include"head3.h"
#include<iostream>
#include<string>
#include<bitset>
#include<fstream>
using namespace std;
//*********挑选两个权值小的节点****************//
int select(element hufftree[ ],int sign,int m)
{
    int i=0;
	int temp, temp1,temp2=0;;
	for(i=0;i<2*m-1;i++)
	{
		if(hufftree[sign].parent==-1)
		{
			temp2++;
			temp1=sign;
			temp=hufftree[sign].weight;
		}
		if(temp2==1)
			break;	
		sign++;
	}
	for(i=0;i<2*m-1;i++)
	{
		if( hufftree[i].weight < temp && hufftree[i].parent==-1 && hufftree[i].weight>0 )
		{ 
			temp1=i;
			temp=hufftree[i].weight;
		}
	}
	return temp1;
}
//////////////////////////////////////////////////////
//*****建立一棵哈夫曼树*******************//
/////////////////////////////////////////////////////
void huffman( element hufftree[ ],abcd letter[ ],int m )
{
	int i1,i2,k,i,sign;
	ofstream outfile("t2.txt",ios::out);
	for(i=0;i<2*m-1;i++)
	{
		hufftree[i].parent=-1;
		hufftree[i].lchild=-1;
		hufftree[i].rchild=-1;
	}
	for(i=0;i<m;i++)
	{
		hufftree[i].zimu=letter[i].character;
		hufftree[i].weight=letter[i].number;
	}
	sign=0;
	for(k=m;k<2*m-1;k++)
	{
		i1=select(hufftree, sign,m);
		hufftree[i1].parent=k;
		hufftree[k].lchild=i1;
		sign++;
		i2=select(hufftree,sign,m);
		hufftree[i2].parent=k;
		hufftree[k].rchild=i2;
		hufftree[k].weight=hufftree[i1].weight+hufftree[i2].weight;
		sign++;
	}
	outfile<<m<<"  "<<endl;
	for(i=0;i<m;i++)
	{   
		if(hufftree[i].zimu==' ')
		{
			outfile<<'_';
			outfile<<hufftree[i].weight;
			outfile<<endl;
		}

		else
		{
			outfile<<hufftree[i].zimu;
			outfile<<hufftree[i].weight;
			outfile<<endl;
		}
	}
	cout<<"*注意*:"<<endl;
	cout<<"    压缩文件的配置文件已经保存到t2.txt中,"<<endl;
	cout<<"    不要轻易的删除,否则将无法还原文件!"<<endl;
}
/////////////////////////////////////////
///////////////////////////////////////
//压缩文件,并把文件保存到二进制的文件里
///////////////////////////////////
/////////////////////////////////
int yasuo(element hufftree[] ,int m,node flag[],bit bitvec[])
{
	char ch;
	int i,n=7;
	int j,stare=0;
	ifstream infile("f1.txt",ios::in);
	ofstream outfile1("ys.dat",ios::out);
	bitset<8>temp4;
	while(infile.get(ch))
	{
		for(j=0;j<m;j++)
		{ 
			if(hufftree[j].zimu==ch)
			{
				for(i=flag[j].sign-1;i>=0; i--)
				{    
					temp4[n]=bitvec[j].bitvec2[i];
					n=n--;
					if(n==-1)
					{
						outfile1.write((char *)&temp4,1);
						stare++;
						n=7;
					}
					
					
				}
			}
		}
	}
	cout<<endl;
	if(n!=7)
	{
		outfile1.write((char *)&temp4,1);
		stare++;
	}
	outfile1.close();
	cout<<"压缩的文件应经已二进制的方式保存到ys.dat文件之中!"<<endl;
	return stare;
}
/////////////////
//对每一个字母进行哈夫曼编码///
//////////
void bianma(element hufftree[],int m,node flag[],bit bitvec[])
{
	int i;
	int j,temp=0,temp1=0,temp2=0;
	for(j=0;j<m;j++)
	{
		flag[j].sign=0;
		temp=0;
		temp2=j;
		for(i=m;i<2*m-1;i++)
		{
			if(hufftree[i].lchild==temp2)
			{  
				
				temp2=i;
				bitvec[j].bitvec2[temp]=0;
				flag[j].a[temp]='0';
				flag[j].sign++;
				temp++;
			}
			if(hufftree[i].rchild==temp2)
			{  
				temp2=i;
				bitvec[j].bitvec2[temp]=1;
				flag[j].a[temp]='1';
				flag[j].sign++;
				temp++;
			}
		}
    }
	/*cout<<"每个字符的哈夫曼编码"<<endl;//输出每一字母的哈夫曼编码
	for(j=0;j<m;j++)
	{
		cout<<hufftree[j].zimu<<"  ";
		for(temp=flag[j].sign-1;temp>=0;temp--)
			cout<<bitvec[j].bitvec2[temp];
		
		cout<<endl;
	}*/
}

/////////////////////////////////////////
//从配置的文件中读出每一个字母的频率,
//根据每一个字母的频率,重新建立哈夫曼树
//按二进制的方式,读取文件的内容
//比对每个字符的哈夫曼编码,输出字母
/////////////////////////////////////////
void jieyasuo(element hufftree[],node flag[ ],bit bitvec[],int stare) 
{
	int m,i=0,j=0;
	char ch;
	ifstream infile("t2.txt",ios::binary);//打开配置的文件
	ofstream outfile2("t3.txt",ios::out);//
	infile>>m;
	for(i=0;i<m;i++)
	{
		infile>>hufftree[i].zimu;
		infile>>hufftree[i].weight;
	}
	infile.close();
	for(i=0;i<2*m-1;i++)
	{
		hufftree[i].parent=-1;
		hufftree[i].lchild=-1;
		hufftree[i].rchild=-1;
	}
	bitset<8> temp4;
	int sign=0,k,i1,i2;
	for(k=m;k<2*m-1;k++)
	{
		i1=select(hufftree, sign,m);
		hufftree[i1].parent=k;
		hufftree[k].lchild=i1;
		sign++;
		i2=select(hufftree,sign,m);
		hufftree[i2].parent=k;
		hufftree[k].rchild=i2;
		hufftree[k].weight=hufftree[i1].weight+hufftree[i2].weight;
		sign++;
	}
	//cout<<"从配置文件中读取的息,建立的哈夫曼编码为"<<endl;
	bianma(hufftree ,m,flag,bitvec);
	ofstream outfile4("t4.txt",ios::out);
	ifstream  in_put("ys.dat",ios::in);
	FILE *fp;
	fp=fopen("ys.dat","ios::in");
	for(i=0;i<stare;i++)
	{
		in_put.read((char*)&temp4,1);
		outfile2<<temp4;
	}
	outfile2.close();
	ifstream outfile3("t3.txt",ios::in);
	string f,s,temp[10000];
	for(j=0;j<m;j++)
	{
		temp[j]="";
	}
	for(j=0;j<m;j++)
		for(i=flag[j].sign-1;i>=0;i--)
		{
			temp[j]=temp[j]+flag[j].a[i];
		}
		while(outfile3.get(ch))
		{
			s=s+ch;
			for(j=0;j<m;j++)
			{
				if(s==temp[j])
				{
					outfile4<<hufftree[j].zimu;
					s="";
				}
			}	
		}
		outfile4.close();
		cout<<"解压缩的文件已经保存到t4.txt中!"<<endl;
		cout<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -