哈夫曼编码译码器(附源码)详解.doc
文本预览下载声明
建立Huffman树进行编码和译码对于给定的一篇英文文章,统计字符出现的概率,并根据概率建立Huffman树,利用Huffman编码对文章进行编码和译码。
程序数据要求及功能实现
主界面
1.读取文件并对字符进行编码
2.哈夫曼编码信息
3.文件编码
显示文件编码
保存文件编码
4.文件译码
显示文章编码的译码
保存文章编码的译码
文件压缩
文件解压缩
附:程序源代码
/*
* File: HUFFMANFUNCTION.h
* Author: Administrator
*
* Created on 2011年12月19日, 下午6:19
*/
#ifndef HUFFMANFUNCTION_H
#define HUFFMANFUNCTION_H
#include cstdlib
#includeiostream
#includefstream
#includemath.h
#define max1 150
#define max2 50
#define max3 256
using namespace std;
class Htnote {
public:
char name; //字符名
double weight; //权重
int lchild; //左孩子
int rchild; //右孩子
int parent; //父亲
Htnote() {
weight = 0;
lchild = -1;
parent = -1;
rchild = -1;
}
};
class Name {
public:
int num; //字符出现的次数
char pname; //字符名
double lweight; //权值
Name() {
num = 0;
lweight = 0;
}
};
class GetName {
public:
char namef[max2];
int n; //字符的种类
int sum; //字符的总数
Name letter[max1]; //存储字符信息的类的数组
GetName() {
sum = 0;
n = 0;
}
void GetWeight()//得到字符的权值
{
for (int i = 0; i n; i++) {
letter[i].lweight = (double) letter[i].num / sum; //出现的次数除总数得到权值
}
}
int ReadLetter() {
ifstream input;
cout 请输入文件名: endl;
cin namef;
input.open(namef); //打开文件
if (input.fail()) {
cout 该文件不存在! endl;
return 0;
}
char ch;
ch = input.get();
letter[0].pname = ch;
letter[0].num++;
sum++;
while (!input.eof()) {//读取文件中的所有字符
int tag = 0;
ch = input.get();
for (int i = 0; i n + 1; i++) {
if (letter[i].pname == ch) {
letter[i].num++;
sum++;
tag = 1;
}
}
if (tag == 0) {
n++;
letter[n].pname = ch;
le
显示全部