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

📄 compressdlg.cpp

📁 程序:将重复的子数列折叠起来。就是连续重复数据的压缩
💻 CPP
字号:
// CompressDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Compress.h"
#include "CompressDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompressDlg dialog

CCompressDlg::CCompressDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCompressDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCompressDlg)
	m_resource = _T("");
	m_result = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCompressDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCompressDlg)
	DDX_Text(pDX, IDC_RESOURCE, m_resource);
	DDX_Text(pDX, IDC_RESULT, m_result);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCompressDlg, CDialog)
	//{{AFX_MSG_MAP(CCompressDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompressDlg message handlers

BOOL CCompressDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCompressDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCompressDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCompressDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

CString CCompressDlg::Compress(CString strsource)
{
	char src[MAXNUM][MAXSIZE]={0};
	char newsrc[MAXNUM][MAXSIZE]={0};
	int i=0,j=0,k,index,m=0,t,s;
	while(strcmp(strsource,"")!=0)
	{//将输入的一串字符串形式的数列放入一个二维数组中src[MAXNUM][MAXSIZE],
	 //MAXSIZE是数列中数字的最大长度或重复折叠需要的首尾标记字符的最大长度
		index=strsource.Find(' ');
		if(index!=-1)
		{
			strcpy(src[i++],strsource.Left(index));
			strsource=strsource.Mid(index+1);
		}
		else
		{
			strcpy(src[i++],strsource);
			strsource="";
		}
	}
	while(j<(i-1))
	{//从第一个数开始取,分别和后面的所有数比较,没有相等的,再取第二个数,与后面所有数比较。。。
	 //当第m个数和第n个数相等时(m<=n),再将这第m个数到第n-1个数分别依次和第n个数到第2n-m-1个数比较(
	 //当全部相等时才算重复,否则将m++,重复上面操作,直到数字输出完了为止。。。
		int find=0,num=1;
		for(k=j+1;k<i;k++)
		{
			if(strcmp(src[j],src[k])!=0)//从前往后找
				continue;
			else
			{//找到第一个重复的数
				find=1;//标记找到两个相等的数
				t=j;//两个相等数左边一个数位置
				s=k;//两个相等数右边一个数位置
				break;
			}
		}
		if(!find)
			strcpy(newsrc[m++],src[j]);//没找到某个数的重复数,考出该数
		else
		{//找到某个数的重复数
			while(t<k)
			{//循环比较两个相等数之间的数
				if(strcmp(src[t++],src[s++])!=0)
				{
					if(num==1)
						strcpy(newsrc[m++],src[j]);//没有找到两个相等数左边数开始的子数列
					else
					{//拷出多次重复的子数列及其起始标记
						CString strnum;
						strnum.Format("%d",num);
						strcpy(newsrc[m++],"0xFE");
						strcpy(newsrc[m++],strnum);
						while(j<k)
						{
							strcpy(newsrc[m++],src[j++]);
						}
						j--;
						strcpy(newsrc[m++],"0xFD");
					}
					break;
				}
				if(t==k)
				{//执行一次,表示找到一次重复子数列
					num++;
					j=k;
					k=s;
				}
			}
		}
		j++;
	}
	strcpy(newsrc[m],src[j]);//拷出最后一个孤独数
	CString strs="";
	for(int n=0;n<=m;n++)
	{//将折叠处理后的数列和标记全部复制到一个字符串中,用于显示出来
		strs+=newsrc[n];
		if(n!=m)
			strs+=" ";
	}
	return strs;
}

void CCompressDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData();
	m_result=Compress(m_resource);
	UpdateData(FALSE);
	//CDialog::OnOK();
}

⌨️ 快捷键说明

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