• 大小: 16KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-10
  • 语言: 其他
  • 标签: 压缩软件  

资源简介

用huffman编码实现的压缩软件:把文件A编码成文件B,再把文件B翻译成文件C,比较A与C

资源截图

代码片段和文件信息

#include“Huffman.h“

HuffmanTree::HuffmanTree()// 初始化
{
int i;
for(i=0;i<511;i++)
{
node[i].weight =0;
node[i].parent=-1;
node[i].lchild=-1;
node[i].rchild=-1;
node[i].code=““;
}
}

HuffmanTree::~HuffmanTree()
{}

void HuffmanTree::GetWeight(char file[])
{
ifstream inf;
inf.open (fileios::binary);
inf.unsetf (ios::skipws );
unsigned char byteFile;
int pos=0;
while(inf.good())
{
byteFile=inf.get ();
if(inf.good())
node[byteFile].weight ++;
   }
inf.setf (ios::skipws );
inf.close ();
}

void HuffmanTree::SelectSmall (int &leastint &lessint n)
{
int ws1=-1ws2=-1;
least=less=-1;
int i;
for(i=0;i {
if(node[i].parent==-1)
{
if(ws1==-1 || node[i].weight {
ws2=ws1;
less=least;
ws1=node[i].weight;
least=i;
}
else if(ws2==-1 || node[i].weight {
ws2=node[i].weight;
less=i;
}

}
}
if(least==-1 || less==-1)
least=-1;
}

void HuffmanTree::CreateTree()
{
int i;
int leastless;
for(i=256;i<511;i++)
{
this->SelectSmall (leastlessi);
node[least].parent =i;
node[less].parent =i;
node[i].lchild=least;
node[i].rchild =less;
node[i].weight=node[least].weight+node[less].weight ;
}
}

void HuffmanTree::GetCode()
{
int mark[512];//编码标志
int i;
string codeString=““;
for(i=0;i<511;i++)
mark[i]=0;//初始化编码
int p=510;//根节点
while(p>=0)
{
if(mark[p]==0)
{
mark[p]=1;//左节点编码
if(node[p].lchild >=0)
{
p=node[p].lchild;
codeString+=“0“;
}
else if(node[p].rchild<0)
node[p].code=codeString;
}
else if(mark[p]==1)
{
mark[p]=2;//右节点编码
if(node[p].rchild>=0)
{
codeString+=“1“;
p=node[p].rchild;
}
}
else
{
p=node[p].parent;
codeString=codeString.substr(0codeString.length ()-1);
}
}
}

void HuffmanTree::Encode(char f1[]char f2[])
{
ifstream inf(f1ios::binary );
ofstream outf(f2ios::binary );
inf.unsetf (ios::skipws);//设置文件得读取空白符
string waiting=““;
unsigned char inByteoutByte;//用来构建读入输出字节
int i;

while(inf.good()) 
{
while(waiting.length()<8)
{
if(!inf.good())
break;
  inf.read ((char *) &inBytesizeof(unsigned char));//读取一个字节
if(inf.good())
waiting+=node[inByte].code;//得到该字节的huffman编码
}
while(waiting.length()>7)
{
outByte=‘\0‘;//字节八位清零
for(i=0;i<8;i++)
{
outByte=outByte<<1;//左移一位
if(waiting[i]==‘1‘)
outByte|=1;
}
outf.write((char *)&outBytesizeof(unsigned char));//将得到的字节存入文件
waiting=waiting.substr (8);//编码字符串去除已存储的前八位构成新的字符串
}
if(inf.eof ())
{
if(waiting.length ()==0)
outByte=static_cast(0);
else //文件结束,但还剩最后一段不满八位的编码
{
    outByte=‘\0‘;
for(i=0;i<(int)waiting.length ();i++)
{
outByte=outByte<<1;
if(waiting[i]==‘1‘)
outByte|=1;
}
outByte=outByte<<(8-waiting.length ());//将编码字段从尾部移到字节的高位
outf.write

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        108  2010-09-05 20:14  压缩软件\Huffman\a.txt

     文件         60  2010-09-05 22:14  压缩软件\Huffman\b.txt

     文件        108  2010-09-05 22:14  压缩软件\Huffman\c.txt

     文件       4707  2010-09-05 18:33  压缩软件\Huffman\Huffman.cpp

     文件       3715  2010-09-03 23:14  压缩软件\Huffman\Huffman.dsp

     文件        522  2010-09-02 16:45  压缩软件\Huffman\Huffman.dsw

     文件        552  2010-09-03 21:33  压缩软件\Huffman\Huffman.h

     文件      66560  2010-09-05 23:19  压缩软件\Huffman\Huffman.ncb

     文件      49664  2010-09-05 23:19  压缩软件\Huffman\Huffman.opt

     文件       1218  2010-09-05 18:33  压缩软件\Huffman\Huffman.plg

     文件        336  2010-09-06 09:32  压缩软件\Huffman\Huffman.sln

    ..A..H.      6144  2010-09-06 09:32  压缩软件\Huffman\Huffman.suo

     文件        104  2010-09-05 18:33  压缩软件\Huffman\Main.cpp

     文件       1467  2010-09-03 23:13  压缩软件\Huffman\Run.cpp

     文件        190  2010-09-03 23:10  压缩软件\Huffman\Run.h

     文件        144  2010-09-03 21:30  压缩软件\Huffman\StdAfx.h

     目录          0  2010-09-07 14:12  压缩软件\Huffman

     目录          0  2010-10-10 22:44  压缩软件

----------- ---------  ---------- -----  ----

               135599                    18


评论

共有 条评论