您的位置:首页 > 其它

由Huffman编码引起的回忆,以及难以发现的bug.

2011-02-24 22:26 190 查看
之前看到cppblog一篇关于huffman的文,和我今早的一个梦不谋而合。我记得似乎曾经给前女友写过一个Huffman的课程大作业,花了当天晚上的一些时间,只是为了完成任务而写的,草草的回忆了一下huffman的原理,然后就开始写了,当时因为她的作业并没要求规模,我只把控制台输入端作为文件输入,先压缩再解压,并且把所有中间过程输出。

我知道自己有个弱点,当自己不把模块划分得很细,那么DEBUG的时间就会变得相对长一些,但是那一次写哈夫曼编码就完全是认为这种作业就不必划分得太细也能很容易实现。结果今早由于梦见一个诡异的梦(梦见自己得了绝症,她那弱不禁风的身影在梦里不断浮现...),又看到有人写了篇哈夫曼编码的随笔,我就重新打开那个我写的工程运行了一次。

嗯,运行正确,然后我找来一篇英文小说复制了一大片进去,结果bug出现了。接着是我不断找bug的过程(比如当时规模设置得太小,只是为了给她应付作业;再比如bit位批量处理类型等等细节...)

在搞定两个bug,最终还有一个诡异的bug没解决,I give up. 我知道这个错误之所以难以找到,是因为自己为了随意发挥违背自己历来遵从的编程原则,就算找到那个bug也无济于事,不如以后不再任性,把模块按down-top、top-down划分(这种方法几乎能让我不出任何错),并且写代码的时候,为每一次项目的测试单元和调试代码做准备。

当时自己任性写的代码如下(注释是很全面,因为当时要给她去交作业,但是却发现那些注释只是给她老师看的,而非给我自己看的....因为我没有在注释里面详细定义哈夫曼编码压缩后的压缩文件格式信息):

(以下代码的input不能直接处理Unicode,写成重定向到文件可以)

嗯嗯,我还把自己这段代码复制到百度知道回答别人的问题过~~真是贻害四方瓦。

1#include <iostream>
2
3using namespace std;
4
5#define MAX_FILE 50000//假设的文件最大长度
6#define MAXLIST 256//最大MAP值
7#define MAX_HUFFMAN_LENGTH 100//哈夫曼编码长度
8unsigned char fileContent[MAX_FILE];//处理的字符串大小
10//编码到假设的文件的哈夫曼压缩格式: 依次存储 原字符串长度(1字节存储:可扩展到2字节)、哈夫曼编码数(1字节)、每个哈夫曼编码的长度序列、每个哈夫曼编码对应的字符序列、编码过的哈夫曼字符串
15
16
18void ShellSort(unsigned char pData[MAXLIST][2],int Count)//Shell排序,用于准备有序化要构造的编码权值构造哈夫曼树做准备
19
51
52struct TNode//哈夫曼树结点
53
69struct LNode//链表结点,用于存储哈夫曼树结点,进而构造哈夫曼树(保证每一步链表结点包含的哈夫曼结点都是有序的)
70
81int len=0;//哈夫曼编码数
82int deep=-1;//深度
83void Preorder(TNode * p);//前序遍历
84void byLeft(TNode*p)//经由左结点
85void byRight(TNode*p)//经由右结点
94void Preorder(TNode * p)

unsigned char generateOne(int k)//产生k个连续1的二进制串,比如111,1111,111111,用于编码进假设的文件

int compareBits(unsigned char b1,unsigned char b2,unsigned char c,int l,int d)//判断由 [b1,b2] 组成的16位二进制数以d为起点,是否是长度为l的c二进制串(哈夫曼编码)的前缀
void input(unsigned char* _list,unsigned char end)

int main()
请输入要压缩的字符串:I ask the indulgence of the children who may read this boo
2k for dedicating it to a grown-up. I have a serious reason: he is the best frie
3nd I have in the world. I have another reason: this grown-up understands everyth
4ing
5even books about children. I have a third reason: he lives in France where he is
6 hungry and cold. He needs cheering up.$
7哈夫曼编码个数:30
8哈夫曼编码序列:
90000:t
00010000:F
00010001:H
00010010:<回车>
00010011:m
000101:b
00011:u
0010:s
0011:o
0100:i
0101:a
011:e
100000:I
100001:.
1000100:-
1000101::
100011:w
1001:r
1010:h
1011:n
1100000:f
1100001:y
1100010:k
1100011:p
110010:l
110011:g
110100:c
110101:v
11011:d
111:
I ask the indulgence of the children who may read this book for dedicating it to
a grown-up. I have a serious reason: he is the best friend I have in the world
. I have another reason: this grown-up understands everything
even books about children. I have a third reason: he lives in France where he is
hungry and cold. He needs cheering up.哈夫曼压缩后二进制序列:
10000011 10101001 01100010 11100001 01001111 10100101 11101100 01111001 01100110
11101111 01000111 11001111 00000111 00001010 01111111 01001010 01001100 1011011
1 00101110 11111100 01110100 01111100 01001101 01110000 11111001 01101011 101111
10 00010100 10000101 11000101 00110011 11000101 11110000 00011100 11111101 10111
101 10100110 10001010 00001001 01111001 11110100 00001110 00000111 11010111 1110
0111 00100111 00011101 11000100 00011110 00111000 01111111 10000011 11010010 111
01010 11111010 11110010 01110010 10000110 00110010 11110010 11010100 10001110 11
100010 11111010 01111101 00001011 10000101 00111110 00101011 00100000 11111000 0
0100101 00011101 11101111 11000001 11101001 01110101 01111101 00101111 10000101
00111111 00011001 11001110 01011011 10000111 11000001 11101001 01110101 01111101
01101100 11000010 10011100 11111001 01101010 01000111 01110001 01111000 0101001
0 00010111 11001110 01001110 00111011 10001000 00111100 01111100 01110111 101101
11 00100100 00001011 01111011 00101110 11110101 01110011 10000100 00101001 00101
111 00110001 00100111 10101011 10111110 00101001 10011110 00100010 11101010 0010
1001 10001100 00111110 10010100 10011001 01101110 01011101 11000011 11100000 111
10100 10111010 10111110 10111100 00101001 00100111 01111110 01011010 10010001 11
011100 01011111 01001111 11100100 10011010 10110010 11101001 01111100 01000010 0
1010110 11110100 01111110 00111010 01110010 11111101 00111110 10000101 11101000
01110111 10011100 11100001 11101011 01111011 11111010 00011110 01011011 10000111
10001000 10111111 01101101 11101100 10111110 10010100 11011100 10100101 1110011
1 11000111 10001110 0001
哈夫曼压缩打包假定文件格式二进制的文本体现:



(此处代码被截断,无法copy到剪贴板)

原字节数为:341
压缩后字节数为:181
压缩率为53.2258%
字符串字节数为:341
字符串解压序列为:I ask the indulgence of the children who may read this book for
dedicating it to a grown-up. I have a serious reason: he is the best friend I
have in the world. I have another reason: this grown-up understands everything
even books about children. I have a third reason: he lives in France where he is
hungry and cold. He needs cheering up.

写到此处,我突然发现在进行位处理的时候,我并没有用一个更简化的方法(100行内可以解决)- -bnr ,好吧 下次写一个通用的压缩软件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐