📄 haffmandlg.cpp
字号:
// HaffmanDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Haffman.h"
#include "HaffmanDlg.h"
#include "fstream.h"
#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()
/////////////////////////////////////////////////////////////////////////////
// CHaffmanDlg dialog
CHaffmanDlg::CHaffmanDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHaffmanDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHaffmanDlg)
m_character = _T("");
m_result = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
for(int i=0; i<100; i++)
{
m_weigh[i] = 0;
}
for(i=0; i<100; i++)
{
m_char[i] = '\0';
}
}
void CHaffmanDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHaffmanDlg)
DDX_Control(pDX, IDC_LIST3, m_code);
DDX_Text(pDX, IDC_CHAR, m_character);
DDX_Text(pDX, IDC_EDIT, m_result);
//}}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)
//}}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
CFileDialog dlg(TRUE, "txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Txt Files|*.txt"); //给文件名编辑框赋初值
CString filepath = "";
if(dlg.DoModal() == IDOK)
{
filepath = dlg.GetPathName();
}
//读文件
m_num = 0;
m_allNum = 0;
int flag;
ifstream fin;
fin.open(filepath);
while(!fin.eof())
{
char c;
fin>>c;
m_allNum += 1;
flag = -1;
for(int i=1; i<=m_num; i++)
{
if(m_char[i] == c)
{
flag = i;
}
}
if(flag==-1)
{
m_char[++m_num] = c;
}
else
{
m_weigh[flag] ++;
}
}
fin.close();
//存储全部字符
fin.open(filepath);
fin>>m_all;
fin.close();
m_code.InsertString(0, "字符 编码");
char code[100];
huffman(tree,m_weigh, m_num);
huffmancode(tree,code,m_num);
tohuffmancode();
UpdateData(false);
}
//解码
void CHaffmanDlg::OnDecode()
{
// TODO: Add your control notification handler code here
int m = 2*m_num - 1;//树根结点
int k=m;
for(int i=0; i<m_character.GetLength(); i++)
{
if(m_character.GetAt(i) == '0')
{
k=tree[k].lchild;
}
else
{
k=tree[k].rchild;
}
if(tree[k].lchild == 0 || tree[k].rchild == 0)
{
m_result += (CString)m_char[k];
k=m;
}
}
UpdateData(false);
}
//生成huffman树
void CHaffmanDlg::huffman(huftree tree[], int *w, int n)
{
int m,i;
if (n<=1) return;
m=2*n-1;
for (i=1;i<=n;i++)
{ tree[i].weight=w[i]; tree[i].parent=0;
tree[i].lchild=0; tree[i].rchild=0; }
for (i=n+1;i<=m;i++)
{ tree[i].weight=0; tree[i].parent=0;
tree[i].lchild=0; tree[i].rchild=0; }
for (i=n+1;i<=m;i++)
{ select(tree, i-1);
tree[s1].parent=i;
tree[s2].parent=i;
tree[i].lchild=s1;
tree[i].rchild=s2;
tree[i].weight =tree[s1]. weight+ tree[s2].weight;
}
}
//找寻parent为0,权最小的两个节点
void CHaffmanDlg::select(huftree tree[], int k)
{
int i;
for (i=1;i<=k && tree[i].parent!=0 ;i++); s1=i;
for (i=1;i<=k;i++)
if (tree[i].parent==0 && tree[i].weight<tree[s1].weight) s1=i;
for (i=1; i<=k ; i++)
if (tree[i].parent==0 && i!=s1) break; s2=i;
for (i=1;i<=k;i++)
if ( tree[i].parent==0 && i!=s1 && tree[i].weight<tree[s2].weight) s2=i;
}
//编码
void CHaffmanDlg::huffmancode(huftree tree[], char code[], int n)
{
int start,c,i,f;
code[n]='\0';
CString str;
for(i=1;i<=n;i++)
{
start=n;
for(c=i,f=tree[i].parent;f!=0;c=f,f=tree[f].parent)
{
if(tree[f].lchild==c)
code[--start]='0';
else
code[--start]='1';
}
strcpy(hc[i],&code[start]);
CString str1,str ="";
str +=(CString)m_char[i]+" ";
str1.Format(hc[i]);
str +=str1;
m_code.InsertString(i, str);
}
}
//针对字符编码
void CHaffmanDlg::tohuffmancode()
{
CString str;
for(int i=0; i<m_allNum-1; i++)
{
for(int j=1; j<=m_num; j++)
{
if(m_all[i] == m_char[j])
{
str.Format(hc[j]);
}
}
m_character += str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -