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

📄 fz.h

📁 一个数据结构课程之后自己编写的lzw压缩算法
💻 H
字号:
#include "iostream.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "fstream.h"
#include "iomanip.h"

struct HASH2{
	int index;
	char word;
};

int getfilelen(CString name)
{
	ifstream re(name,ios::binary|ios::in);
	if(!re)
	{
		int j=MessageBox(NULL,"File could be open! ","wrong  ", MB_OK|MB_ICONWARNING);
		if(j==IDOK) exit(1);
	}
	char ch;
	int i=0;
	ch=re.get();
	while(!re.eof()) 
	{
		i++; 
		ch=re.get();
	}
	return i;
}

char* readfile2(CString filename, int n)
{
    int i=0;
	char* ptr1=new char[n+2];
	char ch;
	int k=1;
	ifstream readf(filename,ios::binary|ios::in);
	if(!readf)
	{
		int j=MessageBox(NULL,"File could be open! ","wrong  ", MB_OK|MB_ICONWARNING);
		if(j==IDOK) exit(1);
	}
	while(!readf.eof())
	{		
		ch=readf.get();
		if(!readf.eof())
		{
			ptr1[i]=ch;
			i++;
		}
	}
	ptr1[i]=char('\0');
	readf.close();
	return ptr1;
}
////////////////////
void refresh2(HASH2 array[])
{
	int i=256;
	while(i<4096)
	{
		array[i].index=-2;
		i++;
	}
}
////////////////////
void initial2(HASH2 hash[4098])
{

	int i=0;
	while(i<256)
	{
		hash[i].index=-1;
		hash[i].word=i;
		i++;
	}
	refresh2(hash);
}


void writefile2(CString filename,char a)
{
	ofstream wri(filename,ios::ate);
	if(!wri)
	{
		int j = MessageBox(NULL,"File can not be writen into!","wrong", MB_OK|MB_ICONEXCLAMATION);
		if(j==IDOK) exit(1);
	}
	wri.write(&a,1);
}

void clear2(CString name)
{
	ofstream file(name,ios::out);
	file.close();
}


void bitoperation(char* chfile,int *numfile,int len)///////////////////////
{
	int total=len/3*2,i=0;
	int chsub=0;
	unsigned char ch1,ch2,ch3,ch4;
	if( len%3!=2 && len%3!=0 ) 
	{
		int j=MessageBox(NULL,"目标文件不是合法的压缩文件!", "illegal" ,MB_ICONEXCLAMATION|MB_OK);
		if(j==IDOK) exit(1);
	}
	while(i<total)
	{
		ch1=chfile[chsub];
		chsub++;
		ch2=ch3=chfile[chsub];
		chsub++;
		ch4=chfile[chsub];
		chsub++;
		ch2=ch2>>4;
		numfile[i]=int(ch2) + int(ch1)*16;
		i++;

		ch3=ch3&15;
		numfile[i]=int(ch3)*256+ch4;
		i++;	
	}
	
	if(len%3==2)
	{
		ch1=chfile[chsub];
		chsub++;
		ch2=chfile[chsub];
		chsub++;
		int a=int(ch1)*16;
		numfile[i]=int(ch2)+a;		
	}
}

char findfirch(HASH2 hash[], int a)
{
	while(hash[a].index!=-1)	a=hash[a].index;
	return hash[a].word;
}


int hashexist(HASH2 hash[], int a)
{
	if( hash[a].index==-2 ) return 0;
	else return 1;
}

void writestring(CString name,char* ptr)
{
	ofstream wri(name, ios::ate);
	if(!wri)
	{
		int j=MessageBox(NULL,"file can not be open!", "wrong " ,MB_OK|MB_ICONEXCLAMATION);
		if(j==IDOK) exit(1);
	}
		wri<<ptr;
}

void reserve(char* ptr)
{
	int a=strlen(ptr);
	int i=0,j=a/2;
	while(i<j)
	{
		char temp;
		temp=ptr[i];
		ptr[i]=ptr[a-1-i];
		ptr[a-1-i]=temp;
		i++;
	}
}

void ext(HASH2 hash[],int* numfile,int numfilelen,CString filename)
{
	int i=0 , k=0 , j=256;
	while(i<numfilelen)
	{
		if(k==0)   
		{
			writefile2(filename,hash[numfile[i]].word);	
			i++;
			k=1;
		}

		char *ptr;
		if( hashexist(hash,numfile[i]) ) 
		{
			int t=0,temp=numfile[i];
			if(numfile[i]>255) t=temp-253;
			else t=2;
			ptr=new char[t];
			t=0;
			while(hash[temp].index!=-1)
			{
				ptr[t]=hash[temp].word;
				temp=hash[temp].index;
				t++;
			}
			ptr[t]=hash[temp].word;
			t++;
			ptr[t]='\0';
			reserve(ptr);
			writestring(filename,ptr); 
			
			hash[j].index=numfile[i-1];
			hash[j].word=findfirch(hash,numfile[i]);
			j++;
			if(j==4096) 
			{
				refresh2(hash);
				j=256;
				k=0;
			}
			i++;
			delete [] ptr;
		}

		else 
		{
			hash[j].index=numfile[i-1];
			hash[j].word=findfirch(hash,numfile[i-1]);
			j++;
			if(j==4096) 
			{
				refresh2(hash);
				j=96;
				k=0;
			}
			if( !hashexist(hash,numfile[i]) )
			{
				int temp=MessageBox(NULL,"The target is illegal file!", "wrong " ,MB_OK|MB_ICONEXCLAMATION);
				if(temp==IDOK) exit(1);
			}
			int temp=numfile[i],t=0;
			ptr=new char[temp-92];
			while(hash[temp].index!=-1)
			{
				ptr[t]=hash[temp].word;
				temp=hash[temp].index;
				t++;
			}
			ptr[t]=hash[temp].word;
			t++;
			ptr[t]='\0';
			reserve(ptr);
			writestring(filename,ptr);
			i++;
			delete []ptr;
		}
	}
}		

⌨️ 快捷键说明

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