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

📄 morse码优化dlg.cpp

📁 信息论:MFC实现MORSE码重组优化减少冗余度
💻 CPP
字号:
// morse码优化Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "morse码优化.h"
#include "morse码优化Dlg.h"
#include "headfile.h"
#include "math.h"

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

void inorder(node *t);
int codelength(node *t);
void PreOrder(node *t);//先根遍历法生成编码
int findletter(CString str,node *t);
void displaycode();
int m;CString mm;
double L,e;
double HS=4.11868;//信源熵

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMorseDlg dialog

CMorseDlg::CMorseDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMorseDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMorseDlg)
	m_edit1 = _T("");
	m_edit2 = _T("");
	m_edit3 = _T("");
	m_edit4 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMorseDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMorseDlg)
	DDX_Control(pDX, IDC_LIST1, m_list1);
	DDX_Control(pDX, IDC_LIST2, m_list);
	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);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMorseDlg, CDialog)
	//{{AFX_MSG_MAP(CMorseDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMorseDlg message handlers

BOOL CMorseDlg::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);
		}
		
	}
	L=0;

    //显示字母出现概率表
	for(int i=0;i<26;i++)
	 {
		ss.Format("%8f",pa[i]);
		ss.Insert(0,"    ");
		ss.Insert(0,letter[i]);
		ss.Insert(0," ");				
		m_list.InsertString(-1,ss);
	 }
	
	//创建HUFFMAN树
	creattree();
	//先根遍历法编码
	PreOrder(root);
	//输出编码及平均码长
	for(int j=0;j<26;j++)
	{
		mm=letter.GetAt(j);		
		m=findletter(mm,root);
		if(m!=0) 
		{
				mm.Format("%d",current->code.GetLength());
		        m_list1.InsertString(-1,current->letter+"    "+current->code);
		        L+=current->p*codelength(current);

		}
		
	}
	mm.Format("%f",L);
	mm.Insert(0,"平均码长为:");
    CWnd *pWnd=GetDlgItem(IDC_STATIC1);
	pWnd->SetWindowText(mm);
	//计算编码效率
	e=HS/L;
	mm.Format("%f",e);
	mm.Insert(0,"编码效率为:");
	CWnd *pWnd1=GetDlgItem(IDC_STATIC2);
	pWnd1->SetWindowText(mm);

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

//编码
void CMorseDlg::OnButton1() 
{
	CString str="",str1="";
	UpdateData(TRUE);
	m_edit1.MakeLower();
	for(int i=m_edit1.GetLength()-1;i>=0;i--)
	{
		str=m_edit1.GetAt(i);
		if(str<'a'||str>'z')
		{
			AfxMessageBox("对不起,你的输入有错,输入必须是字母,单击确定重新输入!");
			m_edit1="";
			m_edit3="";
			UpdateData(FALSE);
			return;
		}
		else
		{
			findletter(str,root);
			str1.Insert(0,current->code);
		}
	}
	m_edit3=str1;
	UpdateData(FALSE);
	//AfxMessageBox(str1);

}


//先根遍历法生成编码
void PreOrder(node *t)//先序遍历二叉树并输出
{
	if(t)
	{
		//处理
		if(t!=root)
		{			
			if(t->parent->left==t)
				t->code=t->parent->code+".";
			else
				t->code=t->parent->code+"-";
		}


		if(t->left)
		{
		  PreOrder(t->left);
		  if(t->right)
			 PreOrder(t->right);
		}
		
	}


}








//计算码长
int codelength(node *t)
{
	int length=0;CString tt;
	for (int i=0;i<t->code.GetLength();i++)
	{
		tt=t->code.GetAt(i);
		if(tt==".")
			length+=1;
		else
			length+=3;
	}

	return length;
}



//按字母查找结点
int findletter(CString str,node *t)
{

	if(t!=NULL)
	{
		findletter(str,t->left);
		//处理
		if(t->letter==str)
		{
			current=t;return 1;
		}
		findletter(str,t->right);
	}
	
	
	
}






//编码框重置
void CMorseDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	m_edit1="";
	m_edit3="";
	UpdateData(FALSE);
	
}


//译码
void CMorseDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	CString str="",str1="";
	int n,length;
	UpdateData(TRUE);
	for(int i=0;i<m_edit2.GetLength();i++)
	{
		str=m_edit2.GetAt(i);
		if(str!='.'&&str!='-')
		{
			AfxMessageBox("对不起,你的输入有错,输入必须是编码字符'.'或'-',单击确定重新输入!");
			m_edit2="";
			m_edit4="";
			UpdateData(FALSE);
			return;
		}

	}
	//初始化
 	current=root;
	length=m_edit2.GetLength();
	n=0;	m_edit4="";
	//开始译码
   while(n<length)
	{
		while(current->letter.GetLength()>1)
		{
			str=m_edit2.GetAt(n);
			if(str=='.')
				current=current->left;
			else if(str=='-')
				current=current->right;
			if((n==length-1)&&(current->letter.GetLength()>1))
			{
				//报错
				AfxMessageBox("输入编码错误!");
				return;
			}

			n++;
		}
		m_edit4+=current->letter;
		current=root;
		UpdateData(FALSE);
		
	   
	   
	}

    return;

	m_edit4=str1;
	UpdateData(FALSE);
	//AfxMessageBox(str1);
	
}

//重置译码框
void CMorseDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	m_edit2="";
	m_edit4="";
	UpdateData(FALSE);
	
}

⌨️ 快捷键说明

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