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

📄 aid.h

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


struct HASH{
	int index;
	char word;
};
/////////////////////

//////////////////////
int getfilelen1(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* readfile(CString filename, int n)
{
    int i=0;
	char* ptr1=new char[n+2];
	char ch;
	int k=1;
	ifstream readf(filename,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 refresh(HASH array[])
{
	int i=256;
	while(i<4096)
	{
		array[i].index=-2;
		i++;
	}
}
////////////////////
void initial(HASH hash[4098])
{

	int i=0;
	while(i<256)
	{
		hash[i].index=-1;
		hash[i].word=i;
		i++;
	}	
	refresh(hash);
}
/////////////////
int searchhash(HASH hash[],char ch,int index)
{
	int i=0;
	while( hash[i].index!=-2 && i<4096)
	{
		if( hash[i].word==ch && hash[i].index==index ) return i;
		else i++;
	}
    return -1;
}
//////////////
void cmps(HASH hash[],char*ptr,int* res,int &len)
{
	int hashsub=256,ptrsub=0,ressub=0,searchreturn,tempnum;
	char tempch=ptr[ptrsub];
	while(tempch!='\0')
	{
		tempnum=-1;
		searchreturn=searchhash(hash,tempch,tempnum);
		if(searchreturn==-1) {
			int j;
			j=MessageBox(NULL,"文档中有非法字符","输入流错误", MB_ICONWARNING | MB_OK);
			if(j==IDOK) exit(1);
		}
			while(searchreturn!=-1&&tempch!='\0')///////////////////////////////////////////
		{
			tempnum=searchreturn;
			ptrsub++;
			tempch=ptr[ptrsub];
			if(tempch!='\0')	searchreturn=searchhash(hash,tempch,tempnum);
		}
		res[ressub]=tempnum;
		ressub++;
		if(tempch!='\0')
		{
			hash[hashsub].index=tempnum;
			hash[hashsub].word=tempch;
			hashsub++;
		}
		if(hashsub==4096) refresh(hash);
	}
	len=ressub;
}
/////////////
void writefile(CString filename,char a)
{
	ofstream wri(filename,ios::binary | 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 bitnumshort(int * res,int len,CString filepath2)
{
	int temp,i=0;
	int a,b,c,d;
	if(len%2==0) temp=len;
	else temp=len-1;
	char ch;
	int ji=0;/////////////////////////////
	
	while(i<temp)
	{
		a=b=res[i];
		i++;
		c=d=res[i];
		i++;
		ch=a>>4;
		writefile(filepath2,ch);

		b=b&15;
		b=b<<4;
		c=c>>8;
		ch=b+c;
		writefile(filepath2,ch);

		ch=d&255;
		writefile(filepath2,ch);

	}
	if(temp==len-1) 
	{
		a=b=res[i];
		ch=a>>4;
		writefile(filepath2,ch);

		ch=b&15;
		writefile(filepath2,ch);
	}
	
}


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

⌨️ 快捷键说明

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