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

📄 allshangdlg.cpp

📁 实现编码理论中各种熵的计算
💻 CPP
字号:
// AllShangDlg.cpp : implementation file
//
//此程序适用于输入为两个符号,输出为三个符号的情况

#include "stdafx.h"
#include "AllShang.h"
#include "AllShangDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CAllShangDlg dialog

CAllShangDlg::CAllShangDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAllShangDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAllShangDlg)
	m_fX0 = 0.0f;
	m_fX1 = 0.0f;
	m_fY0X0 = 0.0f;
	m_fY1X1 = 0.0f;
	m_fYX0 = 0.0f;
	m_fYX1 = 0.0f;
	m_dAverInfo = 0.0;
	m_dComResult = 0.0;
	m_dLoseResult = 0.0;
	m_dNosieResult = 0.0;
	m_dSorResult = 0.0;
	m_dX0Y = 0.0;
	m_dX0Y0 = 0.0;
	m_dX1Y = 0.0;
	m_dX1Y1 = 0.0;
	m_dDesResult = 0.0;
	m_dX0Y1 = 0.0;
	m_dX1Y0 = 0.0;
	m_fY0X1 = 0.0f;
	m_fY1X0 = 0.0f;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAllShangDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAllShangDlg)
	DDX_Text(pDX, IDC_EDITX0, m_fX0);
	DDX_Text(pDX, IDC_EDITX1, m_fX1);
	DDX_Text(pDX, IDC_EDITY0X0, m_fY0X0);
	DDX_Text(pDX, IDC_EDITY1X1, m_fY1X1);
	DDX_Text(pDX, IDC_EDITYX0, m_fYX0);
	DDX_Text(pDX, IDC_EDITYX1, m_fYX1);
	DDX_Text(pDX, IDC_EAVERAGEINFO, m_dAverInfo);
	DDX_Text(pDX, IDC_ECOMBINERESULT, m_dComResult);
	DDX_Text(pDX, IDC_ELOSERESULT, m_dLoseResult);
	DDX_Text(pDX, IDC_ENOISERESULT, m_dNosieResult);
	DDX_Text(pDX, IDC_ESOURCERESULT, m_dSorResult);
	DDX_Text(pDX, IDC_EDITX0ANDY, m_dX0Y);
	DDX_Text(pDX, IDC_EDITX0ANDY0, m_dX0Y0);
	DDX_Text(pDX, IDC_EDITX1ANDY, m_dX1Y);
	DDX_Text(pDX, IDC_EDITX1ANDY1, m_dX1Y1);
	DDX_Text(pDX, IDC_EDESRESULT, m_dDesResult);
	DDX_Text(pDX, IDC_EDITX0ANDY1, m_dX0Y1);
	DDX_Text(pDX, IDC_EDITX1ANDY0, m_dX1Y0);
	DDX_Text(pDX, IDC_EDITY0X1, m_fY0X1);
	DDX_Text(pDX, IDC_EDITY1X0, m_fY1X0);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAllShangDlg, CDialog)
	//{{AFX_MSG_MAP(CAllShangDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTONCALCULATE, OnButtoncalculate)
	ON_BN_CLICKED(IDC_BUTTONCLEAR, OnButtonclear)
	ON_BN_CLICKED(IDC_BEXIT, OnBexit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAllShangDlg message handlers

BOOL CAllShangDlg::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 CAllShangDlg::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 CAllShangDlg::OnPaint() 
{
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(&rect);
	CBrush* br=new CBrush(RGB(232,208,255));
	if(br)
	{
		dc.FillRect(&rect,br);
		delete br;
	}
	
	if (IsIconic())
	{
		 // 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);

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

void CAllShangDlg::OnButtoncalculate() 
{
	// TODO: Add your control notification handler code here
	double X[2],Y[3],XY[2][3],YORX[2][3];
	int i,j;
	double sum1=0.0;
	BOOL InputCollect=true;

	UpdateData(true);
	X[0]=m_fX0;
	X[1]=m_fX1;

	YORX[0][0]=m_fY0X0;//p(y=0|x=0)
	YORX[0][1]=m_fY1X0;//p(y=1|x=0)
	YORX[0][2]=m_fYX0;//p(y=?|x=0)
	YORX[1][0]=m_fY0X1;//p(y=0|x=1)
	YORX[1][1]=m_fY1X1;//p(y=1|x=1)
	YORX[1][2]=m_fYX1;//p(y=?|x=1)

	//判断所输入的值是否合理
	for(i=0;i<2;i++)
		sum1+=X[i];
	
	for(i=0;i<2;i++)
	{
		if(X[i]<0||X[i]>1)
		{
			AfxMessageBox("输入概率应是0—1之间的数",MB_ICONWARNING);
			InputCollect=false;
			OnButtonclear();
			break;
		}
		else if(sum1!=1)
		{
			AfxMessageBox("所有符号的输入概率之和应为1",MB_ICONWARNING);
			InputCollect=false;
			OnButtonclear();
			break;
		}
	
		for(j=0;j<3;j++)
		{
			if(YORX[i][j]<0||YORX[i][j]>1)
			{
				AfxMessageBox("条件概率应是0—1之间的数",MB_ICONWARNING);
				InputCollect=false;
				OnButtonclear();
				break;
			}
		}
		
		if((YORX[0][0]+YORX[0][1]+YORX[0][2])!=1||(YORX[1][0]+YORX[1][1]+YORX[1][2])!=1)
		{
			AfxMessageBox("在同一条件下的概率之和应为1",MB_ICONWARNING);
			InputCollect=false;
			OnButtonclear();
			break;
		}	
	}

	//若输入均无误的情况下
	if(InputCollect==true)
	{
		//计算出X、Y的所有联合概率,并显示出来
		for(i=0;i<2;i++)
		{
			for(j=0;j<3;j++)
			{
				XY[i][j]=X[i]*YORX[i][j];
			}
		}
		m_dX0Y0=XY[0][0];//p(x=0,y=0)
		m_dX0Y1=XY[0][1];//p(x=0,y=1)
		m_dX0Y=XY[0][2];//p(x=0,y=?)
		m_dX1Y0=XY[1][0];//p(x=1,y=0)
		m_dX1Y1=XY[1][1];//p(x=1,y=1)
		m_dX1Y=XY[1][2];//p(x=1,y=?)

		//计算Y的概率分布
		Y[0]=XY[0][0]+XY[1][0];//Y=0
		Y[1]=XY[1][1]+XY[0][1];//Y=1
		Y[2]=XY[0][2]+XY[1][2];//Y=?
		
		//计算信源熵H(X)
		for(i=0;i<2;i++)
		{	
			m_dSorResult+=-X[i]*(log(X[i])/log(2));
		}
	
		//计算Y的熵H(Y)
		for(i=0;i<3;i++)
			m_dDesResult+=-Y[i]*(log(Y[i])/log(2));
	
		//计算噪声熵H(Y|X)
		for(i=0;i<2;i++)
		{
			for(j=0;j<3;j++)
			{
				if(YORX[i][j]!=0)
				{
					m_dNosieResult+=-XY[i][j]*(log(YORX[i][j])/log(2));
				}
			
			}
		}

		//计算联合熵H(XY)=H(X)+H(Y|X)
		m_dComResult=m_dSorResult+m_dNosieResult;

		//计算损失熵H(X|Y)=H(XY)-H(Y)
		m_dLoseResult=m_dComResult-m_dDesResult;

		//计算平均互信息量I(X;Y)=H(X)-H(X|Y)
		m_dAverInfo=m_dSorResult-m_dLoseResult;
	}

	UpdateData(false);
}

void CAllShangDlg::OnButtonclear() 
{
	// TODO: Add your control notification handler code here
	m_fX0 = 0.0f;
	m_fX1 = 0.0f;
	m_fY0X0 = 0.0f;
	m_fY1X1 = 0.0f;
	m_fYX0 = 0.0f;
	m_fYX1 = 0.0f;
	m_dAverInfo = 0.0;
	m_dComResult = 0.0;
	m_dLoseResult = 0.0;
	m_dNosieResult = 0.0;
	m_dSorResult = 0.0;
	m_dX0Y = 0.0;
	m_dX0Y0 = 0.0;
	m_dX1Y = 0.0;
	m_dX1Y1 = 0.0;
	m_dDesResult = 0.0;
	m_dX0Y1 = 0.0;
	m_dX1Y0 = 0.0;
	m_fY0X1 = 0.0f;
	m_fY1X0 = 0.0f;	

	UpdateData(false);
}

void CAllShangDlg::OnBexit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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