我是
靠谱客的博主
优美香水,最近开发中收集的这篇文章主要介绍
课程设计之霍夫曼编码,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
#include<iostream>
#include<stack>
#include<fstream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
class Huffman
{
private:
typedef struct huffmannode
{
int weight;
int parent;
int lchild;
int rchild;
char data;
huffmannode();
}HTNode,*HuffmanTree;
typedef char **HuffmanCode;
HuffmanTree HT;
HuffmanCode HC;
int n;
public:
Huffman();
void print();
void initialization();
void encoding();
void decoding();
void CreatTree();
void recursion(int f,string s,int cot);
void DFS(huffmannode T,int x,int y);
};
int main()
{
Huffman HT;
while(1)
{
cout<<"_____________________"<<endl;
cout<<"哈夫曼编码、译码系统"<<endl;
cout<<"1:初始化"<<endl;
cout<<"2:编码"<<endl;
cout<<"3:译码"<<endl;
cout<<"4:打印"<<endl;
cout<<"5:退出"<<endl;
cout<<"请输入你的选择t";
int argument;
cin>>argument;
switch(argument)
{
case 1:
HT.initialization();
break;
case 2:
HT.encoding();
break;
case 3:
HT.decoding();
break;
case 4:
HT.print();
break;
default:
exit(0);
}
}
return 0;
}
Huffman::huffmannode::huffmannode()
{
lchild=rchild=parent=-1;
}
Huffman::Huffman()
{
}
void Huffman::CreatTree()
{
ofstream ofile("hfmTree.txt");
if(ofile.is_open())
{
int i,j,k;
HC=(HuffmanCode)malloc(n*sizeof(char*));
char* cd=(char *)malloc(n*sizeof(char));
cd[n-1]='