?? 壓縮dlg.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 + -