问题描述:.在一个加密应用中,要处理的信息来自下面的字符集,各个字符的相关使用频度如下:
字符空格 A B C D E F G H I J K L M
频度 180 64 13 23 32103 22 15 47 57 1 5 31 20
字符 N O P Q R S T U V W X Y Z
频度 55 63 15 1 48 56 80 25 7 18 2 16 1
现请编写程序你实现如下功能:
(1)运行时,由用户输入来初始化字符集大小和相应用字符。
(2)输入一个要加密的字符串,将其加密。
(3)输出解密字符串。
问题分析:
在这个信息时代里,人们每天都会面对不一样的信息,对人们的影响越来越大,于是利用哈夫曼编码可以提高信息的保密程度,增加信息的利用程度,缩短信息传输时间,降低传输成本。
结构树:
.
哈夫曼树的结构体:权值(weigth)、左孩子(lchild)、右孩子(rchild)和双亲(parent)由hftree[maxnode+1]数组来定义;
编码结构体:
bit 为一维数组,用来保存字符的哈夫曼编码,
start 表示该编码在数组 bit 中的开始位置;
源代码:
#ifndefHuffmantree_H
#defineHuffmantree_H
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<stdlib.h>
Usingnamespace std;
Constmaxleaf=30;
Constmaxnode=maxleaf*2-1;
Typedefstruct
{
Charch;
Intweight;
Intparent;
Intlchild;
Intrchild;
}
HNodeType;
Typedfstruct
{
Intgbit [maxleaf+1];
Intstart;
}
Codetype;
ClassHuffmantree{
Public;
Huffmantree(){}
~Huffmantree(){}
Viodcreathuffmantree(int n);
voidhuffcodd (int n);
voidtrancode(int n);
voidprinttree(int n);
HNodeTypehftree[maxnode+1];
CodetypeHuffcode[maxleaf];
Private;
INTN;
};
#endif
#include<iostream>
Usingnamespace std;
#include”2.h”
VoidHuffmantree::creathuffmantree(int n)
{
IntI ,j,*1,*2,m1,m2,c,p;
Codeypecd;
For(i=0,i<maxleaf;i++)
{
Hftree[i].parent=-1;
Hftree[i].lchild=-1;
Hftree[i].rchild=-1;
Hftree[i].weight=-1;
}
Cout<<”pleaseprint 字符&权值:\n”<<endl;
For(i=0;i<n;i++)
{
Cout<<”print”<<i+1<<”字符&权值:
Cin>>hftree[i].ch;
Cin>>hftree[i].weight;
}
//
For(i=0;j<n=I;j++)
{
If(hftree[j].parent==-1&&hftree[j].weight<m1)
{
M2=m1;
X2=x1;
Mi=hftree[j].weight;
X1=j;
}
Else
If(hftree[j].parent==-1&hftree[j].weight<m2)
{
m2=hftree[j].weight;
X2=j;
}
}
Hftree[x1].parent=n+I;
Hftree[x2].parent=n+I;
Hftree[n+i].weight=hftree[x1].weight+hftree[x2].weight;
Hftree[n+i].lchild=x1;
Hftree[n+i].rchild=x2;
}
For(i=0;i<n;i++)
{
cd.start=n-1;
c=i;
p=hftree[c].parent;
while(p!=-1)
{
If(hftree[p].lchild==c)
cd.bit[cd.start]=0;
elsecd.bit[cd.start]=1;
cd.start--;
c=p;
p=hftree[c].parent;
}
For(j=cd.start+1;j<n;j++)
Huffcode[i].bit[j]=cd.bit[j];
Huffcode[i].start=cd.start;
}
For(i=0;i<n;i++)
{
Cout<<hftree[i].ch<<”的编码是:”
For(j=Huffcode[i].strat+1;j<n;j++)
Cout<<Huffcode[i].bit[j];
Cout<<endl;
}
}
void Huffmantree::huffcode(int n) //编码
{
ofstream file1("d:\\hftreecode.txt",ios::out);
char ch;
int q=0;
int j;
cout<<"请输入要编码的字符串(按@)结束"<<endl;
cin>>ch; //对字符串进行编码
while(q<n&&ch!='#')
{
while(hftree[q].ch!=ch)
q++;
for(j=Huffcode[q].start+1;j<n;j++)
{
cout<<Huffcode[q].bit[j];
file1<<Huffcode[q].bit[j];
}
q=0;
cin>>ch;
}
file1.close();
cout<<endl;
}
void Huffmantree::trancode(int n)//译码
{
char c;
int m1=0,m2=0;
ifstream file1("d:\\hftreecode.txt",ios::in);
while(file1.get(c)!='\0')
m1++;
file1.close();
if(m1==0)
{
cout<<"请先编码!!"<<endl;
return;
}
ifstream file2("d:\\hftreecode.txt",ios::in);//文件以输入方式打开(文件数据输入到内存)
char *cc=new char[m1+1];
while(file2.get(c)!='\0')
{
cc[m2]=c;
m2++;
}
cc[m2]='\0';
int i,j=0;
i=2*n-2;
while(cc[j]!='\0')
{
if(cc[j]=='0')
i=hftree[i].lchild;
else
i=hftree[i].rchild;
if(hftree[i].lchild==-1)
{
cout<<hftree[i].ch<<endl;
i=2*n-2;
}
j++;
}
file2.close();
}
void Huffmantree::printtree(int n)//打印哈夫曼树
{
int i;
ofstream file("e:\\printhuffman.txt",ios::out);
cout<<"字符"<<setw(8)<<"权值"<<setw(8)<<"左孩子"<<setw(8)<<"右孩子"<<setw(8)<<"双亲"<<endl;
file<<"字符"<<setw(8)<<"权值"<<setw(8)<<"左孩子"<<setw(8)<<"右孩子"<<setw(8)<<"双亲"<<endl;
for(i=0;i<2*n-1;i++)
{
cout<<hftree[i].ch<<setw(8)<<hftree[i].weight<<setw(8)<<hftree[i].lchild<<setw(8)<<hftree[i].rchild<<setw(8)<<hftree[i].parent<<endl;
file<<hftree[i].ch<<setw(8)<<hftree[i].weight<<setw(8)<<hftree[i].lchild<<setw(8)<<hftree[i].rchild<<setw(8)<<hftree[i].parent<<endl;
}
cout<<endl;
file.close();
} #include<iostreamusing namespace std#include "2.void Huffmantree::creathuffmantree(in{int i,j,x1,x2,m1,m2,c, codetyp for(i=0;i<maxleaf;i++) //对每个叶子结点进 {
#include<iostream>
Usingnamespace std;
#include”2.h”
Int main()
{
Int n,choice;
Huffmantree ht;
While(1)
{
System(“cls”);
cout<<"\t*******************★哈夫曼树的编码和译码★***************** \n";
cout<<"\t*******************请选择: **********************************\n";
cout<<"\t***********************1-----创建哈夫曼树********************\n";
cout<<"\t***********************2-----编码****************************\n";
cout<<"\t***********************3-----译码****************************\n";
cout<<"\t***********************4-----打印哈夫曼树********************\n";
cout<<"\t***********************0-----退出****************************\n";
cout<<”\t你要输入的编号(0--4):“;
cin>>choice;
if(choice==0)cout<<”退出本系统”<《endl;break;
switch(choice)
{
Case1:
{ hftree[i].parent=-1; hftree[i].lchild=-1; hftree[i].rchi hftree[i].weight=-1 cout<<"请输入字符和权值:\n"<<endl for(i=0;i<n;i++) //从键盘输入字符和权值 cout<<"输入第"<<i+1<<"个字符和权值: cin>>hftree[i].ch; cin>>hftree[i].weight; } //构建哈夫曼树并对其进行编码和输出 for(i=0;i<n-1;i++) //构造哈夫曼 { m1=m2=10000 x1=x2=0;
Cout<<“输入叶子结点数n:”;
Cin>>n;
Cout<<””””<<endl;
Ht.creathuffmantree(n);
}
System(“pause”);
Break;
Case2:ht.huffcode(n);
System(“pause”);
Break;
Case3:ht,trancode(n);
System(“pause”);
Break;
Case4:ht.printtree(n);
System(“pause”);
Break;
Default:
Cout<<“没有此选项,请重新选择!”<<endl;
}
}
Return 0;
}