实验内容:
- 输入HuffmanTree的参考标准底稿:输入一段字符串,作为发送方和接收方进行编码的统一标准,统计出现的字符种类数和出现的频度。
2)初始化HuffmanTree参数:给定报文中26个字母a-z及空格的出现频率{64, 13, 22, 32, 103, 21, 15, 47, 57, 1, 5, 32, 20, 57, 63, 15, 1, 48, 51, 80, 23, 8, 18, 1, 16, 1, 168}。
3)创建HuffmanTree和编码表:根据1)或2)的输入,构建赫夫曼树并为这些字符编制赫夫曼编码,并输出。
5)模拟发送端,从键盘输入字符串,在屏幕上输出输入串的编码
6)模拟接收端,从键盘上输入0-1赫夫曼编码串,翻译出对应的原文。
实验步骤:
(1)按照实验要求编写代码,构造赫夫曼树及其编码,体会网络发送端和接收端编码和译码过程及其工作原理。
(2)输入模拟发送端和模拟接受端的验收用例,验证其输出结果。
#include <iostream>
#include <string.h>
using namespace std;
typedef struct{
int weight;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char **HuffmanCode;
int n=27,s1,s2,m,i,j,count2;
void Select(HuffmanTree &HT,int end , int &s1,int &s2){
int min1 =9999;
int min2 = 9999;
for(int i=1;i<=end;i++){
if(HT[i].parent==0 && HT[i].weight<min1){
min1 = HT[i].weight;
s1 = i;
}
}
for(int i=1;i<=end;i++){
if(HT[i].parent == 0&&HT[i].weight<min2 &&s1!=i){
min2 = HT[i].weight;
s2 = i;
}
}
}
void CreatHuffmanTree(HuffmanTree &HT,int n){
m = n*2-1;
HT = new HTNode[m+1];
for(i=1;i<=m;i++){
HT[i].parent = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
}
for(i=1;i<=n;i++){
cin >> HT[i].weight;
}
for(i = n+1;i<=m;i++){
Select(HT,i-1,s1,s2);
HT[i].weight = HT[s1].weight+HT[s2].weight;
HT[s1].parent = i;
HT[s2].parent = i;
HT[i].lchild = s1;
HT[i].rchild = s2;
}
}
void creatHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int n){
int start,c,f;
HC = new char*[n+1];
char *cd = new char[n];
cd[n-1] ='\0';
for(int i=1;i<=n;i++){
start = n-1;
c = i;
f = HT[c].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]);
}
delete cd;
}
void TransCode(HuffmanTree HT,char a[],char ch[],char b[],int n){
count2 = 0;
int q = 2*n-1;
int k=0;
for(int i=0;a[i]!='%';i++){
if(a[i]=='0'){
q = HT[q].lchild;
}else if(a[i]=='1'){
q = HT[q].rchild;
}
if(HT[q].lchild == 0 &&HT[q].rchild==0){
b[k++] = ch[q-1];
q = 2*n-1;
}
}
count2=k;
b[k]='\0';
}
int main()
{
HuffmanTree HT;
HuffmanCode HC;
int count1=0;
int code;
char a[1000];
char b[1000];
char c[1000];
char ch[27]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '};
cout<<"1.输入HuffmanTree的参数"<<endl;
cout<<"2.初始化HuffmanTree的参数(含有26字母及空格)"<<endl;
cout<<"3.创建HuffmanTree和编码表"<<endl;
cout<<"4.输出编码表"<<endl;
cout<<"5.输入编码,并翻译为字符"<<endl;
cout<<"6.输入字符,并实现转码"<<endl;
cout<<"7.退出."<<endl;
while(1){
cout<<"请输入操作码:"<<endl;
cin >>code;
if(code == 1){
cout<<"操作完成"<<endl;
}
else if(code == 2){
cout<<"请输入权值数组:";
CreatHuffmanTree(HT,n);
cout<<"操作完成"<<endl;
}
else if(code == 3){
creatHuffmanCode(HT,HC,n);
cout<<"操作完成"<<endl;
}
else if(code == 4){
cout<<"字符"<<"\t"<<"权值"<<"\t"<<"编码"<<endl;
for(int i=1;i<=n;i++){
cout<<ch[i-1]<<"\t"<<HT[i].weight<<"\t"<<HC[i]<<endl;
}
cout<<"操作完成"<<endl;
}
else if(code == 5){
cout<<"输入二进制编码(以%结束):";
for(i=0;i<1000;i++){
cin >>a[i];
char data = a[i];
if(a[i]=='%'){
break;
}
}
TransCode(HT,a,ch,b,n);
cout<<"输出:";
for(i = 0;i<count2;i++){
cout<<b[i];
}
cout<<"\n操作完成"<<endl;
}
else if(code ==6){
count1 = 0;
cout<<"请输入一串字符(以%结束):";
for( i=0;i<1000;i++){
cin >>c[i];
char data = c[i];
count1++;
if(c[i]=='%'){
break;
}
}
cout<<"输出:";
for(i=0;i<count1-1;i++){
for(j=0;j<n;j++){
if(c[i]==ch[j]){
cout<<HC[j+1];
break;
}
}
}
cout<<"\n操作完成"<<endl;
}
else if(code == 7){
break;
}
}
return 0;
}