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

📄 lzari.cpp

📁 一个学生考试成绩管理的半成品
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		if (match_length <= THRESHOLD) 
		{
			match_length = 1;  EncodeChar(text_buf[r]);
		} 
		else 
		{
			EncodeChar(255 - THRESHOLD + match_length);
			EncodePosition(match_position - 1);
		}
		last_match_length = match_length;
		if(!m_bMem)
		{
			for (i = 0; i < last_match_length && (c = getc(infile)) != EOF; i++) 
			{
				DeleteNode(s);  text_buf[s] = c;
				if (s < F - 1) text_buf[s + N] = c;
				s = (s + 1) & (N - 1);
				r = (r + 1) & (N - 1);
				InsertNode(r);
			}
		}
		else
		{			
			for (i = 0; i < last_match_length && m_nInCur < m_nInLength ; i++) 
			{
				c = m_pInBuffer[m_nInCur++];
				DeleteNode(s);  
				text_buf[s] = c;
				if (s < F - 1) text_buf[s + N] = c;
				s = (s + 1) & (N - 1);
				r = (r + 1) & (N - 1);
				InsertNode(r);
			}
		}
		if ((textsize += i) > printcount) 
		{
#ifdef _OUTPUT_STATUS
			printf("%12ld\r", textsize);
#endif	
			printcount += 1024;
		}
		while (i++ < last_match_length) 
		{
			DeleteNode(s);
			s = (s + 1) & (N - 1);
			r = (r + 1) & (N - 1);
			if (--len) InsertNode(r);
		}
	} while (len > 0);
	
	EncodeEnd();

#ifdef _OUTPUT_STATUS	
	printf("In : %lu bytes\n", textsize);
	printf("Out: %lu bytes\n", codesize);
	printf("Out/In: %.3f\n", (double)codesize / textsize);
#endif
}

void LZARI::Decode(void)
{
	int  i, j, k, r, c;
	unsigned long int  count;

	if (!m_bMem)
	{
		if (fread(&textsize, sizeof textsize, 1, infile) < 1)
			Error("Read Error");  /* read size of text */
	}
	else
	{
		if(m_nInLength < sizeof textsize)
			Error("Read Error");
		memcpy(&textsize,m_pInBuffer + m_nInCur,sizeof textsize);
		
		//m_OutBuffer.reserve(textsize);
		m_nOutLength = textsize;
		//m_nOutCur = 0;
		
		m_nInCur += sizeof textsize;
	}
	
	if (textsize == 0) return;
	
	StartDecode();
	StartModel();
	
	for (i = 0; i < N - F; i++) text_buf[i] = ' ';
	r = N - F;
	for (count = 0; count < textsize; ) 
	{
		c = DecodeChar();
		if (c < 256) 
		{
			if(!m_bMem)
				putc(c, outfile);
			else
			{
				//m_OutBuffer[m_nOutCur++] = c;
				m_OutBuffer.push_back(c);
				//m_nOutCur++;
			}
			text_buf[r++] = c;
			r &= (N - 1);
			count++;
		}
		else
		{
			i = (r - DecodePosition() - 1) & (N - 1);
			j = c - 255 + THRESHOLD;
			for (k = 0; k < j; k++) 
			{
				c = text_buf[(i + k) & (N - 1)];
				if(!m_bMem)
					putc(c, outfile);
				else
				{
				//	m_pOutBuffer[m_nOutCur++] = c;
					m_OutBuffer.push_back(c);
					//m_nOutCur ++;
				}
				text_buf[r++] = c;
				r &= (N - 1);
				count++;
			}
		}
		if (count > printcount) 
		{
#ifdef _OUTPUT_STATUS
			printf("%12lu\r", count);
#endif			
			printcount += 1024;
		}
	}

#ifdef _OUTPUT_STATUS
	printf("%12lu\n", count);
#endif
}

void LZARI::Compress(const char *lpszInfile,const char *lpszOutfile)
{
	m_bMem = FALSE;
	
	infile = fopen(lpszInfile,"rb");
	outfile = fopen(lpszOutfile,"wb");
	if(infile && outfile)
	{
		Encode();
		fclose(infile);
		fclose(outfile);
		infile = NULL;
		outfile = NULL;
		remove(lpszInfile);
	}
}

void LZARI::UnCompress(const char *lpszInfile,const char *lpszOutfile)
{
	m_bMem = FALSE;

	infile = fopen(lpszInfile,"rb");
	outfile = fopen(lpszOutfile,"wb");
	if(infile && outfile)
	{
		Decode();
		fclose(infile);
		fclose(outfile);
		infile = NULL;
		outfile = NULL;
		remove(lpszInfile);
	}
}

void LZARI::Compress(const BYTE *pInBuffer,int nInLength,const BYTE *&pOutBuffer ,int &nOutLength)
{
	m_pInBuffer = pInBuffer;
	m_nInLength = nInLength;
	m_nInCur = 0;

//	m_nOutCur = 0;

	m_bMem = TRUE;
	Encode();
	pOutBuffer = &m_OutBuffer[0];
	nOutLength = (int)m_OutBuffer.size();
}

void LZARI::UnCompress(const BYTE *pInBuffer,int nInLength,const BYTE *&pOutBuffer ,int &nOutLength)
{
	m_pInBuffer = pInBuffer;
	m_nInLength = nInLength;
	m_nInCur = 0;

	m_bMem = TRUE;
	Decode();
	pOutBuffer = &m_OutBuffer[0];
	nOutLength = (int)m_OutBuffer.size();
	m_OutBuffer.push_back(0);
}

void LZARI::Release()
{
	if(!m_OutBuffer.empty())
	{
		infile = NULL;
		outfile = NULL;
		textsize = 0;
		codesize = 0;
		printcount = 0;
		low = 0;
		high = Q4;
		value = 0;
		shifts = 0;
		m_bMem = FALSE;
		m_pInBuffer = NULL;
		m_nInLength = 0;
		m_nInCur = 0;
		m_OutBuffer.clear();
		m_nOutLength = 0;
		buffer_putbit = 0;
		mask_putbit = 128;
		buffer_getbit = 0;
		mask_getbit = 0;
	}
}

CString LZARI::Merge(CString bakpath,CString filename1,CString filename2)
{
	SYSTEMTIME t;
	GetSystemTime(&t);
	t.wHour+=8;
	if(t.wHour>=24)
		if(t.wHour==24)
			t.wHour=0;
		else
			t.wHour-=24;
	CString m_sysdate,m_systime;
	m_sysdate.Format("%4d年%02d月%02d日",t.wYear,t.wMonth,t.wDay);
	m_systime.Format("%02d时%02d分%02d秒[教学成绩管理系统]数据备份.blk",t.wHour,t.wMinute,t.wSecond);
	bakpath += m_sysdate;
	bakpath += m_systime;
	CFile f(bakpath,CFile::typeBinary | CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite);
	CFile f1(filename1,CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);
	CFile f2(filename2,CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);
	ULONGLONG length1=f1.GetLength();
	ULONGLONG length2=f2.GetLength();
	CString string1,string2;
	string1.Format("%u",length1);
	string2.Format("%u",length2);
	string1=p_MySetClass->addordelpassword(string1,TRUE);
	string2=p_MySetClass->addordelpassword(string2,TRUE);
	string1+="\r\n";
	string2+="\r\n";
	f.Write(string1,string1.GetLength());
	f.Write(string2,string2.GetLength());
	ULONGLONG lengtha,lengthb;
	int count;
	char p[SIZE];
	if(length1<SIZE)
	{
		count=length1;
		lengtha=1;
	}
	if(length1==SIZE)
	{
		count=SIZE;
		lengtha=1;
	}
	if(length1>SIZE)
	{
		count=SIZE;
		if((length1 % SIZE)==0)
			lengtha=length1/SIZE;
		else
			lengtha=length1/SIZE+1;
	}
	for(ULONGLONG n=0;n<lengtha;n++)
	{
		if(length1>=(n+1)*count)
		{
			f1.Read(p,count);
			f.Write(p,count);
		}
		else
		{
			f1.Read(p,length1-n*count);
			f.Write(p,length1-n*count);
		}
	}
	f1.Close();
	f1.Remove(filename1);
	if(length2<SIZE)
	{
		count=length2;
		lengthb=1;
	}
	if(length2==SIZE)
	{
		count=SIZE;
		lengthb=1;
	}
	if(length2>SIZE)
	{
		count=SIZE;
		if((length2 % SIZE)==0)
			lengthb=length2/SIZE;
		else
			lengthb=length2/SIZE+1;
	}
	for(ULONGLONG n=0;n<lengthb;n++)
	{
		if(length2>=(n+1)*count)
		{
			f2.Read(p,count);
			f.Write(p,count);
		}
		else
		{
			f2.Read(p,length2-n*count);
			f.Write(p,length2-n*count);
		}
	}
	f2.Close();
	f2.Remove(filename2);
	f.Close();
	return bakpath;
}

void LZARI::Partition(CString file,CString file1,CString file2)
{
	CStringFile stringfile(1024);
	stringfile.Open(file,0);
	CString row1,row2;
	stringfile.GetNextLine(row1);
	stringfile.GetNextLine(row2);
	stringfile.Reset();
	stringfile.Close();
	ULONGLONG count=row1.GetLength()+row2.GetLength()+4;
	row1=p_MySetClass->addordelpassword(row1,FALSE);
	row2=p_MySetClass->addordelpassword(row2,FALSE);
	CFile f(file,CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);
	CFile f1(file1,CFile::typeBinary | CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite);
	CFile f2(file2,CFile::typeBinary | CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite);
	f.Seek(count,CFile::begin);
	ULONGLONG length1,length2,sum1,sum2,lengtha,lengthb;
	length1=length2=0;
	int count_size,rowa,rowb;
	rowa=row1.GetLength();
	rowb=row2.GetLength();
	for(int n=0;n<rowa;n++)
	{
		sum1=atol(row1.Left(1));
		row1=row1.Right(row1.GetLength()-1);
		int x=row1.GetLength();
		for(int j=0;j<x;j++)
			sum1*=10;
		length1+=sum1;
	}
	for(int n=0;n<rowb;n++)
	{
		sum2=atol(row2.Left(1));
		row2=row2.Right(row2.GetLength()-1);
		int x=row2.GetLength();
		for(int j=0;j<x;j++)
			sum2*=10;
		length2+=sum2;
	}
	char p[SIZE];
	if(length1<SIZE)
	{
		count_size=length1;
		lengtha=1;
	}
	if(length1==SIZE)
	{
		count_size=SIZE;
		lengtha=1;
	}
	if(length1>SIZE)
	{
		count_size=SIZE;
		if((length1 % SIZE)==0)
			lengtha=length1/SIZE;
		else
			lengtha=length1/SIZE+1;
	}
	for(ULONGLONG n=0;n<lengtha;n++)
	{
		if(length1>=(n+1)*count_size)
		{
			f.Read(p,count_size);
			f1.Write(p,count_size);
		}
		else
		{
			f.Read(p,length1-n*count_size);
			f1.Write(p,length1-n*count_size);
		}
	}
	f1.Close();
	if(length2<SIZE)
	{
		count_size=length2;
		lengthb=1;
	}
	if(length2==SIZE)
	{
		count_size=SIZE;
		lengthb=1;
	}
	if(length2>SIZE)
	{
		count_size=SIZE;
		if((length2 % SIZE)==0)
			lengthb=length2/SIZE;
		else
			lengthb=length2/SIZE+1;
	}
	for(ULONGLONG n=0;n<lengthb;n++)
	{
		if(length2>=(n+1)*count_size)
		{
			f.Read(p,count_size);
			f2.Write(p,count_size);
		}
		else
		{
			f.Read(p,length2-n*count_size);
			f2.Write(p,length2-n*count_size);
		}
	}
	f2.Close();
	f.Close();
	f.Remove(file);
}

⌨️ 快捷键说明

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