哈夫曼编码器

//加入了文件读写操作,有部分数据成员没有放到结构体里,可以改一改

#include<iostream>
#include <string>
#include <cstring>
#include<fstream>
using namespace std;
struct HfmTreeNode
{
int weight;
int parent, lchild, rchild;
};
bool readFileToString(string file_name, string& fileData);
class Hfm
{
private:
char **hfmCode;
HfmTreeNode* hfmTree;
char* zf;
int num;
public:
Hfm()
{
hfmCode = NULL;
hfmTree = NULL;
zf = NULL;
num = 0;
}


Hfm(int n, int *weight, char *z)
{
int l;
zf = new char(n);
ofstream out("hfmTree.txt", ios::trunc);
for (l = 0; l<n-1; l++) {
zf[l] = z[l];
out << z[l] << endl;
out << weight[l] << endl;
cout << z[l] << endl;
cout << weight[l] << endl;
}
zf[l] = z[l];
out << z[l] << endl;
out << weight[l];
out.close();
num = n;
if (n > 1)
{
int m = 2 * n-1 ;
hfmTree = new HfmTreeNode[m];
for (int i = 0; i < m; i++)
{
hfmTree[i].parent = -1;
hfmTree[i].lchild = -1;
hfmTree[i].rchild = -1;
}
for (int i = 0; i < n; i++)
{
hfmTree[i].weight = weight[i];
}
for (int i = n ; i < m; ++i)
{
int s1, s2;
select(i - 1, s1, s2);
hfmTree[s1].parent = i;
hfmTree[s2].parent = i;
hfmTree[i].lchild = s1;
hfmTree[i].rchild = s2;
hfmTree[i].weight = hfmTree[s1].weight + hfmTree[s2].weight;
}
}
createHuffmanCode();
cout << "初始化完成" << endl;
for (int i = 0; i <n; i++)
cout << zf[i] << " : " << hfmCode[i] << endl;
}
~Hfm()
{
delete[] hfmCode;
delete[] hfmTree;
delete[] zf;
}




void select(int m, int &s1, int &s2);
void createHuffmanCode();
int bianMa(string y, string &r);
void yiMa();
void reLoad();
void printHfmTree();
void printHelp(HfmTreeNode h, string ss);
};
void Hfm::printHfmTree() {
/* cout << "序号\t权值\t双亲\t左\t右" << endl;
for (int i = 0; i < 2 * num - 1; i++)
{
cout << i << "\t" << hfmTree[i].weight << "\t" << hfmTree[i].parent << "\t" << hfmTree[i].lchild << "\t" << hfmTree[i].rchild << endl;
}*/
ofstream out("TreePrint.txt", ios::trunc);
out.close();
string ss = "";
printHelp(hfmTree[2*num-2], ss);
cout << "打印哈弗曼树完成" << endl;
}
void Hfm::printHelp(HfmTreeNode h, string ss)
{
ofstream out("TreePrint.txt", ios::app);
if (hfmTree == NULL)
return;
ss += "   ";
if (h.rchild != -1) {
printHelp(hfmTree[h.rchild], ss);
}

cout << ss << h.weight << endl;
out << ss << h.weight << endl;
if (h.lchild != -1) {
printHelp(hfmTree[h.lchild], ss);
}


}
void Hfm::yiMa()
{
string r, miMa;
readFileToString("CodeFile.txt", miMa);
r.clear();
int i=0;
int len = miMa.length();
int root;


root = 2 * num - 1;
int p = root-1;
while (true)
{
if (hfmTree[p].lchild == -1 && hfmTree[p].rchild == -1)
{
r.append(1, zf[p]);
p = root-1;
}
if (i >= len)
break;
if (miMa[i++] == '0')
p = hfmTree[p].lchild;
else
p = hfmTree[p].rchild;
}
ofstream out("TextFile.txt");
out << r;
out.close();
cout << "译码完成" << endl;
}


int Hfm::bianMa(string y, string &r) {
r.clear();
int len = y.length();
bool flag;
for (int i = 0; i < len; i++)
{
flag = false;
int j;
for (j = 0; j<num; j++)
if (zf[j] == y[i])
{
flag = true;
break;
}
if (!flag)
{
cout << "检测到无法编码\""<<y[i]<<"\"的元素,请检查" << endl;
return 0;
}
r.append(hfmCode[j]);
}
ofstream out("CodeFile.txt");
out << r;
out.close();
cout << "编码完成" << endl;
return 1;


}
void Hfm::createHuffmanCode() {
int n = num;
hfmCode = new char*[n + 1];
char *cd;
cd = new char[n];
cd[n - 1] = '\0';
for (int i = 0; i < n; i++)
{
int start = n - 1;
int c = i;
int f = hfmTree[i].parent;
while (f != -1)
{
--start;
if (hfmTree[f].lchild == c)
cd[start] = '0';
else
cd[start] = '1';
c = f;
f = hfmTree[f].parent;
}
hfmCode[i] = new char[n - start];
strcpy(hfmCode[i], &cd[start]);
}
delete cd;
}
void Hfm::reLoad() {
ifstream in("hfmTree.txt");
char buffer[25], z[100];
int iWeight[100], n = 0;
if (in.is_open())
{
while (!in.eof())
{
in.getline(buffer, 10);
z[n] = buffer[0]; 
in.getline(buffer, 100);
iWeight[n] = (int)(buffer[0] - '0'); 
n++;
}
num = n;
zf = new char(n);
for (int i = 0; i < n; i++) {
zf[i] = z[i];
}
in.close();
if (n > 1)
{
int m = 2 * n-1 ;
hfmTree = new HfmTreeNode[m];
for (int i = 0; i < m; i++)
{
hfmTree[i].parent = -1;
hfmTree[i].lchild = -1;
hfmTree[i].rchild = -1;
}
for (int i = 0; i < n; i++)
{
hfmTree[i].weight = iWeight[i];
}
for (int i = n ; i < m; ++i)
{
int s1, s2;
select(i , s1, s2);
hfmTree[s1].parent = i;
hfmTree[s2].parent = i;
hfmTree[i].lchild = s1;
hfmTree[i].rchild = s2;
hfmTree[i].weight = hfmTree[s1].weight + hfmTree[s2].weight;
}
}
createHuffmanCode();
cout << "内存中不存在哈弗曼树,已从历史记录中加载" << endl;
for (int i = 0; i <n; i++)
cout << zf[i] << " : " << hfmCode[i] << endl;
}
else {
cout << "无历史初始化记录,请重新初始化" << endl;
}
in.close();
}
void Hfm::select(int m, int &s1, int &s2) {
s1 = -1;
s2 = -1;
for (int i = 0; i < m; i++)
{
if (hfmTree[i].parent == -1)
{
if (s1 == -1)
{
s1 = i;
}
else if (s2 == -1)
{
s2 = i;
if (hfmTree[s1].weight> hfmTree[s2].weight)
{
int p = s1;
s1 = s2;
s2 = p;
}
}
else
{
if (hfmTree[i].weight< hfmTree[s2].weight)
{
s2 = i;
if (hfmTree[i].weight< hfmTree[s1].weight)
{
s2 = s1;
s1 = i;
}
}
}
}
}




}
bool readFileToString(string file_name, string& fileData)
{
ifstream file(file_name.c_str(), std::ifstream::binary);


if (file)
{
// Calculate the file's size, and allocate a buffer of that size.
file.seekg(0, file.end);
const int file_size = file.tellg();
char* file_buf = new char[file_size + 1];
//make sure the end tag \0 of string.


memset(file_buf, 0, file_size + 1);


// Read the entire file into the buffer.
file.seekg(0, ios::beg);
file.read(file_buf, file_size);




if (file)
{
fileData.append(file_buf);
}
else
{
std::cout << "error: only " << file.gcount() << " could be read";
fileData.append(file_buf);
return false;
}
file.close();
delete[]file_buf;
}
else
{
return false;
}
return true;
}
void printCode(string s) {
ofstream out("CodePrin.txt",ios::trunc);
out << s;
out.close();
}
int main() {
Hfm* hfm = NULL;
int n;
char *z = NULL;
int *weight = NULL;
char ch = 'y';
int b;
string s, r;
while (ch == 'y')
{
cout << endl << "1、初始化" << endl;
cout << "2、编码" << endl;
cout << "3、译码" << endl;
cout << "4、打印代码文件" << endl;
cout << "5、印哈夫曼树" << endl;
cout << "0、退出" << endl;
cout << "请输入:";
cin >> b;
switch (b) {
case 1:
cout << "请输入叶结点个数:";
cin >> n;
z = new char[n ];
weight = new int[n ];
cout << "请输入 " << n << " 个字符和其对应的权值:" << endl;
for (int i = 0; i < n; i++) {
cin >> z[i] >> weight[i];
}
delete hfm;
hfm = new Hfm(n, weight, z);
delete[] z;
delete[] weight;
cout << "构造哈夫曼树完成" << endl;
break;
case 2:
if (hfm != NULL) {
readFileToString("ToBeTran.txt", s);
hfm->bianMa(s, r);
s.clear();
}
else {
hfm = new Hfm();
hfm->reLoad();
}
break;
case 3:
hfm->yiMa();
break;
case 4:
r.clear();
readFileToString("CodeFile.txt",r);
cout << r << endl;
printCode(r);
break;
case 5:
hfm->printHfmTree();
break;
case 0:
ch = 'n';
break;
default:
cout << "输入错误,请重新输入" << endl;
break;
}

}
return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值