基于huffman编码的文本压缩程序

二、程序功能描述

程序实现的功能:对文本文件进行压缩以及对压缩的文本文件进行解压缩。程序的实现的理论依据是赫夫曼编码。赫夫曼编码是一种无损的压缩算法,一般用来压缩文本和程序文件。赫夫曼压缩属于可变代码长度算法一族。意思是个体符号(例如,文本文件中的字符)用一个特定长度的位序列替代。因此,在文件中出现频率高的符号,使用短的位序列,而那些很少出现的符号,则用较长的位序列。

程序由三个文件组成:头文件CourseDesign.h、函数实现文件CourseDesign.cpp、测试文件Test.cpp。在CourseDesign.h中声明数据的存储结构以及程序所需要的处理函数;CourseDesign.cpp文件实现在CourseDesign.h中声明的函数;Test.cpp负责对所实现的函数进行调用测试,确定是否满足程序设计要求。

利用赫夫曼编码实现对文本的压缩的过程大致为:打开要压缩的文本文件,读取文件中的字符,统计文件中不同字符出现的频率,建立赫夫曼树,通过赫夫曼树对出现的互不相同的字符进行编码,建立编码表,接着将将赫夫曼树(即解码表)写入压缩文件中。再次重新读取文件中的字符,对每个字符通过查阅编码表确定对应的编码,将该字符的赫夫曼编码写入压缩文件。对压缩文件的解压过程为:打开压缩文件,读取压缩文件解码表部分,读取压缩文件的编码数据,将压缩数据通过解码表进行解码,将解码出的字符写入解码文件中。

程序执行后,用户按照程序的提示选择相应的功能选项。当用户选择压缩功能,此时程序要求用户输入要压缩的文本文件的路径,用户输入完成后。程序检查文件是否能够建立。检查完成后,程序将文件从硬盘读入内存。接着程序将统计不同字符出现的频率以及建立编码表的初步结构。例如当文件中存有如下表所示字符。

表1 文件字符属性表

字符

第一字节机内码/ASCII

第二字节机内码

权重

181

196

20

a

97

0

9

176

209

14

177

237

5

176

224

1

178

185

2

176

214

17

183

192

12

183

201

9

178

169

13

176

252

2

178

197

6

183

189

8

176

221

3

A

65

0

3

183

221

5

177

216

5

英文字符在计算机中是以标准ASCII码进行表示,一个英文字符占一个字节空间,编码范围为0~127;汉字在计算机中是以GB2312编码进行表示。每个汉字占两个字节存储空间,汉字的存储使用机内码,各字节的机内码编码范围为160~254。现在需要考虑使用怎样的数据结构来存放这些字符,如果采用如下简单的数据结构存放:

typedef struct

{

  char  data[3];  // 存放字符

int   internal_code1;  // 存放第一字节的机内码/ASCII码

int   internal_code2;  // 存放第二字节的机内码,英文默认为0

int   weight;  // 存放字符的权重

char  *code;  // 字符的赫夫曼编码

}CodeList, *CodePList;

分析所要处理的字符数据会发现:许多的字符的第一字节的机内码相同,如“防”、“飞”、“方”、“份”,第一字节机内码都是183。这是因为汉字是通过划分区号和位号来表示的,所有汉字被划分成了94个区,94个位,所以当汉字属于同一个区,那么它的第一字节机内码就会相同。如果采用如上的数据结构建立的线性表来存放处理字符,就会存在大量数据冗余。

在这种情况下,就有必要为特定的数据设计合适的数据结构。通过分析,采用如下数据结构:

typedef struct

{

    char internal_code;  // 存放第二字节机内码

    char *code;  // 存放字符的赫夫曼编码

}InternalCode;

 

typedef struct

{

  int count;  // 已编码字符数

  char internal_code;  // 存放第一字节机内码

  InternalCode *internal_code_address;  // 第二字节机内码及字符的

 }HuffmanCode, *HuffmanPCode;           //赫夫曼编码的地址

该结构的优点:当汉字的第一字节机内码相同,则该第一字节机内码只会被存储一次,从而消除汉字第一字节机内码存储的冗余,而且可以方便的使用折半查找快速检索编码表来确定字符的赫夫曼编码。采用该数据结构对表1数据进行表示如图1。

 

基于huffman编码的文本压缩程序

1 编码表HC的存储结构 

这种数据结构形式上类似于图的邻接表结构,功能上类似于哈希表的链地址法。但邻接表的表结点采用链式存储,而图1的表结点和头结点都采用线性表储存。图1中编码表HC的内码1是纵向非递减排列,内码2是横向非递减排列。HC[i].count – HC[i – 1].count等于HC[i]实际存储的字符数量。例如, HC[3]中字符数为7,HC[2]中字符数为2,则HC[3]存放了5个字符,这5个字符拥有相同的第一字节机内码176。

程序执行压缩操作详细过程:当程序从文件中读取一个字符后,通过字符的编码范围分析该字符是属于ASCII还是GB2312,若是ASCII编码,增加编码表HC纵向表长,将该字符的ASCII码按非递减次序插入到内码1处,并将当前位置的字符数加1,并置内码2默认为0;如果是汉字,首先通过折半查找算法纵向查找编码表HC的内码1成员,若当前汉字第一字节机内码已经出现过,则折半查找返回该机内码1在HC表中的位置,增加当前位置的横向表长,将汉字的第二字节机内码按非递减次序插入当前位置的内码2处。否则将汉字的第一字节机内码按非递减次序插入HC表的内码1区域,第二字节机内码直接插入内码2处。在读取文件的同时记录文件中各字符出现的频率,当编码表HC表构建完成,此时w = {3, 9, 14, 3, 1, 2, 17, 5, 5, 13, 2, 6, 20, 9, 8, 5, 12}。依次从w中选择权重最小并且双亲为0的两个结点,根据这两个结点生成新的结点,新结点的权重为这两个最小结点的和,新结点的左右子树为这两个结点在w中的位置。根据表1数据构建赫夫曼树如图2所示。赫夫曼树存储结构的初始状态如图3(a),终结状态如图3(b)。

 

基于huffman编码的文本压缩程序
 

图2 根据表1构造的赫夫曼树

 

基于huffman编码的文本压缩程序

图3 (a)  HT初始状态                       图3 (b) HT终止状态 

根据生成的赫夫曼树对HC表中的字符进行编码,编码的方法:从该叶子到根逆向求该字符的编码。例如图2中“把”的权值为14,对应的编码为:“000”。将得到的赫夫曼编码写入HC[i].internal_code_address[j].code指向的区域。当字符编码完成之后,打开压缩文件,将赫夫曼树HT中除权重以外的数据(解码无需权重信息)写入压缩文件中,作为下一次解压缩的解码表。再次打开要压缩的文本文件,读取文件中的字符,根据编码的范围确定该字符是ASCII还是GB2312,如果ASCII则调用折半查找函数,在编码表HC中进行纵向查找,查找此ASCII出现的位置p1,该字符的编码为HC[p1].internal_code_address[1].code;如果字符是汉字,则调用折半查找先纵向查找该汉字的第一字节机内码在HC中的位置p1,然后从HC[p1].internal_code_address开始横向查找该汉字的第二字节机内码的位置p2,这样就得到了该汉字的赫夫曼编码为HC[p1].internal_code_address[p2].code因为赫夫曼编码在HC表中是以字符串形式存放(因为计算机的基本储单位是字节,如果以位存放,需要另设一个空间来表示未编码的位空间大小)。所以需要将字符串“0101“转换为二进制0101写入文件。因为每个赫夫曼编码的长度是不一样的,假设某字符的赫夫曼长度为4,则将该编码写入一个字节后,还剩余4个位,则下一次可以继续从第5个位开始写入,当所有字符的编码都写入完毕后,最后一个字节并不一定会用完,所以需要附设一个字节来记录最后一个字符编码实际写入的编码位数。编码文件的结构如下图所示:

                         基于huffman编码的文本压缩程序

                              图4 压缩文件存储结构

程序解压文件:打开压缩文件,取出压缩文件的解码表长度N,根据N读取N条解码表记录,重建解码表HT,然后读取压缩数据DATA,解码的过程是从根出发,按DATA数据的0或1确定找左子树还是右子树,直至叶子结点,便求得了DATA相应的字符。将字符写入文件,直至所有DATA数据处理完毕,整个解压过程结束。

三、程序源代码

1. 头文件CourseDesign.h

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef _COURSEDESIGN_H_  
  2. #define _COURSEDESIGN_H_  
  3.    
  4. //-----Huffman树存储结构  
  5. typedef struct  
  6. {  
  7.   char ch[3];  
  8.   unsigned int weight;  
  9.   unsigned int parent, lchild, rchild;  
  10. }HTNode, *HuffmanTree;  
  11.    
  12. //----Huffman编码表存储结构  
  13. typedef struct  
  14. {  
  15.   char internal_code;  
  16.   char *code;  
  17. }InternalCode;  
  18.    
  19. typedef struct  
  20. {  
  21.   int count;  
  22.   char internal_code;  
  23.   InternalCode *internal_code_address;  
  24. }HuffmanCode, *HuffmanPCode;  
  25.    
  26. //-------解码表存储结构  
  27. typedef struct  
  28. {  
  29.   char ch[3];  
  30.   unsigned int lchild, rchild;  
  31. }DecodeList, *DecodePList;  
  32.    
  33. //------辅助数组,置/取一个字节的指定位  
  34. const static unsigned char mask[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};  
  35.    
  36. template<class T> static int xj_Search_Bin(int key, T L, int low, int high);  
  37. template<class T> static void xj_InsertSort(T L , int start, int end);  
  38. void xj_Select(const HuffmanTree HT, int n, int &s1, int &s2);  
  39. void xj_Statistics(HuffmanPCode &HC, int internal_code1, int internal_code2, int (*FrequencyMeter)[255], int &n);  
  40. bool xj_Init(char *filename, HuffmanPCode &HC, int *&w, int &n);  
  41. void xj_CreateHuffmanTree(HuffmanTree &HT, const HuffmanPCode HC, const int *w, int n);  
  42. void xj_HuffmanCoding(const HuffmanTree HT, HuffmanPCode HC, int n);  
  43. bool xj_Compress(char *ifilename, char *ofilename, const HuffmanPCode HC, const HuffmanTree HT, int n);  
  44. bool xj_DeCompress(char *ifilename, char *ofilename);  
  45. void xj_Interface();  
  46.    
  47. #endif  

2. 函数实现文件CourseDesign.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include"CourseDesign.h"  
  2. #include<iostream>  
  3. #include<fstream>  
  4. #include<iomanip>  
  5. #include<malloc.h>  
  6. #include<string.h>  
  7. using namespace std;  
  8.    
  9. //-----------折半查找----------------  
  10. template<class T>  
  11. int xj_Search_Bin(int key, T L, int low, int high)  
  12. {  
  13.   int mid = 0;  
  14.   int internal_code;  
  15.    
  16.   while (low <= high)  
  17.   {  
  18.      mid = (low + high) / 2;  
  19.      internal_code = int(L[mid].internal_code & 0xFF);  
  20.      if (key == internal_code)  
  21.      {  
  22.          return mid;  
  23.      }  
  24.      else if (internal_code > key)  
  25.      {  
  26.          high = mid - 1;  
  27.      }  
  28.      else  
  29.      {  
  30.          low = mid + 1;  
  31.      }  
  32.   }  
  33.   return 0;  
  34. }  
  35.    
  36. //--------对HC表的字符域做插入非递减排序-----  
  37. template<class T>  
  38. void xj_InsertSort(T L , int start, int end)  
  39. {  
  40.   int i;  
  41.   L[0] = L[end];  
  42.   i = end - 1;  
  43.   while (i >= start && int(L[i].internal_code & 0xFF) > int(L[0].internal_code & 0xFF))  
  44.   {  
  45.      L[i + 1] = L[i];  
  46.      i--;  
  47.   }  
  48.   L[i + 1] = L[0];  
  49. }  
  50.    
  51. //------- 寻找权重最小的两个结点----------------------  
  52. void xj_Select(const HuffmanTree HT, int n, int &s1, int &s2)  
  53. {  
  54.   int i = 0;  
  55.   s1 = s2 = 0;  
  56.   for (i = 1; i <= n; ++i)  
  57.   {  
  58.      if (HT[i].parent == 0)  
  59.      {  
  60.          if (s1 == 0)  
  61.          {  
  62.             s1 = i;  
  63.          }  
  64.          else if (s2 == 0)  
  65.          {  
  66.             s2 = i;  
  67.          }  
  68.          else if (HT[i].weight < HT[s1].weight || HT[i].weight < HT[s2].weight)  
  69.          {  
  70.             s1 = HT[s1].weight < HT[s2].weight ? s1 : s2;  
  71.             s2 = i;  
  72.          }  
  73.      }  
  74.   }  
  75. }  
  76.    
  77.    
  78. //----构建HC.internal_code以及HC.internal_code_address结构-------------  
  79. void xj_Statistics(HuffmanPCode &HC, int internal_code1, int internal_code2, int (*FrequencyMeter)[255], int &n)  
  80. {  
  81.   int position;  
  82.   if (internal_code1 < 128)  
  83.   {  
  84.      if (FrequencyMeter[internal_code1][0] == 0)  
  85.      {  
  86.          ++n;  
  87.          HC = (HuffmanPCode)realloc(HC, (n + 1) * sizeof(HuffmanCode));  
  88.          HC[n].internal_code = internal_code1;  
  89.          HC[n].count = 1;  
  90.          HC[n].internal_code_address = (InternalCode *)malloc(2 * sizeof(InternalCode));  
  91.          HC[n].internal_code_address[1].internal_code = 0;  //0号单元未用  
  92.          xj_InsertSort(HC, 1, n);  
  93.      }  
  94.      ++FrequencyMeter[internal_code1][0];  
  95.   }  
  96.   else  
  97.   {  
  98.      if (FrequencyMeter[internal_code1][internal_code2] == 0)  
  99.      {  
  100.          position = xj_Search_Bin(internal_code1, HC, 1, n);  
  101.          if (position != 0)  
  102.          {  
  103.             ++HC[position].count;  
  104.             HC[position].internal_code_address = (InternalCode *)realloc(HC[position].internal_code_address, (HC[position].count + 1) * sizeof(InternalCode));  
  105.             HC[position].internal_code_address[HC[position].count].internal_code = internal_code2;  
  106.             xj_InsertSort(HC[position].internal_code_address, 1, HC[position].count);  
  107.          }  
  108.          else  
  109.          {  
  110.             ++n;  
  111.             HC = (HuffmanPCode)realloc(HC, (n + 1) * sizeof(HuffmanCode));  
  112.             HC[n].internal_code = internal_code1;  
  113.             HC[n].count = 1;  
  114.             HC[n].internal_code_address = (InternalCode *)malloc(2 * sizeof(InternalCode));  
  115.             HC[n].internal_code_address[1].internal_code = internal_code2;  
  116.             xj_InsertSort(HC, 1, n);  
  117.          }  
  118.      }  
  119.      ++FrequencyMeter[internal_code1][internal_code2];  
  120.   }  
  121. }  
  122.    
  123. //--------统计不同字符出现的频率以及构建HC的机内码成员结构-------  
  124. bool xj_Init(char *filename, HuffmanPCode &HC, int *&w, int &n)  
  125. {  
  126.   ifstream ifs(filename);  
  127.   int i = 0, j = 0;  
  128.   int FrequencyMeter[255][255] = {0};  
  129.   char ch1, ch2;  
  130.   n = 0;  
  131.   HC = NULL;  
  132.   w = NULL;  
  133.    
  134.   if (ifs.fail())  
  135.   {  
  136.      cout<<"can't open file!"<<endl;  
  137.      return false;  
  138.   }  
  139.   while ((ch1 = ifs.get()) != EOF)  
  140.   {  
  141.      if (int(ch1 & 0xFF) > 128)  
  142.      {  
  143.          ch2 = ifs.get();  
  144.      }  
  145.      else  
  146.      {  
  147.          ch2 = 0;  
  148.      }  
  149.      xj_Statistics(HC, int(ch1 & 0xFF), int(ch2 & 0xFF), FrequencyMeter, n);  
  150.   }  
  151.   HC[0].count = 0;  
  152.   for (i = 2; i <= n; ++i) HC[i].count += HC[i - 1].count;  
  153.   w = (int *)malloc(HC[n].count * sizeof(int));  
  154.   for (i = 1; i <= n; ++i)  
  155.   {  
  156.      for (j = HC[i - 1].count; j < HC[i].count; ++j)  
  157.      {  
  158.          w[j] = FrequencyMeter[int(HC[i].internal_code & 0xFF)][int(HC[i].internal_code_address[j - HC[i - 1].count + 1].internal_code & 0xFF)];  
  159.      }  
  160.   }  
  161.   ifs.close();  
  162.   return true;  
  163. }  
  164.    
  165. //--------构造赫夫曼树HT---------------------  
  166. void xj_CreateHuffmanTree(HuffmanTree &HT, const HuffmanPCode HC, const int *w, int n)  
  167. {  
  168.   int i = 0, j = 0;  
  169.   int m = 0, s1 = 0, s2 = 0;  
  170.    
  171.   if (HC[n].count <= 1) return;  
  172.   m = 2 * HC[n].count - 1;  
  173.   HT = (HuffmanTree)malloc((m + 1) * sizeof(HTNode));  
  174.   for (i = 1; i <= n; ++i)  
  175.   {  
  176.      for (j = HC[i - 1].count + 1; j <= HC[i].count; ++j, ++w)  
  177.      {  
  178.          HT[j].ch[0] = HC[i].internal_code;  
  179.          HT[j].ch[1] = HC[i].internal_code_address[j - HC[i - 1].count].internal_code;  
  180.          HT[j].ch[2] = '\0';  
  181.          HT[j].weight = *w;  
  182.          HT[j].lchild = HT[j].rchild = HT[j].parent = 0;  
  183.      }  
  184.   }  
  185.   for (i = HC[n].count + 1; i <= m; ++i)  
  186.   {  
  187.      *HT[i].ch = 0;  
  188.      HT[i].weight = HT[i].lchild = HT[i].rchild = HT[i].parent = 0;  
  189.   }  
  190.   for (i = HC[n].count + 1; i <= m; ++i)  
  191.   {  
  192.      xj_Select(HT, i - 1, s1, s2);  
  193.      HT[s1].parent = i; HT[s2].parent = i;  
  194.      HT[i].lchild = s1; HT[i].rchild = s2;  
  195.      HT[i].weight = HT[s1].weight + HT[s2].weight;  
  196.   }  
  197. }  
  198.    
  199. //----------建立编码表HC-------------------  
  200. void xj_HuffmanCoding(const HuffmanTree HT, HuffmanPCode HC, int n)  
  201. {  
  202.   int start = 0, c = 0, f = 0;  
  203.   int i = 0, k = 1, r = 1; ;  
  204.   char *cd = NULL;  
  205.   cd = (char *)malloc(HC[n].count * sizeof(char));  
  206.   cd[HC[n].count - 1] = '\0';  
  207.   for (i = 1; i <= HC[n].count; ++i)  
  208.   {  
  209.      start = HC[n].count -1;  
  210.      for (c = i, f = HT[i].parent; f!= 0; c=f, f = HT[f].parent)  
  211.      {  
  212.          if (HT[f].lchild == c)  
  213.          {  
  214.             cd[--start] = '0';  
  215.          }  
  216.          else  
  217.          {  
  218.             cd[--start] = '1';  
  219.          }  
  220.      }  
  221.      if (k > HC[r].count - HC[r - 1].count)  
  222.      {  
  223.          k = 1;  
  224.          ++r;  
  225.      }  
  226.      HC[r].internal_code_address[k].code = (char *)malloc((HC[n].count - start) * sizeof(char));  
  227.      strcpy(HC[r].internal_code_address[k].code, &cd[start]);  
  228.      ++k;  
  229.   }  
  230.   free(cd);  
  231. }  
  232.    
  233. //-------------------------压缩文件--------------  
  234. bool xj_Compress(char *ifilename, char *ofilename, const HuffmanPCode HC, const HuffmanTree HT, int n)  
  235. {  
  236.   ifstream ifs(ifilename);  
  237.   ofstream ofs(ofilename, ios::binary);  
  238.   int bit_size = 0;  
  239.   int position1, position2;  
  240.   int internal_code1, internal_code2;  
  241.   char ch;  
  242.   char code = 0;  
  243.   char *code_address;  
  244.   DecodePList decode_list = (DecodePList)malloc((HC[n].count * 2) * sizeof(DecodeList));  
  245.    
  246.   if (ifs.fail() || ofs.fail())  
  247.   {  
  248.      cout<<"Can't open the file!"<<endl;  
  249.      return false;  
  250.   }  
  251.    
  252.   ofs.write((char *)&HC[n].count, sizeof(int));  //写入解码表  
  253.   for (int i = 1; i <= 2 * HC[n].count - 1; ++i)  
  254.   {  
  255.      strcpy(decode_list[i].ch, HT[i].ch);  
  256.      decode_list[i].lchild = HT[i].lchild;  
  257.      decode_list[i].rchild = HT[i].rchild;  
  258.      ofs.write((char *)&decode_list[i], sizeof(DecodeList));  
  259.   }  
  260.    
  261.   while ((ch = ifs.get()) != EOF)  
  262.   {  
  263.      internal_code1 = int(ch & 0xFF);  
  264.      position1 = xj_Search_Bin(internal_code1, HC, 1, n);  
  265.      if (internal_code1 < 128)  
  266.      {  
  267.          internal_code2 = 0;  
  268.          position2 = 1;  
  269.      }  
  270.      else  
  271.      {  
  272.          internal_code2 = int(ifs.get() & 0xFF);  
  273.          position2 = xj_Search_Bin(internal_code2, HC[position1].internal_code_address, 1, HC[position1].count - HC[position1 - 1].count);  
  274.      }  
  275.      code_address = HC[position1].internal_code_address[position2].code;  
  276.      while (*code_address)  
  277.      {  
  278.          code |= (*code_address++ - 48) * mask[bit_size % 8];  
  279.          ++bit_size;  
  280.          if (bit_size % 8 == 0)  
  281.          {  
  282.             ofs<<code;  
  283.             code = 0;  
  284.          }  
  285.      }  
  286.   }  
  287.   if (bit_size % 8 != 0)  
  288.   {  
  289.      ofs<<code;  
  290.      ofs<<char(bit_size % 8);  
  291.   }  
  292.   else  
  293.   {  
  294.      ofs<<char(8);  
  295.   }  
  296.   ifs.clear();  
  297.   ifs.seekg(0, ios::end);  
  298.   cout<<"压缩完成!"<<endl;  
  299.   cout<<"原始文件大小: "<<ifs.tellg()<<" B"<<endl;  
  300.   cout<<"压缩文件大小:" <<ofs.tellp()<<" B"<<endl;  
  301.   cout<<"压缩率: "<<float(ofs.tellp())/float(ifs.tellg())*100<<" %\n";  
  302.   free(decode_list);  
  303.   free(HT);  
  304.   free(HC);  
  305.   ifs.close();  
  306.   ofs.close();  
  307.   return true;  
  308. }  
  309.    
  310. //---------------解压缩文件---------------------  
  311. bool xj_DeCompress(char *ifilename, char *ofilename)  
  312. {  
  313.   ifstream ifs(ifilename,ios::binary);  
  314.   ofstream ofs(ofilename);  
  315.   int bit_size;  
  316.   int i;  
  317.   int m, n;  
  318.   char buf;  
  319.   int value;  
  320.   DecodePList decode_list;  
  321.    
  322.   if (ifs.fail() || ofs.fail())  
  323.   {  
  324.      cout<<"Can't open the file!"<<endl;  
  325.      return false;  
  326.   }  
  327.   ifs.read((char *)&n, sizeof(int));  // 读取解码表  
  328.   m = 2 * n - 1;  
  329.   decode_list = (DecodePList)malloc((m + 1) * sizeof(DecodeList));  
  330.   for (i = 1; i <= m; ++i)  
  331.   {  
  332.      ifs.read((char *)&decode_list[i], sizeof(DecodeList));  
  333.   }  
  334.   streampos pos1 = ifs.tellg();  
  335.   ifs.seekg(-1, ios::end);  
  336.   streampos pos2 = ifs.tellg();  
  337.   bit_size = (int(pos2 - pos1) - 1) * 8 + ifs.get();  
  338.   ifs.seekg(pos1, ios::beg);  
  339.   for (i = 0; i < bit_size; ++i)  
  340.   {  
  341.      if (i % 8 == 0)  
  342.      {  
  343.          ifs.read(&buf, 1);  
  344.          value = int(buf & 0xFF);  
  345.      }  
  346.      if (int(value & mask[i % 8]) != mask[i % 8]) //value编码的i % 8 + 1位是0  
  347.      {  
  348.          m = decode_list[m].lchild;  
  349.      }  
  350.      else  
  351.      {  
  352.          m = decode_list[m].rchild;  
  353.      }  
  354.      if (decode_list[m].lchild == 0 )  
  355.      {  
  356.          ofs<<decode_list[m].ch;  
  357.          m = 2 *n - 1;  
  358.      }  
  359.   }  
  360.   ifs.close();  
  361.   ofs.close();  
  362.   free(decode_list);  
  363.   cout<<"解压完成!"<<endl;  
  364.   return true;  
  365. }  
  366.    
  367. void xj_Interface()  
  368. {  
  369.   cout<<"----------------------------------------------------------------\n\n";  
  370.   cout<<"                    《基于Huffman编码的文档压缩》\n\n";  
  371.   cout<<"           学号:2011150058    姓名:张剑锋     班级: 软工01\n\n";  
  372.   cout<<"                    请选择功能选项:\n\n";  
  373.   cout<<"                    1. 压缩文件\n";  
  374.   cout<<"                    2. 解压缩文件\n";  
  375.   cout<<"                    3. 退出\n\n";  
  376.   cout<<"----------------------------------------------------------------\n";  
  377. }<span style="white-space:pre"> </span>  

3. 测试文件Test.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include"CourseDesign.h"  
  2. #include <time.h>  
  3. #include<iostream>  
  4. #include<malloc.h>  
  5. using namespace std;  
  6.    
  7. int main()  
  8. {  
  9.     clock_t start,finish;  
  10.     double duration;  
  11.     int n, *w;  
  12.     char ifile[50];  
  13.     char compress_file[50];  
  14.     char decompress_file[50];  
  15.     char key;  
  16.     HuffmanTree HT = NULL;  
  17.     HuffmanPCode HC = NULL;  
  18.    
  19.     do  
  20.     {  
  21.        system("cls");  
  22.        xj_Interface();  
  23.         cin>>key;  
  24.        switch (key)  
  25.        {  
  26.        case '1':  
  27.            cout<<"请输入压缩文件路径:"<<endl;  
  28.            cin>>ifile;  
  29.            strcpy(compress_file, ifile);  
  30.            compress_file[strlen(ifile) - 4] = '\0';  
  31.            strcat(compress_file, ".huf");  
  32.            cout<<"请稍等..."<<endl;  
  33.            start=clock();  
  34.            if (!xj_Init(ifile, HC, w, n)) break;  
  35.            if (HC[n].count <= 1) break;  
  36.            xj_CreateHuffmanTree(HT, HC, w, n);  
  37.            xj_HuffmanCoding(HT, HC, n);  
  38.            xj_Compress(ifile, compress_file, HC, HT, n);  
  39.            finish=clock();  
  40.            duration=(double)(finish-start)/CLOCKS_PER_SEC;  
  41.            cout<<"压缩时间:"<<duration<<"s"<<endl;  
  42.            break;  
  43.        case '2':  
  44.            cout<<"请输入解压缩文件路径:"<<endl;  
  45.            cin>>compress_file;  
  46.            strcpy(decompress_file, compress_file);  
  47.            decompress_file[strlen(compress_file) - 4] = '\0';  
  48.            strcat(decompress_file, ".txt");  
  49.            cout<<"请稍等..."<<endl;  
  50.            start=clock();  
  51.            xj_DeCompress(compress_file, decompress_file);  
  52.            finish=clock();  
  53.            duration=(double)(finish-start)/CLOCKS_PER_SEC;  
  54.            cout<<"解压缩时间:"<<duration<<"s"<<endl;  
  55.            break;  
  56.        case '3':  
  57.            break;  
  58.        default :  
  59.            cout<<"输入编号错误"<<endl;  
  60.            break;  
  61.        }  
  62.        cout<<"是否继续<y/n>"<<endl;  
  63.        cin>>key;  
  64.     }while(key == 'y');  
  65.     cout<<"谢谢使用!"<<endl;  
  66.     return 0;  
  67. }  

四、程序运行描述以及截图


五、程序评价

        此程序对大小为2.08M的文件进行压缩和解压缩测试,运行的总时间在2.3s左右,压缩后文件为1.36M。但与主流压缩软件2345好压相比还是有差距。使用2345好压压缩,压缩文件大小为679KB。所以压缩性能上还是有待提高。

        几点想法:若文件进行分段压缩,效率会更好。其基本思想:若有5个字符,对应的权重w = {1, 2, 3, 4, 5},则对应的赫夫曼编码为:000001011011。此时如果将w = {w1, w2}, w1 = {1, 2, 3}, w2 = {4, 5}。则对应的编码就变为:00、01、1、0、1。可以让频率高和频率低的字符都有较短的编码。程序实现时需要将压缩文本进行分段压缩,解压时也需要分段解压。每一段压缩文件拥有一个独立的解码表。一个解码表只对该段压缩文本进行解码。分段压缩/解压互不依赖。所以可以引入多线程技术实现并行处理,当一个进程正在向文件写入数据时,它就会主动释放所占有的CPU资源,并将自己设为等待状态。此时CPU就处于空闲状态,所以为了提高CPU的利用率,可以让其他处理进程获取空闲的CPU资源继续执行压缩/解压,提高CPU的利用率,加快编解码速度。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值