实验要求:
一个基本的系统应具有以下功能:
(1)I:初始化(Initialization)。从终端读入字符集大小n,以及n个字符和n个权值,建立哈夫曼树,并将它存于文件hfmTree中。
(2)E:编码(Encoding)。利用以建好的哈夫曼树(如不在内存,则从文件hfmTree中读入),对文件ToBeTran中的正文进行编码,然后将结果存入文件CodeFile中。
(3)D:译码(Decoding)。利用已建好的哈夫曼树将文件CodeFile中的代码进行译码,结果存入文件TextFile中。
(4)V:打印代码文件(Print)。将文件CodeFile以紧凑格式显示在终端上,每行50个代码。同时将此字符形式的编码文件写入文件CodePrin中。
可选的部分:
( 1 )初始化I阶段:对于任意txt文件,统计其中出现的字符集以及各字符的出现频率
( 2 )编码E阶段:生成文件CodeFile应该转换为二进制文件 //未实现
( 3 )如果原始的输入是一个任意的txt文件,则需要添加一个验证V阶段,验证源文件和译码文件是否相同。
代码如下:
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef struct{
int weigth;
int parent,lchrld,rchrld;
}HTNode;
typedef struct{
HTNode *node;
int n,m;
int r;
}HuffmanTree;
typedef struct{
int num; //出现次数
char ch; //字符
}ElemType;
typedef struct{
ElemType data[MAXSIZE];
int length;
}Number;
typedef struct{
char data;
string code;
}ElemType_2;
typedef struct{
ElemType_2 *chars;
int length;
}Sqlist;
HTNode* InitHTNode(int n