资源简介
哈夫曼编码译码,数据结构课程设计,C++语言
代码片段和文件信息
#include
#include
#include
#include
#include
using namespace std;
#define MAX 100
typedef struct HTNode{
char c;
int weight;
int parentlchildrchild;
}HTNode*HuffmanTree;
typedef char **HuffmanCode;
HuffmanTree HT;
HuffmanCode HC;
void Select(HuffmanTree HTint iint &s1int &s2) //Select sub-function
//choose parent is 0 and weight is smallest node from forest
{ int jk=1; //s1 is the least of HT[].weight
while(HT[k].parent!=0) //s2 is the second least of HT[].weight
k++;
s1=k;
for(j=1;j<=i;++j)
if(HT[j].parent==0&&HT[j].weight s1=j;
k=1;
while((HT[k].parent!=0||k==s1))
k++;
s2=k;
for(j=1;j<=i;++j)
if(HT[j].parent==0&&HT[j].weight s2=j;
} //Select() end
void CreatHuffmanTree(HuffmanTree &HT int n)
//creat a huffmantree which is HT
{
int s1 s2;
if(n <= 1) return ;
int m = 2 * n - 1;
HT = new HTNode[m+1];
for(int i = 1;i <= m;++i) {
HT[i].parent = 0; HT[i].lchild = 0; HT[i].rchild = 0;
}
//read char and weight from a file
/*ifstream infile(“data.txt“ios::in);
if(!infile) {
cerr<<“open error!“< exit(1);
}
for(int i = 1;i <= n;i++){
infile >>HT[i].c>> HT[i].weight;
}
infile.close();*/
cout << “Enter the char and weight“< for(int i = 1;i <= n;i++){
cin >>HT[i].c>> HT[i].weight;
}
for(int i = n+1;i <= m;++i) {
Select(HTi-1s1s2);
HT[s1].parent = i; HT[s2].parent = i;
HT[i].lchild = s1; HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
}
void Savefile(HuffmanTree HTint n)
{
//save the weight into a file
ofstream outfile(“datas.txt“ios::out);
if(!outfile) {
cerr<<“open error!“< exit(1);
}
for(int i = 1;i <= n;i++){
outfile<< HT[i].weight<<“ “;
}
cout <<“the data of weight is already saved in files“< outfile.close();
}
void CreatHuffmanCode(HuffmanTree HT HuffmanCode &HC int n)
// Create the HuffmanCode
{
int startcf;
HC = new char*[n+1];
char *cd = new char[n];
cd[n-1] = ‘\0‘;
cout< for (int i = 1;i <= n;i++){
start = n-1;
c = i; f = HT[i].parent;
while (f != 0){
--start;
if(HT[f].lchild == c) cd[start] = ‘0‘;
else cd[start] = ‘1‘;
c = f; f = HT[f].parent;
}
HC[i] = new char[n - start];
strcpy(HC[i]&cd[start]);
cout << HT[i].c;
cout << “ ‘s Huffman code is: “< }
delete cd;
}
void EnCod- 上一篇:c++项目详细简单适合初学者
- 下一篇:英飞凌BMS解决方案C语言程序源码及使用说明
相关资源
- 通信过程中的RS编译码程序(c语言)
- 百度C++编码规范
- GBT 28169-2011 嵌入式软件 C语言编码规范
- 缩短循环码(2616)编译码程序-C语言
- pcm语音编码
- UNIX/LINUX下C语言中文短信UCS2编码和解
- 信息论课程设计——LZW编码
- c语言实现bch编码
- 个人总结的一些C/C++编码规范
- 谷歌C++编码规范 Google C++ 风格指南
- C语言实现LZW编码
- BCH编译码c语言实现
- C++实现的改进遗传算法
- c++ 数据结构 哈夫曼压缩&解压软件 控
- ldpc编译码c代码
- MFC莫尔斯电码的编码发声
- Google-C++编码规范中文版最新整理PDF版
- 哈夫曼压缩和解压c++源码
- spiht算法小波图像编码算法
- GBT28169-2011嵌入式软件C语言编码规范
- 嵌入式软件C语言编码规范(高安全性
- 文件压缩程序基于哈夫曼C++算法
- 密码编码学:加密方法的C与C++实现
- MMX-密码编码学:加密方法的C与C++实现
- MFC 一维码生成+打印 支持多种编码1
- Huffman和算术编码的C++实现
- 基于JPEG2000的图像编码与解码c++版
- 简单实现的自适应算术编码
- BCH编译码--C语言实现
- 谷歌C++编码规范-中文 2018-03-30 最新版
川公网安备 51152502000135号
评论
共有 条评论