📄 haffcode.cpp
字号:
Tree* INtoCode()
{ NUM=0;
cout<<"NUM = "<<NUM<<endl;
char* IN_Str=Input();
char* Num_Str=new char[50];
Tree* B_tree;
Tree* T_temp;
int num[50];
char Str_Temp;
char temp;
char C=*IN_Str;
int n=0;
unsigned int K=strlen(IN_Str);
unsigned int i;
for( i=0;i<K;i++)
{
temp=*(IN_Str+i);
if(NUM>0&&temp==C)
continue;
for(unsigned int j=i;j<K;j++)
{
Str_Temp=*(IN_Str+j);
if(NUM==0)
{
if(temp==Str_Temp)
++n;
}
else
{
if(temp==Str_Temp)
{
++n;
*(IN_Str+j)=C;
}
}
}
Num_Str[NUM]=temp;
num[NUM]=n;
++NUM;
n=0;
}
Num_Str[NUM]='\0';
M_num=2*NUM-1;
B_tree=new Tree[M_num];
for(i=0;i<NUM;i++)
{
T_temp=B_tree+i;
T_temp->ID=i+1;
T_temp->C=Num_Str[i];
T_temp->Weight=num[i];
T_temp->Parent=0;
T_temp->L_child=0;
T_temp->R_child=0;
T_temp->flag=0;
}
for(i=NUM;i<M_num;i++)
{
T_temp=B_tree+i;
T_temp->ID=i+1;
T_temp->C='-';
T_temp->Weight=0;
T_temp->Parent=0;
T_temp->L_child=0;
T_temp->R_child=0;
T_temp->flag=0;
}
return (B_tree);
}
int Search(char* CCode,C_Code* CR,int l)
{
int mark=-1,i;
for(i=0;i<l;i++)
{
if(strcmp(CCode,CR[i].Code)==0)
{
mark=i;
break;
}
}
return(mark);
}
void CodetoStr()
{
int len,n,Len_Code;
ifstream Rule("Rule.boy");
Rule>>len;
C_Code* CRule=new C_Code[len];
for(int i=0;i<len;i++)
{
Rule>>CRule[i].STR>>n;
CRule[i].Code=new char[n+1];
Rule>>CRule[i].Code;
}
ifstream F_Code("code.boy");
F_Code>>Len_Code;
char* Code=new char[Len_Code+1];
for(i=0;i<Len_Code;i++)
F_Code>>Code;
cout<<"Code : "<<endl;
Code_Show();
char* temp=Code;
char* Cut_Code;
char Cur;
char* P;
char* P1=NULL;
int Mark,N_str=0,m=0;
for(i=1;i<len;i++)
{
Cut_Code=new char[i+1];
strncpy(Cut_Code,temp,i);
Cut_Code[i]='\0';
Mark=Search(Cut_Code,CRule,len);
if(Mark<0||Mark>=len)
continue;
else
{
++N_str;
Cur=CRule[Mark].STR;
P=new char[N_str+1];
if(P1!=NULL)
strcpy(P,P1);
P[N_str-1]=Cur;
P[N_str]='\0';
P1=P;
temp+=i;
m+=i;
i=0;
if(m>=Len_Code)
{
break;
}
}
}
if(i>=len)
cout<<"The code Error!"<<endl;
else
{
ofstream File("TEXT.boy");
cout<<"The text is "<<N_str<<" bytes: "<<P<<endl;
File<<N_str<<" "<<P<<endl;
}
}
void TreePrint(int);
void TreeShow()
{
int len=0,n=0,i,j,k=0;
Tree* Htree;
ifstream Tree_File;
Tree_File.open("tree.boy",ios::nocreate);
if(Tree_File)
{
Tree_File>>len;
n=2*len-1;
Htree=new Tree[n];
for(i=0;i<n;i++)
{
Tree_File>>Htree[i].ID>>Htree[i].C>>Htree[i].Weight>>Htree[i].Parent>>Htree[i].L_child>>Htree[i].R_child;
Htree[i].flag=0;
}
NUM=len;
M_num=n;
Tree* temp=Htree+n-1;
int imag=n-1;
fstream Tree_in("temp.boy",ios::trunc);
Tree_in<<M_num<<endl;
while(Htree[n-1].flag!=3)
{
++(temp->flag);
if(temp->flag==1)
{
Tree_in<<temp->ID<<' '<<temp->C<<' '<<imag<<endl;
if(k==0)
k=imag;
else
{
if(k>imag)
k=imag;
}
}
if(temp->flag>=3)
{
j=temp->Parent-1;
temp=Htree+j;
++imag;
continue;
}
if(temp->L_child==0&&temp->R_child==0)
{
j=temp->Parent-1;
temp=Htree+j;
++imag;
continue;
}
if(temp->flag==2)
{
j=temp->R_child-1;
temp=Htree+j;
--imag;
continue;
}
if(temp->L_child==0)
++(temp->flag);
else
{
j=temp->L_child-1;
temp=Htree+j;
--imag;
continue;
}
if(temp->R_child==0)
++(temp->flag);
else
{
j=temp->R_child-1;
temp=Htree+j;
--imag;
continue;
}
}
Tree_in.close();
TreePrint(k);
}
else
cout<<"The Haffmantree doesn't exit!"<<endl;
NUM=0;
M_num=0;
}
void TreePrint(int n)
{
Tree* Htree;
int SPA_n,len,i,j,k;
ifstream Tree_Out;
Tree_Out.open("temp.boy",ios::nocreate);
Tree_Out>>len;
if(!Tree_Out.fail())
{
if(len==0)
cout<<"The file save the HaffmanTree is empty!"<<endl;
else
{
--n;
cout<<"┏━━━━━━━━━━━━━━┓"<<endl; ; //15 words ,30 bytes
Htree=new Tree[len];
for(i=0;i<len;i++)
{
Tree_Out>>Htree[i].ID>>Htree[i].C>>Htree[i].flag;
k=Htree[i].flag;
k-=n;
SPA_n=19-2*k;
cout<<"┃"<<setw(3)<<Htree[i].ID<<setw(4)<<Htree[i].C<<setw(SPA_n)<<' ';
for(j=0;j<k;j++)
cout<<"■";
cout<<setw(4)<<"┃"<<endl;
}
cout<<"┗━━━━━━━━━━━━━━┛"<<endl;
Tree_Out.close();
delete[] Htree;
}
}
else
cout<<"The file save the HaffmanTree isn't exit!"<<endl;
}
void Code_Show()
{
int len=0,i;
ifstream CodeTxt;
CodeTxt.open("code.boy",ios::nocreate);
if(CodeTxt.fail())
cout<<"The code file isn,t exit!"<<endl;
else
{
CodeTxt>>len;
if(len==0)
cout<<"The code file is empty!"<<endl;
else
{
char* Code_Array=new char[len+1];
CodeTxt>>Code_Array;
for(i=0;i<len;i++)
{
cout<<Code_Array[i];
if((i+1)%50==0)
cout<<endl;
}
}
}
cout<<endl;
}
void main()
{
Image();
cout<<"Please wait ..."<<endl;
cout<<"\n\n"<<setw(50)<<"欢迎使用Haffman编码系统!"<<"\n\n";
sleep((clock_t)3*CLOCKS_PER_SEC);
Introduce();
sleep((clock_t)3*CLOCKS_PER_SEC);
Direction();
int CTRL=0;
Tree* HEAD;
char** Str_Code;
char* Control=new char[5];
while(Control!=NULL)
{
cout<<"Please input the control string: ";
cin>>Control;
if(strcmp(Control,"i")==0||strcmp(Control,"I")==0)
{
HEAD= INIT();
Print(HEAD);
HEAD=HaffTree(HEAD);
Print(HEAD);
Str_Code=TranstoCode(HEAD);
C_Code* StrandCode=Build_Code(Str_Code,HEAD);
Print_Code(StrandCode);
cout<<"The rule has been build,do you want to back-up? (Y/N) " ;
char* B=new char[2];
cin>>B;
while(B!=NULL)
{
if(strcmp(B,"Y")==0||strcmp(B,"y")==0)
{
Backup_Rule(0);
break;
}
else
if(strcmp(B,"N")==0||strcmp(B,"n")==0)
break;
cout<<"The wrong order,reput again:";
cin>>B;
}
delete[] B;
cout<<"OK,the command has been executed.Press ENTER key to continue!"<<endl;
cin.get();
}
else
{
if(strcmp(Control,"e")==0||strcmp(Control,"E")==0)
{
cout<<"输入命令\"accord\"依据编码规则编码或者输入\"direct\"直接对输入内容编码,编码后保存该编码规则."<<endl;
char * Comm=new char[7];
cin>>Comm;
while(strcmp(Comm,"accord")!=0&&strcmp(Comm,"direct")!=0)
{
cout<<"错误的指令代码,请重新输入!"<<endl;
cin>>Comm;
}
if(strcmp(Comm,"accord")==0)
{
Input();
StrtoCode();
}
else
{
CTRL=1;
HEAD= INtoCode();
Print(HEAD);
HEAD=HaffTree(HEAD);
Print(HEAD);
Str_Code=TranstoCode(HEAD);
C_Code* StrandCode=Build_Code(Str_Code,HEAD);
Print_Code(StrandCode);
StrtoCode();
}
}
else
{
if(strcmp(Control,"d")==0||strcmp(Control,"D")==0)
{
if(CTRL==1)
{
CTRL=0;
cout<<"Translate the codes accord to the rule builded in the Initialization,or the Least? (I/L) " ;
char* R=new char[2];
cin>>R;
while(R!=NULL)
{
if(strcmp(R,"I")==0||strcmp(R,"i")==0)
{
Backup_Rule(1);
break;
}
else
if(strcmp(R,"L")==0||strcmp(R,"l")==0)
break;
cout<<"The wrong command!Input again: ";
cin>>R;
}
delete[] R;
}
char p;
cout<<"Input F to read the code from the file,or iput I to read the information from keyoard!"<<endl;
cin>>p;
while (p!='F'&&p!='f'&&p!='i'&&p!='I')
{
cout<<"The wrong string,please input again!"<<endl;
cin>>p;
}
if (p=='i'||p=='I')
{
getCode();
CodetoStr();
}
else
CodetoStr();
}
else
{
if(strcmp(Control,"p")==0||strcmp(Control,"P")==0)
{
Code_Show();
}
else
{
if(strcmp(Control,"t")==0||strcmp(Control,"T")==0)
{
;TreeShow();
}
else
{
if(strcmp(Control,"q")==0||strcmp(Control,"Q")==0)
{
cout<<"\n\n"<<setw(50)<<"欢迎使用本程序! 再见!\n "<<endl;
Image();
sleep((clock_t)3*CLOCKS_PER_SEC);
exit(0);
}
else
cout<<"\nThe control string you input is wrong,please input again!"<<endl;
}
}
}
}
}
cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -