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

📄 huffmandlg.cpp

📁 1952年霍夫曼提出了一种构造最佳码的方法
💻 CPP
字号:
// huffmanDlg.cpp : implementation file
//

#include "stdafx.h"
#include "huffman.h"
#include "huffmanDlg.h"
#include "huffmanHead.h"
#include "stdlib.h"

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

#define MaxSize 10000
#define MaxN   1000
#define MaxBit  100
#define MaxValue  10000
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int num[MaxSize];
char form[MaxSize][MaxSize];
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()

/////////////////////////////////////////////////////////////////////////////
// CHuffmanDlg dialog

CHuffmanDlg::CHuffmanDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHuffmanDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHuffmanDlg)
	m_State = _T("");
	m_Output_Back = _T("");
	m_Output = _T("");
	m_Input_Back = _T("");
	m_Input = _T("");
	m_Group = _T("");
	m_Frequent = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHuffmanDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHuffmanDlg)
	DDX_Text(pDX, IDC_STATIC_State, m_State);
	DDX_Text(pDX, IDC_EDIT_Output_Back, m_Output_Back);
	DDX_Text(pDX, IDC_EDIT_Output, m_Output);
	DDX_Text(pDX, IDC_EDIT_Input_Back, m_Input_Back);
	DDX_Text(pDX, IDC_EDIT_Input, m_Input);
	DDX_Text(pDX, IDC_EDIT_Group, m_Group);
	DDX_Text(pDX, IDC_EDIT_Frequent, m_Frequent);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHuffmanDlg, CDialog)
	//{{AFX_MSG_MAP(CHuffmanDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_Begin, OnBUTTONBegin)
	ON_BN_CLICKED(IDC_BUTTON_Back, OnBUTTONBack)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHuffmanDlg message handlers

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

void CHuffmanDlg::OnBUTTONBegin() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);	
	CalcFrequent(m_Input);
	//生成码表
	int flag,n=m_WordForm.GetLength();
	CString temp,temp1,temp2,InputTemp;
	HaffNode *myHaffTree=(HaffNode *)malloc(sizeof(HaffNode) *(2*n+1));
	Code *myHaffCode =(Code *)malloc(sizeof(Code)*n);

	if(n>MaxN)
	{
		MessageBox("Error!");
		exit(0);
	}
    Haffman(num,n,myHaffTree);
	HaffmanCode(myHaffTree,n,myHaffCode);

	m_Group.Format("字符  编码\r\n");
	for(int i=0;i<n;i++)
	{
		temp1.Empty();
		temp2.Empty();
		for(int j=myHaffCode[i].start;j<n;j++)
		{
			temp1.Format("%d",myHaffCode[i].bit[j]);
			temp2+=temp1;
		}	     
		temp.Format("%s   %s",m_WordForm.Mid(i,1),temp2);
		strcpy(form[i],temp2);//保存哈弗曼码表
		m_Group+=temp+"\r\n";
	}
     
	//开始编码
	InputTemp=m_Input;
    for(i=0;i<m_Input.GetLength();i++)
	{
		temp=InputTemp.Mid(i,1);
		flag=m_WordForm.Find(temp);
		temp2.Format("%s",form[flag]);
		m_Output+=temp2;
		//InputTemp=InputTemp.Right(InputTemp.GetLength()-1);
		temp.Empty();
		temp2.Empty();
	}	
	UpdateData(FALSE);
}

void CHuffmanDlg::OnBUTTONBack() 
{
	// TODO: Add your control notification handler code here
	CString output=m_Output,OutputTemp,temp;
	int tag,i=1;
//	for(int i=1;i<=m_Output.GetLength();i++)
//	{
	while(output.GetLength()!=0)
	{
		OutputTemp=output.Mid(0,i);
	    for(int j=0;j<m_WordForm.GetLength();j++)
		{
			tag=strcmp(form[j],OutputTemp);
			if(tag==0)
			{
				temp=m_WordForm.Mid(j,1);
			    m_Output_Back+=temp;
				output=output.Right(output.GetLength()-i);
				i=0;
				break;
			}
			else
				continue;
		}
		i++;
	}
		
//	}
	UpdateData(FALSE);
}

void CHuffmanDlg::CalcFrequent(CString str)
{
	CString temp,temp1,str1=str,strTemp;
	int flag;
	for(int i=0;i<str.GetLength();i++)
	{
		temp=str1.Left(1);
		str1=str1.Right(str1.GetLength()-1);
		flag=strTemp.Find(temp);
		if(flag==-1)//若strTemp中没有该元素,则插入元素并初始化个数为1
		{
			strTemp+=temp;
			num[strTemp.GetLength()-1]=1;
		}
		else //若m_List_word该元素已经存在,则元素个数增1
		{
            num[flag]++;
		}
	}
	m_Frequent.Format("字符频率统计如下:\r\n\r\n字符        次数\r\n");
	for(i=0;i<strTemp.GetLength();i++)
	{
		temp1.Format("%s            %d",strTemp.Mid(i,1),num[i]);
		m_Frequent+=temp1+"\r\n";
	//	m_WordFrequent+=num[i]+"*";
	}
	m_WordForm=strTemp;	
	UpdateData(FALSE);   
}

⌨️ 快捷键说明

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