文档介绍:《数据结构与算法》实验报告
需求分析
问题描述:
利用哈夫曼编码进行通信可以大大提高信道利用率,缩短信息传输
时间,降低传输成本。但是,这要求在发送端通过一个编码系统对
待传数据预先编码,在接收端将传来的数据进行译码(复原 ) 。
ofstream outstuf;
typedef struct{
unsigned int weight;
unsigned int parent,lchild,rchild;
} HTNode,*HuffmanTree;
typedef char** HuffmanCode;
HuffmanTree HT=NULL;
int n=0;
HuffmanCode HC=NULL;
char *ch=NULL;
void initHuffmanTree();
int openfileInit();
int inputInit();
int HuffmanCoding(int *w);
void Select(int j,int &s1,int &s2);
void encoding();
void openfileEnco();
void inputEnco();
void decode();
void openfileDeco();
void inputDeco();
void dispHT( HuffmanTree nodeRoot, int level );
void initHuffmanTree(){ // 选择初始化哈夫曼树
int sel=0; for(;;){
cout<<"\t*
********************************************************
"<<endl;
cout<<"\t* "<<" 字符集及权值来源\t\t\t\t\t*"<<endl;
cout<<"\t*\t"<<"1. 进行编码 \t\t\t*"<<endl;
cout<<"\t*\t"<<"2. 自行输入字符集及权值 \t\t\t\t*"<<endl;
cout<<"\t*\t"<<"3. 返回上层 \t\t\t\t\t*"<<endl;
,********************************************************1
cout<〈”请输入您的选择"«endl«" cin»sel;
if(sel==3) break;
switch(sel)
{case 1 :openfilelnit();break;
case 2:inputlnit();break;
default:cout«"对不起,您输入的数据有误!请重新输入。 "<<endl;
)
int openfilelnit(){〃
int *w=(int*)malloc(28*sizeof(int));
ch=(char*)malloc(28*sizeof(char));
n=27;
ifstream infile("",ios::in);
if(!infile){
cerr«"open error!"«endl; exit(1);
cout«"权值文件中的信息(#代表空格)"«endl«endl;
for(int i=1 ;()==0;i++){
infile»ch[i];
infile»w[i]; )
cout«endl;
();
cout«"字符
for(i=1 ;i<10;i++) cout«ch[i]«'\t';
cout«"权值
for(i=1 ;i<10;i++) cout«w[i]«'\t';
cout«"字符
for(i=10;i<19;i++) cout«ch[i]«'\t';
cout«"权值
for(i=10;i<19;i++) cout«w[i]«'\t';
cout«"字符
for(i=19;i<28;i++) cout«ch[i]«'\t';
cout«"权值
for(i=19;i<28;i++) cout«w[i]«'\t';
cout«endl;
n=27;
HuffmanCoding(w);
cout<<" 各字符编码如下: "<<endl;
for(i=1;i<=27;i++) {
cout<<""<<ch[i]<<'\t';
cout<<HC[i]<<endl;
};
("",io