哈夫曼压缩与解压算法(可以直接运行


哈夫曼压缩与解压算法(可以直接运行),压缩成二进制文件,而且生成了txt文件可以查看哈夫曼编码。C++代码
资源截图
代码片段和文件信息
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include  
#include 
using namespace std;
typedef struct
{
int weight;
int parent lchild rchild;
}HafuNode*HufumanTree;
typedef struct
{
char *data;
int *num;
int length;
}TNode;
typedef struct
{
char *data;
char** HM;
}Code;
typedef char** HuffmanCode;
void initTnode(TNode &tnod)
{
tnod.data = new char[256];
tnod.num = new int[256];
if (tnod.data == NULL || tnod.num==NULL)
{
cout << “发生错误“ << endl;
exit(1);
}
tnod.length = 0;
}
void initmap(map& hafuman HuffmanCode code int nTNode nodeint &codelength)
{
for (int i = 1; i <= n; i++)
{
string s = code[i];
hafuman.insert(pair(s node.data[i - 1]));
codelength = s.size()*node.num[i - 1]+ codelength;
}


}
void Read(vector& s)
{
char ch;
ifstream infile(“test.txt“ ios::in);
if (!infile)
{
cout << “open error“ << endl;
exit(1);
}
while (infile.peek() != EOF)
{
 infile.get(ch);
 s.push_back(ch);
}
infile.close();
}
bool find(const char ch TNode t)
{
for (int i = 0; i < t.length; i++)
{
if (t.data[i] == ch)
{
return true;
}
}
return false;
}
void TNodeCount(TNode &t vector v)
{
int m = v.size()j=0;
char ch;
for (int i = 0; i < m; i++)
{
ch = v[i];
if (!find(ch t))
{
t.data[j] = ch;
t.num[j] = count(v.begin() v.end() ch);
t.length++;
j++;
}
}
}
void Select(HufumanTree &tree int a int &b int &c)
{
int min1 min2 minweight = 10000;
for (int i = 1; i <= a; i++)
{
if (tree[i].parent == 0)
{
if (tree[i].weight < minweight)
{
minweight = tree[i].weight;
min1 = i;
}
}
}
tree[min1].parent = 1;
minweight = 10000;
for (int i = 1; i <= a; i++)
{
if (tree[i].parent == 0)
{
if (tree[i].weight < minweight)
{
minweight = tree[i].weight;
min2 = i;
}
}
}
tree[min2].parent = 1;
b = min1;
c = min2;
}
void CreateHuffmanTree(HufumanTree &tree TNode node int n)
{
if (n <= 1)
{
return;
}
int m = 2 * n - 1;
tree = new HafuNode[m+1];
for (int i = 1; i <= m; i++)//为0表示没有左右节点,父节点
{
tree[i].lchild = 0;
tree[i].parent = 0;
tree[i].rchild = 0;
}
for (int i = 1; i <= n; i++)
{
tree[i].weight = node.num[i - 1];
}
int s1 s2;
for (int i = n + 1; i <= m; i++)
{
Select(tree i - 1 s1 s2);
tree[s1].parent = i;
tree[s2].parent = i;
tree[i].lchild = s1;
tree[i].rchild = s2;
tree[i].weight = tree[s1].weight + tree[s2].weight;
}
}
void CreatHuffmanCode(HufumanTree tree HuffmanCode &code int n)
{
int pare child start;
code = new char *[n + 1];
char* cd = new char[n];
cd[n - 1] = ‘‘;
for (int i = 1; i <= n; i++)
{
start = n - 1;
child = i;
pare = tree[i].parent;
while (pare != 0)
{

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

    ..A..H.     27648  2018-06-09 18:03  hafuaman.vshafuamanv15.suo

     文件   36753408  2018-06-09 18:03  hafuaman.vshafuamanv15Browse.VC.db

     文件   28442624  2018-06-05 22:24  hafuaman.vshafuamanv15ipchAutoPCH3c47d55aa3b80158源.ipch

     文件   75497472  2018-06-09 17:56  hafuaman.vshafuamanv15ipchAutoPCH0a45fc93d32f417源.ipch

     文件     184320  2018-06-09 17:58  hafuamanDebughafuaman.exe

     文件    1138536  2018-06-09 17:58  hafuamanDebughafuaman.ilk

     文件    1921024  2018-06-09 17:58  hafuamanDebughafuaman.pdb

     文件         33  2018-06-09 17:59  hafuamanhafuamancompression.txt

     文件          5  2018-06-09 17:59  hafuamanhafuamancompression_2.dat

     文件        928  2018-06-09 17:58  hafuamanhafuamanDebughafuaman.log

     文件        572  2018-06-09 17:58  hafuamanhafuamanDebughafuaman.tlogCL.command.1.tlog

     文件      33540  2018-06-09 17:58  hafuamanhafuamanDebughafuaman.tlogCL.read.1.tlog

     文件        344  2018-06-09 17:58  hafuamanhafuamanDebughafuaman.tlogCL.write.1.tlog

     文件        205  2018-06-09 17:58  hafuamanhafuamanDebughafuaman.tloghafuaman.lastbuildstate

     文件        998  2018-06-09 17:58  hafuamanhafuamanDebughafuaman.tloglink.command.1.tlog

     文件       2896  2018-06-09 17:58  hafuamanhafuamanDebughafuaman.tloglink.read.1.tlog

     文件        328  2018-06-09 17:58  hafuamanhafuamanDebughafuaman.tloglink.write.1.tlog

     文件     355328  2018-06-09 17:58  hafuamanhafuamanDebugvc141.idb

     文件     585728  2018-06-09 17:58  hafuamanhafuamanDebugvc141.pdb

     文件     854178  2018-06-09 17:58  hafuamanhafuamanDebug源.obj

     文件         11  2018-06-09 17:59  hafuamanhafuamandecompression.txt

     文件       6001  2018-06-05 21:53  hafuamanhafuamanhafuaman.vcxproj

     文件        948  2018-06-05 21:52  hafuamanhafuamanhafuaman.vcxproj.filters

     文件        165  2018-06-05 19:20  hafuamanhafuamanhafuaman.vcxproj.user

     文件         11  2018-06-08 16:30  hafuamanhafuaman est.txt

     文件       6830  2018-06-09 17:58  hafuamanhafuaman源.cpp

     文件       1438  2018-06-05 19:20  hafuamanhafuaman.sln

     目录          0  2018-06-05 19:30  hafuaman.vshafuamanv15ipchAutoPCH3c47d55aa3b80158

     目录          0  2018-06-09 17:56  hafuaman.vshafuamanv15ipchAutoPCH0a45fc93d32f417

     目录          0  2018-06-05 19:31  hafuaman.vshafuamanv15ipchAutoPCH

............此处省略12个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)