⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 haffunode_1.txt

📁 一个很简单的哈夫曼编码程序!!要的就下哟!
💻 TXT
字号:
#include<iostream.h>   
  #include<string.h>   
  #include<conio.h>   
  #include<fstream.h>   
  #include"harfnode.h"   
  #define   maxsize   52   
  class   harfcode{   
  public:   
  int   start;   
  char   cd[maxsize];   
  };   
  harfnode   hnode[maxsize];   
          harfcode   hcode[maxsize],d;   
  int   size;   
    
  //初始化   
  void   Initialization(){   
  cout<<"请先输入字符集的大小:"<<endl;   
  cin>>size;   
    
  for(int   t=1;t<=size;t++){   
  cout<<"第"<<t<<"个结点的值域:";   
  cin>>(hnode[t]).data;   
  cout<<"\t第"<<t<<"个结点的权值:";   
  cin>>(hnode[t]).weight;   
  }   
      cout<<"哈夫曼树建立成功!"<<endl;   
    cout<<"请选择功能号:";   
  };   
  //编码   
  void   Enoding(){   
    
  for(int   i=size+1;i<=2*size-1;i++){   
  int   templ_weight=32767,tempr_weight=32767,templ_local,tempr_local;   
  for(int   j=1;j<i;j++){   
  if(((hnode[j]).parent==0)&&(templ_weight>(hnode[j]).weight))   
  {     
      templ_weight=(hnode[j]).weight;     
      templ_local=j;}}//for   end   
  (hnode[templ_local]).parent=i;   
  (hnode[i]).left=templ_local;//left   end   
  for(j=1;j<i;j++){   
  if(((hnode[j]).parent==0)&&(tempr_weight>(hnode[j]).weight))   
  {     
      tempr_weight=(hnode[j]).weight;     
      tempr_local=j;}}//for   end   
  (hnode[tempr_local]).parent=i;   
  (hnode[i]).right=tempr_local;//right   end   
  (hnode[i]).weight=(hnode[templ_local]).weight+(hnode[tempr_local]).weight;//   
    
    
  }   
  ofstream   fhfmfile("c:\\hfmTree.txt");   
  for(i=1;i<=2*size-1;i++){   
  fhfmfile<<(hnode[i]).data;   
  fhfmfile<<(hnode[i]).weight;   
  fhfmfile<<(hnode[i]).parent;   
  fhfmfile<<(hnode[i]).left;   
  fhfmfile<<(hnode[i]).right;   
    
  }   
  //编码   
  int   c,f;   
  for(i=1;i<=size;i++){   
  d.start=size+1;   
  c=i;   
          f=hnode[i].parent;   
  while(f!=0){   
  if(hnode[f].left==c)d.cd[--d.start]='0';   
  else   d.cd[--d.start]='1';   
  c=f;   
  f=hnode[f].parent;   
  }//while   end   
  hcode[i]=d;   
  }//for   end   
  ofstream   fhfmcode("c:\\Codefile.txt");   
  for(i=1;i<=size;i++){   
  // fhfmcode<<(hcode[i]).start;   
                  for(int   t=hcode[i].start;t<=size;t++)   
                fhfmcode<<hcode[i].cd[t];   
  }   
  cout<<"建立编码成功!"<<endl;   
  cout<<"请输入功能号!"<<endl;   
  }   
  //译码   
  void   Decoding(){   
  cout<<size;   
  };   
    
  //印代码文件   
  void   Print(){};   
  //印哈夫曼树   
  void   Printingtree(){};   
  int   main(){   
  //clrscr();   
  cout<<"1:建立哈夫曼树"<<endl;   
          cout<<"2:建立编码"<<endl;   
  cout<<"3:   译码:"<<endl;   
  cout<<"4:印代码文件"<<endl;   
  cout<<"5:印哈夫曼树"<<endl;   
  cout<<"q:退出"<<endl;   
    
  char   ch;   
  while((cin>>ch)&&(ch!='q')&&(ch!='Q')){   
  switch(ch){   
  case   '1':   Initialization();break;   
  case   '2':   Enoding();break;   
  case   '3':   Decoding();break;   
  case   '4':   Print();   break;   
  case   '5':   Printingtree();break;   
  default:cout<<"呵呵,你按错了键!"<<endl;   
  }   
  }   
  return   0;}   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -