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

📄 haffcode.cpp

📁 本程序能够实现哈夫曼编码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fstream.h>
#include "Design.h"
#include "Introduce.h"
#include "Direction.h"
static unsigned int NUM=0,M_num=0;
struct Tree
{ 
	unsigned int ID;
	char C;
	unsigned int Weight;
	unsigned int Parent,L_child,R_child;
	int flag;
};
struct C_Code
{
	char STR;
	char* Code;
};
void sleep(clock_t wait)
{
	clock_t delay;
	delay=wait+clock();
	while(delay>clock())
		;
}
void getCode()
{
	int len;
	char* str=new char[50];
	cout<<"Please input the code you want to translate: ";
	cin>>str;
	len=strlen(str);
	fstream writeTo("code.boy",ios::trunc);
	writeTo<<len<<' '<<str;
	writeTo.close();	
	delete[] str;
}
int find(Tree* sear,char C,int n)
{
	int Ctrl=-1;
	for(int i=0;i<n;i++)
	{
		if(sear[i].C==C)
		{
			Ctrl=1;
			break;
		}
	}
	return(Ctrl);
	
}
void Backup_Rule(int shift)
{
	if(shift==0)
	{
		int len=0;
		char* str;
		char C;
		int n;
		ifstream Back_From("Rule.boy",ios::nocreate);
			if (!Back_From.fail())
			{
				Back_From>>len;
				if (len>0)
				{
					fstream Back_To("Rule_Backup.boy",ios::trunc);
					Back_To<<len<<endl;
					for (int i=0;i<len;i++)
					{
						Back_From>>C>>n;
						str=new char[n+1];
						Back_From>>str;
						Back_To<<C<<' '<<n<<' '<<str<<endl;
						delete[] str;
					}
					Back_To.close();
				}
				Back_From.close();
				
			}
	}
	else
		{
		int len=0;
		char* str;
		char C;
		int n;
		ifstream Back_From("Rule_Backup.boy",ios::nocreate);
			if (!Back_From.fail())
			{
				Back_From>>len;
				if (len>0)
				{
					fstream Back_To("Rule.boy",ios::trunc);
					Back_To<<len<<endl;
					for (int i=0;i<len;i++)
					{
						Back_From>>C>>n;
						str=new char[n+1];
						Back_From>>str;
						Back_To<<C<<' '<<n<<' '<<str<<endl;
						delete[] str;
					}
					Back_To.close();
				}
				Back_From.close();
			}
	}
}
void Code_Show();
Tree* INIT()                                               //
{
	Tree* Temp=NULL;
	char C;
	int Ctrl;
	unsigned int Wt,i;
	cout<<"Please input the number of the chars : ";
	cin>>NUM;
	while(NUM<2)
	{
		cout<<"The wrong number,please input the number of the chars again: ";
		cin>>NUM;
	}
	M_num=2*NUM-1;
	Tree* INit_Tree=new Tree[M_num+1];
	for(i=0;i<NUM;i++)
	{
		Temp=INit_Tree+i;
		cout<<"Please input the char  :  ";
		cin>>C;
		Ctrl=find(INit_Tree,C, i);
		while(Ctrl==1)
		{
			cout<<"Please input the char again :  ";
			cin>>C;
			Ctrl=find(INit_Tree,C, i);
		}
		cout<<"Please input the weight:  ";
		cin>>Wt;
		while(Wt<=0)
		{
			cout<<"The wrong number,please input the weight of this char again: ";
			cin>>NUM;
		}
		Temp->ID=i+1;
		Temp->C=C;
		Temp->Weight=Wt;
		Temp->Parent=0;
		Temp->L_child=0;
		Temp->R_child=0;
		Temp->flag=0;		
	}
	for(unsigned int j=NUM;j<M_num;j++)
	{
		Temp=INit_Tree+j;
		Temp->ID=j+1;
		Temp->C='-';		
		Temp->Weight=0;
		Temp->Parent=0;
		Temp->L_child=0;
		Temp->R_child=0;
		Temp->flag=0;
	}	
	return (INit_Tree);
}
Tree* HaffTree(Tree* Head)
{
	unsigned int i,j,Ctrl=0;
	Tree* Point_1;
	Tree* Point_2;
	Tree* temp;
	Tree* Temp;
	for(j=NUM;j<M_num;j++)
	{
		for(i=0;i<j;i++)
		{
			Temp=Head+i;
			if(Temp->flag==1)
				continue;	
			if(Ctrl==0)
			{
				Point_1=Temp;
				++Ctrl;
				continue;
			}
			else
			{
				if(Ctrl==1)
				{
					Point_2=Temp;
					++Ctrl;
					if(Point_1->Weight>Point_2->Weight)
					{
						temp=Point_1;
						Point_1=Point_2;
						Point_2=temp;
					}
					continue;
				}
			}				
				if(Temp->Weight<Point_1->Weight)
				{			
					Point_2=Point_1;
					Point_1=Temp;
					continue;
				}
				else
					if(Temp->Weight>=Point_1->Weight&&Temp->Weight<Point_2->Weight)
						Point_2=Temp;				
		}	
		Temp=Head+j; 		
		Temp->Weight=(Point_1->Weight+Point_2->Weight);
		Point_1->Parent=Temp->ID;
		Point_1->flag=1;
		Point_2->Parent=Temp->ID;
		Point_2->flag=1;
		Temp->L_child=Point_1->ID;
		Temp->R_child=Point_2->ID;	
		Ctrl=0;
	}
	fstream HaffTree("tree.boy",ios::trunc);
	HaffTree<<NUM<<endl;
	for(i=0;i<M_num;i++)
	{
		HaffTree<<Head[i].ID<<' '<<Head[i].C<<' '<<Head[i].Weight<<' '<<Head[i].Parent<<' '<<Head[i].L_child<<' '<<Head[i].R_child<<endl;
	}
	HaffTree.close();
	return (Head);
}
void Print(Tree* head)
{
	ofstream Code("code.boy");
	unsigned int i=0;
	Tree* Temp=NULL;
	cout<<"┏━━┳━━━━┳━━━━┳━━━━┳━━━━━┳━━━━━┳━━━┓"<<endl;
	cout<<"┃"<<setw(3)<<"ID"<<setw(3)<<"┃"<<setw(7)<<"string"<<setw(3)<<"┃"
		<<setw(7)<<"Weight"<<setw(3)<<"┃"<<setw(7)<<"Parent"<<setw(3)<<"┃"
		<<setw(8)<<"L_child"<<setw(4)<<"┃"<<setw(8)<<"R_child"<<setw(4)<<"┃"
		<<setw(5)<<"flag"<<setw(3)<<"┃"<<endl;
	
	for(;i<M_num;i++)
	{
		Temp=head+i;
		cout<<"┣━━╋━━━━╋━━━━╋━━━━╋━━━━━╋━━━━━╋━━━┫"<<endl;
		cout<<"┃"<<setw(3)<<Temp->ID<<setw(3)<<"┃"<<setw(5)<<Temp->C<<setw(5)<<"┃"
			<<setw(5)<<Temp->Weight<<setw(5)<<"┃"<<setw(5)<<Temp->Parent<<setw(5)<<"┃"
			<<setw(6)<<Temp->L_child<<setw(6)<<"┃"<<setw(6)<<Temp->R_child<<setw(6)<<"┃"
			<<setw(4)<<Temp->flag<<setw(4)<<"┃"<<endl;		
	}
	cout<<"┗━━┻━━━━┻━━━━┻━━━━┻━━━━━┻━━━━━┻━━━┛"<<endl;
} 
char** TranstoCode(Tree* Head)
{
	char** Save_Code=new char* [NUM];	
	Tree* Temp_Son;
	Tree* Temp_Father;
	int K;
	unsigned int i=0;
	for(;i<NUM;i++)
	{
		Temp_Son=Head+i;
		char* temp=new char[NUM];     //Code should use the temp array
		int Pos=NUM-1;
		temp[Pos]='\0';
		while(Temp_Son->Parent!=0)
		{
			K=(Temp_Son->Parent)-1;
			Temp_Father=Head+K;
			if(Temp_Son->ID==Temp_Father->L_child)
				temp[--Pos]='0';		
			else	
				temp[--Pos]='1';		
			Temp_Son=Temp_Father;
		}
		Save_Code[i]=new char[NUM-Pos];
		strcpy(Save_Code[i],&temp[Pos]);
		delete[] temp;
	}	
	return(Save_Code);
}
C_Code* Build_Code(char** Head_Code,Tree* Head) //build Rule
{
	unsigned int i;
	char* Str;	
	Tree* Temp;
	C_Code* C_Head=new C_Code[NUM];
	C_Code* C_Temp;
	for(i=0;i<NUM;i++)
	{	
		Temp=Head+i;
		Str=Head_Code[i];
		C_Temp=C_Head+i;
		C_Temp->STR=Temp->C;
		C_Temp->Code=new char[strlen(Str)+1];
		strcpy(C_Temp->Code,Str);
	}
	ofstream Str_Code("Rule.boy");
	Str_Code<<NUM<<endl;
	for(i=0;i<NUM;i++)
		Str_Code<<C_Head[i].STR<<" "<<strlen(C_Head[i].Code)<<" "<<C_Head[i].Code<<endl;
	return ( C_Head );
}
void Print_Code(C_Code* Head)
{
	unsigned int i;
	C_Code* Temp;
	cout<<"┏━━━━┳━━━━━━━━┓"<<endl;
	cout<<"┃"<<setw(7)<<"String"<<setw(3)<<"┃"<<setw(11)<<"Code"<<setw(7)<<"┃"<<endl;
	
	for(i=0;i<NUM;i++)
	{
		Temp=Head+i;
		cout<<"┣━━━━╋━━━━━━━━┫"<<endl;
		cout<<"┃"<<setw(5)<<Temp->STR<<setw(5)<<"┃"<<setw(13)<<Temp->Code<<setw(5)<<"┃"<<endl;		
	}
	cout<<"┗━━━━┻━━━━━━━━┛"<<endl;
}
char* Input()   
{	
	int len=0;
	char* InStr;
	char Str;
	cout<<"You can Press F key to read the file or press I key to input once more !"<<endl;
	cin>>Str;
	if(Str=='i'||Str=='I')
	{
		cout<<"Please input the strings:  ";
		InStr=new char[50];
		cin>>InStr;
		len=strlen(InStr);
		while(len<2)
		{
			cout<<"The strings are too short,please input again: ";
			InStr=new char[50];
			cin>>InStr;
			len=strlen(InStr);
		}
		ofstream Text_In("TEXT.boy");
		Text_In<<len<<' '<<InStr; 
		Text_In.close();
		return (InStr);
	}
	ifstream Text_Read;
	Text_Read.open("TEXT.boy",ios::nocreate);
	if(!Text_Read.fail()&&!Text_Read.eof())
	{	
		Text_Read>>len;
		InStr=new char[len+1];
		Text_Read>>InStr;
		Text_Read.close();
	}
	if(len==0)
	{
		if(Text_Read.fail())
			cout<<"The file isn't exit ,please input the strings:  ";	
		else
			cout<<"The file is empty,please input the strings:  ";	
		InStr=new char[50];
		cin>>InStr;
		len=strlen(InStr);
		while(len<2)
		{
			cout<<"The strings are too short,please input again: ";
			InStr=new char[50];
			cin>>InStr;
			len=strlen(InStr);
		}
		ofstream Text_In("TEXT.boy",ios::trunc);
		Text_In<<len<<' '<<InStr;
		Text_In.close();
	}
	return(InStr);
}
void StrtoCode()
{
	int n,i;
	int R_len;
	C_Code* C_Head=NULL;
	ifstream Text_Rule("Rule.boy",ios::nocreate);
	if(Text_Rule)
	{
		Text_Rule>>R_len;
		NUM=R_len;
		C_Head=new C_Code[R_len];
		for(i=0;i<R_len;i++)
		{
			Text_Rule>>C_Head[i].STR>>n;
			C_Head[i].Code=new char[n+1];
			Text_Rule>>C_Head[i].Code;
		}
		Text_Rule.close();
	}	
	ifstream Text_Read("TEXT.boy",ios::nocreate);	
	Text_Read>>n;
	char* IN_Head=new char[n+1];
	Text_Read>>IN_Head;
	Text_Read.close();	
	char** temp=new char* [n];
	unsigned int j;
	char P1;
	int len=0;
	C_Code* P2;
	for(i=0;i<n;i++)
	{
		P1=*(IN_Head+i);
		for(j=0;j<NUM;j++)
		{
			P2=C_Head+j;
			if(P1==P2->STR)
				break;
		}
		if(j==NUM)
		{
			cout<<P1<<" isn't to be translate to code!"<<endl;
			break;
		}
		else
			temp[i]=new char [strlen(P2->Code)+1];
		len+=strlen(P2->Code);
		temp[i]=P2->Code;
	}
	if(j!=NUM)
	{
		ofstream Text_Code("CODE.boy");
		Text_Code<<len<<endl;
		cout<<"The code is: "<<endl;
		for(i=0;i<n;i++)
		{
			Text_Code<<temp[i];
		}
		Text_Code.close();
		Code_Show();
		cout<<endl;
	}
}

⌨️ 快捷键说明

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