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

📄 压缩dlg.cpp

📁 这是一个MFC的压缩,解压小程序,可以对各种文件进行压缩
💻 CPP
字号:
// 压缩Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "压缩.h"
#include "压缩Dlg.h"
#include <string>
#include <fstream>
#include <map>
using std::string;
using std::ifstream;
using std::ofstream;
using std::map;

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

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
string file(string name,string last)    //产生输出文件的后缀名
{
	int pos=0;
	while(pos<name.size())
	{
		if(name[pos]=='.') break;
		else ++pos;
	}
	name=name.substr(0,pos+1);
	name+=last;
	return name;
}
void compress(const char* filename)
{
	ifstream in(filename);
	ofstream out(file(filename,"nc").c_str());
	//string expand;            //应该记录文件扩展名,在输出文件的某处应该记录
	string stream;
	in>>stream;
	map< string , int > msi;     //本应该建立一个更大的字典集  比如一个ASCII集合
	msi["a"]=0; msi["b"]=1;
	msi["c"]=2; msi["d"]=3;
	msi["e"]=4; msi["f"]=5;
	msi["g"]=6; msi["h"]=7;
	msi["i"]=8; msi["j"]=9;
	msi["k"]=10; msi["l"]=11;
	msi["m"]=12; msi["n"]=13;
	msi["o"]=14; msi["p"]=15;
	msi["q"]=16; msi["r"]=17;
	msi["s"]=18; msi["t"]=19;
	msi["u"]=20; msi["v"]=21;
	msi["w"]=22; msi["x"]=23;
	msi["y"]=24; msi["z"]=25;	
	int i=0,k=2;               
	while(i<stream.size())
	{
		std::map<string,int>::iterator it=msi.begin();
		std::map<string,int>::iterator at;
		int max=(it->first).size();
		while(it!=msi.end())
		{
			int pos=stream.find(it->first,i);
			if(pos==i)
			{
				if(max<(it->first).size()) max=(it->first).size();
				at=it;
			}
			++it;
		}
		out<<(at->second)<<" ";          //最后在每个输出的时候留一个空格,以便解压
		string temp=stream.substr(i,max+1);
		msi[temp]=k;
		++k;
		i+=max;
	}
	out.close();
	out.clear();
}

void decompression(const char* filename)
{
	ifstream in(filename);
	//string expand;           ofstream out(file(filename,expand).c_str())
	ofstream out(file(filename,"txt").c_str());
	map<int,string> mis;
	mis[0]="a"; mis[1]="b";
	mis[2]="c"; mis[3]="d";
	mis[4]="e"; mis[5]="f";
	mis[6]="g"; mis[7]="h";
	mis[8]="i"; mis[9]="j";
	mis[10]="k"; mis[11]="l";
	mis[12]="m"; mis[13]="n";
	mis[14]="o"; mis[15]="p";
	mis[16]="q"; mis[17]="r";
	mis[18]="s"; mis[19]="t";
	mis[20]="u"; mis[21]="v";
	mis[22]="w"; mis[23]="x";
	mis[24]="y"; mis[25]="z";
	int prev_code,cur_code;
	in>>prev_code;
	out<<mis[prev_code];
	int k=2;              // k refer to map_key;
    while(in>>cur_code)
	{
		if(mis.find(cur_code)!=mis.end())
		{
			out<<mis[cur_code];
            mis[k++]=mis[prev_code]+mis[cur_code].substr(0,1);
		}
		else {
			string temp=mis[prev_code];
			temp+=mis[prev_code].substr(0,1);
			out<<temp;
			mis[k++]=temp;	
		}
		prev_code=cur_code;
	}
	out.close();
	out.clear();
}
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

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

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_JIEYA, OnJieya)
	ON_BN_CLICKED(IDC_YASUO, OnYasuo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

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

	// 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
}

// 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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyDlg::OnJieya() 
{
	CFileDialog dlg(TRUE);
	if(dlg.DoModal()==IDOK)
	{
		FileName=dlg.GetPathName();
		if(dlg.GetFileExt()!="nc")
			::MessageBox(m_hWnd,"此文件无法识别","Error",MB_OK);
		else decompression((const char*)(FileName));
	}
// TODO: Add your control notification handler code here
	
}

void CMyDlg::OnYasuo() 
{
	CFileDialog dlg(TRUE);
	if(dlg.DoModal()==IDOK)
	{
		FileName=dlg.GetPathName();
		if(dlg.GetFileExt()!="txt")
			::MessageBox(m_hWnd,"此文件无法识别","Error",MB_OK);
		else compress((const char*)FileName);
	}
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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