📄 haffmandlg.cpp
字号:
// HaffmanDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Haffman.h"
#include "HaffmanDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CList <int,int> iProbabilityList;
CList <CString,CString&> strCodeList;
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()
/////////////////////////////////////////////////////////////////////////////
// CHaffmanDlg dialog
CHaffmanDlg::CHaffmanDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHaffmanDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHaffmanDlg)
m_edit1 = _T("");
m_edit2 = _T("");
m_edit3 = _T("");
m_edit4 = _T("");
m_edit5 = _T("");
m_strReport="";
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHaffmanDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHaffmanDlg)
DDX_Text(pDX, IDC_EDIT1, m_edit1);
DDX_Text(pDX, IDC_EDIT2, m_edit2);
DDX_Text(pDX, IDC_EDIT3, m_edit3);
DDX_Text(pDX, IDC_EDIT4, m_edit4);
DDX_Text(pDX, IDC_EDIT5, m_edit5);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHaffmanDlg, CDialog)
//{{AFX_MSG_MAP(CHaffmanDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CODE, OnCode)
ON_BN_CLICKED(IDC_DECODE, OnDecode)
ON_BN_CLICKED(IDC_BROWSE1, OnBrowse1)
ON_BN_CLICKED(IDC_BROWSE2, OnBrowse2)
ON_BN_CLICKED(IDC_BROWSE3, OnBrowse3)
ON_BN_CLICKED(IDC_BROWSE4, OnBrowse4)
ON_BN_CLICKED(IDC_BROWSE5, OnBrowse5)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHaffmanDlg message handlers
BOOL CHaffmanDlg::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 CHaffmanDlg::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 CHaffmanDlg::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 CHaffmanDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CHaffmanDlg::OnCode()
{
// TODO: Add your control notification handler code here
int i,temp;
double k=0;
CString strSource,strDistination="",strtemp;
CStdioFile file;
UpdateData(TRUE);
if(m_edit1=="")
{
MessageBox("请先输入源文件名!");
return ;
}
CodeScan(m_edit1);//统计字符频率,给字符编码
if(!file.Open(m_edit1, CFile::modeRead | CFile::typeText ))
{
MessageBox("打开文件失败!");
return ;
}
while(file.ReadString(strSource))//对源文件进行编码
{
temp=0;
for(i=0;i<strSource.GetLength();i++)
{
strDistination+=strCodeList.GetAt(strCodeList.FindIndex(m_code.Find(strSource.GetAt(i))));
temp+=strCodeList.GetAt(strCodeList.FindIndex(m_code.Find(strSource.GetAt(i)))).GetLength();
}
strDistination+=0x0a;
k+=(double)temp/8;
}
file.Close();
m_strReport+="压缩程度信息报告:\n";
strtemp.Format("源文件字符数为:%d,压缩后的字符数为%d,压缩比为%f\n",m_total,(int)k,m_total/k);
m_strReport+=strtemp;
m_strReport+="码表存储空间报告:\n";
strtemp.Format("压缩后一共需要存储空间:%d bit!",int(k*8));
m_strReport+=strtemp;
if(m_edit2=="")
{
MessageBox("请先输入编码文件名!");
return ;
}
if(!file.Open(m_edit2, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
{
MessageBox("打开文件失败!");
return ;
}
file.WriteString(strDistination);
file.Close();
if(m_edit5=="")
{
MessageBox("请先输入报告文件名!");
return ;
}
if(!file.Open(m_edit5, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
{
MessageBox("打开文件失败!");
return ;
}
file.WriteString(m_strReport);
file.Close();
UpdateData(FALSE);
}
void CHaffmanDlg::OnDecode()
{
// TODO: Add your control notification handler code here
int i,k;
CString strSource,strDistination="",strtemp;
CStdioFile file;
UpdateData(TRUE);
if(m_edit3=="")
{
MessageBox("请先输入源文件名!");
return ;
}
if(!file.Open(m_edit3, CFile::modeRead | CFile::typeText ))
{
MessageBox("打开文件失败!");
return ;
}
while(file.ReadString(strSource))
{
if(strSource.GetLength()==0)
break;
while(strSource.GetLength())
{
strtemp="";
for(i=0;i<strSource.GetLength();i++)
{
strtemp+=strSource.GetAt(i);
if((k=StrCompare(strtemp))!=-1)
{
strDistination+=m_code.GetAt(k);
strSource.Delete(0,i+1);
break;
}
}
}
strDistination+=0x0a;
}
file.Close();
if(m_edit4=="")
{
MessageBox("请先输入编码文件名!");
return ;
}
if(!file.Open(m_edit4, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
{
MessageBox("打开文件失败!");
return ;
}
file.WriteString(strDistination);
file.Close();
UpdateData(FALSE);
}
void CHaffmanDlg::OnBrowse1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileDialog dlgFile(TRUE);
if(IDOK==dlgFile.DoModal())
{
m_edit1=dlgFile.GetPathName();
}
UpdateData(FALSE);
}
void CHaffmanDlg::OnBrowse2()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileDialog dlgFile(FALSE);
if(IDOK==dlgFile.DoModal())
{
m_edit2=dlgFile.GetPathName();
}
UpdateData(FALSE);
}
void CHaffmanDlg::OnBrowse3()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileDialog dlgFile(TRUE);
if(IDOK==dlgFile.DoModal())
{
m_edit3=dlgFile.GetPathName();
}
UpdateData(FALSE);
}
void CHaffmanDlg::OnBrowse4()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileDialog dlgFile(FALSE);
if(IDOK==dlgFile.DoModal())
{
m_edit4=dlgFile.GetPathName();
}
UpdateData(FALSE);
}
void CHaffmanDlg::OnBrowse5()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileDialog dlgFile(FALSE);
if(IDOK==dlgFile.DoModal())
{
m_edit5=dlgFile.GetPathName();
}
UpdateData(FALSE);
}
void CHaffmanDlg::CodeScan(CString sFileName)
{
int i,j,k,temp=0,tempProbability;//,top=0,down=0
CStdioFile file;
CString strSource,strTemp,str;//str strTemp linshi
CList<CString,CString&> codeTempList;
CList<int,int> probabilityTempList;
BOOL flag=TRUE;
m_strReport="";
m_code="";
iProbabilityList.RemoveAll();
m_total=0;
strCodeList.RemoveAll();
if(!file.Open(sFileName, CFile::modeRead | CFile::typeText ))
{
MessageBox("打开文件失败!");
return ;
}
while(file.ReadString(strSource))//统计字符频率
{
for(i=0;i<strSource.GetLength();i++)
{
m_total++;
if((k=m_code.Find(strSource.GetAt(i),0))==-1)
{
m_code.Insert(m_code.GetLength(),strSource.GetAt(i));
codeTempList.AddTail((CString)strSource.GetAt(i));
iProbabilityList.AddTail(1);
}
else
{
iProbabilityList.SetAt(iProbabilityList.FindIndex(k),iProbabilityList.GetAt(iProbabilityList.FindIndex(k))+1);
}
}
}
file.Close();
m_strReport="字符频率统计报告为:\n";
for(i=0;i<m_code.GetLength();i++)
{
m_strReport+=m_code.GetAt(i)+(CString)" : ";
strTemp.Format("%.6f\n",(double)iProbabilityList.GetAt(iProbabilityList.FindIndex(i))/m_total);
m_strReport+=strTemp;
}
for(i=0;i<m_code.GetLength();i++)
probabilityTempList.AddTail(iProbabilityList.GetAt(iProbabilityList.FindIndex(i)));
for(i=0;i<m_code.GetLength();i++)//排序
{
temp=i;
for(j=i+1;j<m_code.GetLength();j++)
if(probabilityTempList.GetAt(probabilityTempList.FindIndex(j))>probabilityTempList.GetAt(probabilityTempList.FindIndex(temp)))
temp=j;
if(temp!=i)
{
k=probabilityTempList.GetAt(probabilityTempList.FindIndex(i));
probabilityTempList.GetAt(probabilityTempList.FindIndex(i))=probabilityTempList.GetAt(probabilityTempList.FindIndex(temp));
probabilityTempList.GetAt(probabilityTempList.FindIndex(temp))=k;
strTemp=codeTempList.GetAt(codeTempList.FindIndex(i));
codeTempList.GetAt(codeTempList.FindIndex(i))=codeTempList.GetAt(codeTempList.FindIndex(temp));
codeTempList.GetAt(codeTempList.FindIndex(temp))=strTemp;
}
}
for(i=0;i<m_code.GetLength();i++)//初始化码表链,为空
strCodeList.AddTail((CString)"");
k=m_code.GetLength()-1;//缩减次数
for(i=0;i<k;i++)//字符编码
{
tempProbability=probabilityTempList.GetTail();
probabilityTempList.RemoveTail();
tempProbability+=probabilityTempList.GetTail();
probabilityTempList.RemoveTail();
for(j=0;j<codeTempList.GetTail().GetLength();j++)
strCodeList.GetAt(strCodeList.FindIndex(m_code.Find(codeTempList.GetTail().GetAt(j),0))).Insert(0,'0');
str=codeTempList.GetTail();
codeTempList.RemoveTail();
for(j=0;j<codeTempList.GetTail().GetLength();j++)
strCodeList.GetAt(strCodeList.FindIndex(m_code.Find(codeTempList.GetTail().GetAt(j),0))).Insert(0,'1');
flag=TRUE;
for(j=0;j<probabilityTempList.GetCount();j++)
{
if(tempProbability>=probabilityTempList.GetAt(probabilityTempList.FindIndex(j)))
{
probabilityTempList.InsertBefore(probabilityTempList.FindIndex(j),tempProbability);
codeTempList.InsertBefore(codeTempList.FindIndex(j),codeTempList.GetTail()+str);
codeTempList.RemoveTail();
flag=FALSE;
break;
}
}
if(flag)
{
probabilityTempList.InsertAfter(probabilityTempList.FindIndex(j-1),tempProbability);
codeTempList.InsertAfter(codeTempList.FindIndex(j-1),codeTempList.GetTail()+str);
codeTempList.RemoveTail();
}
}
m_strReport+="编码报告为:\n";
for(i=0;i<m_code.GetLength();i++)
{
m_strReport+=m_code.GetAt(i)+(CString)" : ";
m_strReport+=strCodeList.GetAt(strCodeList.FindIndex(i));
m_strReport+="\n";
}
}
int CHaffmanDlg::StrCompare(CString str)//查找strCodeList中是否有str串,如有则返回序号,否则返回-1
{
int i;
for(i=0;i<strCodeList.GetCount();i++)
if(str==strCodeList.GetAt(strCodeList.FindIndex(i)))
return i;
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -