?? haffunode_1.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 + -