怎么利用C++实现赫夫曼编码译码器

本篇内容主要讲解“怎么利用C++实现赫夫曼编码译码器”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么利用C++实现赫夫曼编码译码器”吧!

首先,我们来看看main函数:

void main()  {          char choice=' ';          while(choice!='q')          {       cout<<"/n******************************"<                cout<<" 欢迎使用赫夫曼编码译码系统"<                cout<<"******************************"<                cout<<"(1)要初始化赫夫曼链表请输入'i'"<                cout<<"(2)输入要编码的字符'w'"<                cout<<"(3)要编码请输入'e'"<                cout<<"(4)要译码请输入'd'"<                cout<<"(5)要打印编码请输入'p'"<                cout<<"(6)要打印赫夫曼树请输入't'"<                cout<<"(7)要离开请输入'q'"<                if(flag==0)cout<<"/n请先初始化赫夫曼链表,输入'i'"<                cin>>choice;                   switch(choice)                   {                   case 'i':                                  Initialization();                                  break;                   case 'w':                                  InputCode();                                  break;                   case 'e':                                  Encoding();                                  break;                   case 'd':                                  Decoding();                                  break;                   case 'p':                                  Code_printing();                                  break;                   case 't':                                      Tree_printing(HT,2*n-1);                                      break;                   case 'q':                                  break;                   default:                                  cout<<"input error"<                 }            }         free(z);         free(w);         free(HT);  }

这个函数主要就是和用户交互使用。这个过程我用switch case 来实现,你们也可以选择其他的方式实现如if else ,这个就个人爱好选择。下面我们来看看初始化赫夫曼树的函数Initialization()。
<n<<"个字符(字符型)/n注意:必须以回车结束:"<<endl;
<i+1<<"个字符:"<<endl;
<n<<"个权值(/n注意:必须以回车结束):"<<endl;
<endl<<"第"<<i+1<<"个字符的权值:";

void Initialization()  {          flag=1;          int num;          int num2;          cout<<"下面初始化赫夫曼链表"<        cin>>num;          n=num;          w=(int*)malloc(n*sizeof(int));          z=(char*)malloc(n*sizeof(char));          cout<<"/n请依次输入"<        char base[2];          for(i=0;i   {          cout<<"第"<        gets(base);//这个地方有点小小的问题          *(z+i)=*base;          }          for(i=0;i<=n-1;i++)          {                  cout<        }          cout<<"/n请依次输入"<        for(i=0;i<=n-1;i++)          {           cout<         cin>>num2;           *(w+i)=num2;     }          HuffmanCoding(HT,HC,w,n);  //------------------------打印编码-------------------------------------------          cout<<"字符对应的编码为:"<        for(i=1;i<=n;i++)          {                  //cout<<"字符"<<*(z+i-1)<<"的编码";                  puts(HC[i]);          }  //--------------------------将赫夫曼编码写入文件------------------------          cout<<"下面将赫夫曼编码写入文件"<            FILE *hfmTree;          char r[]={' ','/0'};                 if((hfmTree=fopen("hfmTree.txt","w"))==NULL)     {     cout<<"can not open file"<   return;     }            fputs(z,hfmTree);          for(i=0;i        {           fprintf(hfmTree,"%6d",*(w+i));           fputs(r,hfmTree);     }          for(i=1;i<=n;i++)          {           fputs(HC[i],hfmTree);           fputs(r,hfmTree);          }          fclose(hfmTree);          cout<<"已将字符与对应编码写入根目录下文件hfmTree.txt中"<}

上述代码中有个小问题,已经用红色标示,这个地方获取数据的时候有一次是没有输入就直接执行的。这个疑问我一直都没想通。初始化的结果放入hfmTree.txt的文件中。

下面来看看输入要编码的字符的函数InputCode()

void InputCode()  {      //cout<<"请输入你想要编码的字符"<    FILE *tobetran;      char str[100];      if((tobetran=fopen("tobetran.txt","w"))==NULL)      {          cout<<"不能打开文件"<        return;      }      cout<<"请输入你想要编码的字符"<    cin>>str;      fputs(str,tobetran);      cout<<"获取报文成功"<    fclose(tobetran);  }

这个函数里我也碰到了一个问题,cin>>str;部分一开始的时候,我是用gets代替的,不过问题就是用gets我不用输入就能直接执行完函数。这个郁闷啊。

下面来看看赫夫曼编码/译码器函数

//---------------------编码函数---------------------------------  void Encoding()  {          cout<<"下面对目录下文件tobetran.txt中的字符进行编码"<            FILE *tobetran,*codefile;            if((tobetran=fopen("tobetran.txt","rb"))==NULL)          {                  cout<<"不能打开文件"<        }          if((codefile=fopen("codefile.txt","wb"))==NULL)          {                  cout<<"不能打开文件"<        }            char *tran;          i=99;          tran=(char*)malloc(100*sizeof(char));            while(i==99)          {                  if(fgets(tran,100,tobetran)==NULL)                  {                          cout<<"不能打开文件"<                        break;                  }                  for(i=0;*(tran+i)!='/0';i++)                  {                          for(j=0;j<=n;j++)                          {                                  if(*(z+j-1)==*(tran+i))                                  {                                    fputs(HC[j],codefile);                                    if(j>n)                                    {                                     cout<<"字符错误,无法编码!"<                                   break;                                    }                                  }                          }                  }          }          cout<<"编码工作完成"<        fclose(tobetran);          fclose(codefile);          free(tran);  }    //-----------------译码函数---------------------------------  void Decoding()  {  cout<<"下面对根目录下文件codefile.txt中的字符进行译码"<        FILE *codef,*txtfile;          if((txtfile=fopen("//Textfile.txt","w"))==NULL)          {                  cout<<"不能打开文件"<        }  //txtfile=fopen("Textfile.txt","w");          if ((codef=fopen("codefile.txt","r"))==NULL)          {                  cout<<"不能打开文件"<        }  //codef=fopen("codefile.txt","r");     char *work,*work2,i2;  int i4=0,i,i3;          unsigned long length=10000;          work=(char*)malloc(length*sizeof(char));  fgets(work,length,codef);  work2=(char*)malloc(length*sizeof(char));  i3=2*n-1;  for(i=0;*(work+i)!='/0';i++)  {     i2=*(work+i);     if(HT[i3].lchild==0)      {      *(work2+i4)=*(z+i3-1);      i4++;      i3=2*n-1;      i--;     }     else if(i2=='0') i3=HT[i3].lchild;     else if(i2=='1') i3=HT[i3].rchild;  }  *(work2+i4)='/0';  fputs(work2,txtfile);  cout<<"译码完成"<free(work);  free(work2);          fclose(txtfile);          fclose(codef);    }

整个赫夫曼编码/译码器实现过程也就这样了。

到此,相信大家对“怎么利用C++实现赫夫曼编码译码器”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/237600.html

(0)
上一篇 2022年1月29日
下一篇 2022年1月29日

相关推荐

发表回复

登录后才能评论