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

📄 dlgrletext.cpp

📁 对文本进行游程压缩
💻 CPP
字号:
// DlgRLEText.cpp : implementation file
//

#include "stdafx.h"
#include "Compress.h"
#include "DlgRLEText.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgRLEText dialog


CDlgRLEText::CDlgRLEText(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgRLEText::IDD, pParent)
{

	//{{AFX_DATA_INIT(CDlgRLEText)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgRLEText::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgRLEText)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgRLEText, CDialog)
	//{{AFX_MSG_MAP(CDlgRLEText)
	ON_BN_CLICKED(IDC_BUTTON_Compress, OnBUTTONCompress)
	ON_BN_CLICKED(IDC_BUTTON_Uncompress, OnBUTTONUncompress)
	ON_BN_CLICKED(IDC_BUTTON_Compress2, OnBUTTONCompress2)
	ON_EN_CHANGE(IDC_EDIT_Original, OnChangeEDITOriginal)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgRLEText message handlers
BOOL CDlgRLEText::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	this->SetDlgItemText(IDC_EDIT_Original,"1\r\n22\r\n333\r\n4444\r\n55555\r\n666666AAAAAAA Wish you a good luck! IIIIIIII");	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void CDlgRLEText::OnBUTTONCompress() 
{
	// TODO: Add your control notification handler code here
	/*************************************************************
	//【获取原文本】
	//【压缩文本】
	//初始化计数变量
	//如果文本没有结束{
		//读取当前字符
		//如果当前字符是第一个字符,将比较字符设为当前字符,continue
		//如果当前字符等于比较字符,重复次数加一,continue
		//如果重复次数小于4,将比较字符写入压缩流:R+1次; 重复次数置零,将比较字符设为当前字符,continue
		//将重复字符,以三字节格式写入压缩流;重复次数置零,将比较字符设为当前字符,continue
	//}
	//【设置压缩文本,激活解压按钮】
	**************************************************************/

	//【获取原文本】
	CString	sOriginalText,s;
	this->GetDlgItemText(IDC_EDIT_Original,sOriginalText);
	strcpy( m_cOriginalText,(LPSTR)(LPCTSTR)sOriginalText );  
	//【压缩文本】
	//初始化计数变量
	int	R=0;
	int C=0;
	int CC=0;
	char	CH;
	char	SH;

	//如果文本没有结束{
	do{
		s.Format("%c",m_cOriginalText[C]);
		//::AfxMessageBox(s);
		//读取当前字符
		CH=m_cOriginalText[C];
		C++;
		//如果当前字符是第一个字符,将比较字符设为当前字符,continue
		if (C==1){
			SH=CH;
			continue;
		}
		//如果当前字符等于比较字符,重复次数加一,continue
		if (CH==SH){
			R++;
			continue;
		}
		//如果重复次数小于4,将比较字符写入压缩流:R+1次; 重复次数置零,将比较字符设为当前字符,continue
		if (R<4){
			for(int i=0; i<=R; i++){
				m_cCompressText[CC]=SH;
				CC++;
			}
			R=0;
			SH=CH;
			continue;
		}
		//将重复字符,以三字节格式写入压缩流;重复次数置零,将比较字符设为当前字符,continue
		m_cCompressText[CC++]='@';
		char cNum=(char)(R+1);
		m_cCompressText[CC++]=cNum;
		m_cCompressText[CC++]=SH;
		R=0;
		SH=CH;
	//}
	} while( CH!='\0' );
	C--;

	m_cCompressText[CC]='\0';

	s.Format(" 原字符数=%d\n 压缩字符数=%d\n 压缩比=%f",C,CC,(C+0.0)/CC);
	::AfxMessageBox(s);


	//【设置压缩文本,激活解压按钮】
	this->SetDlgItemText(IDC_EDIT_Compress,CString(this->m_cCompressText));
	CButton	*pBtn;
	pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Uncompress);
	pBtn->EnableWindow(TRUE);		//bAble为TRUE或FALSE
	pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Compress2);
	pBtn->EnableWindow(TRUE);		//bAble为TRUE或FALSE
	
}

void CDlgRLEText::OnBUTTONCompress2() 
{
	// TODO: Add your control notification handler code here
	//将压缩文本中的@后的一字节计数用数字串替换
	int	iAt,iStart=0;
	char	cNum;
	CString csCompressText,csNum;
	csCompressText.Format("%s",m_cCompressText);
	while( (iAt=csCompressText.Find('@',iStart))>=0 ){
		cNum=csCompressText.GetAt(iAt+1);
		csNum.Format("[%d]",int(cNum));
		csCompressText.Delete(iAt+1);
		csCompressText.Insert(iAt+1,csNum);
		iStart=iAt+1;
	}

	this->SetDlgItemText(IDC_EDIT_Compress,csCompressText);
}

void CDlgRLEText::OnChangeEDITOriginal() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	CButton	*pBtn;
	pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Compress2);
	pBtn->EnableWindow(FALSE);		//bAble为TRUE或FALSE
	pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Uncompress);
	pBtn->EnableWindow(FALSE);		//bAble为TRUE或FALSE
	
}

void CDlgRLEText::OnBUTTONUncompress() 
{
	// TODO: Add your control notification handler code here
	/*****************************************************************
	如果压缩文本中还有字符{
		读取当前字符;
		如果当前字符等于'@',读取重复字符RC以及计数N,将重复字符写入解压流:N次,continue;
		将当前字符写入解压流;
	}
	*****************************************************************/
	int iC=0,iU=0;
	int	iNum;
	char cCur,cR;
	//如果压缩文本中还有字符{
	while( m_cCompressText[iC]!='\0' ){
		//读取当前字符;
		cCur=m_cCompressText[iC];
		iC++;
		//如果当前字符等于'@',读取重复字符cR以及计数iNum,将重复字符写入解压流:iNum次,continue;
		if (cCur=='@'){
			cR=m_cCompressText[iC+1];
			iNum=(int)m_cCompressText[iC];
			for(int i=0; i<iNum; i++){
				m_cUncompressText[iU++]=cR;
			}
			iC+=2;
			continue;
		}
		//将当前字符写入解压流;
		m_cUncompressText[iU++]=cCur;
	//}
	}
	m_cUncompressText[iU]='\0';

	this->SetDlgItemText(IDC_EDIT_Uncompress,CString(this->m_cUncompressText));
}


⌨️ 快捷键说明

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