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

📄 bayesmfcdlg.cpp

📁 针对Bayes网络中有关因果推理的方法设计与实现
💻 CPP
字号:
// BayesMFCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BayesMFC.h"
#include "BayesMFCDlg.h"
//#include "math.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()

/////////////////////////////////////////////////////////////////////////////
// CBayesMFCDlg dialog

CBayesMFCDlg::CBayesMFCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBayesMFCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBayesMFCDlg)
	m_inStr = _T("");
	m_outStr = _T("");
	m_result = _T("");
	m_state = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBayesMFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBayesMFCDlg)
	DDX_Text(pDX, IDC_EDIT_IN, m_inStr);
	DDX_Text(pDX, IDC_EDIT_OUT, m_outStr);
	DDX_Text(pDX, IDC_EDIT_RESULT, m_result);
	DDX_Text(pDX, IDC_EDIT_STATE, m_state);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBayesMFCDlg, CDialog)
	//{{AFX_MSG_MAP(CBayesMFCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTONI_INIT, OnButtoniInit)
	ON_BN_CLICKED(IDC_BUTTON_WORK, OnButtonWork)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBayesMFCDlg message handlers

BOOL CBayesMFCDlg::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
	char* fileName="discursion.in";
	input.open(fileName);
	if(!input)
	{
		MessageBox("can't open file discursion.in!");
		OnCancel();
	}
	GetDlgItem(IDC_BUTTON_WORK)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_IN)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_OUT)->EnableWindow(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CBayesMFCDlg::OnButtoniInit() 
{
	// TODO: Add your control notification handler code here	
	int i, j, k, p, q;
	char ch;
	BOOL positive;
	input>>nodeNum>>testnodeNum;//testnodeNum有待删除
	if(nodeNum==0)
	{
		MessageBox("文件处理完毕!");
		OnCancel();
	}
	m_state="条件:";
	for(i=0; i<nodeNum-1; i++)
	{
		input>>condition[i].letter;//条件
		m_state+=condition[i].letter;
		m_state+=' ';
	}
	m_state+="\r\n";
	input>>result.letter;//结果
	m_state+="结果:";
	m_state+=result.letter;
	UpdateData(FALSE);
	j=power(2, nodeNum-1);
	//原init代码-------------------------
	for(i=0; i<nodeNum-1; i++)
	{//读入原始概率
		input>>ch;
		for(p=0; p<nodeNum; p++)
			if(condition[p].letter==ch) break;
		input>>pro11[p];
	}
	for(i=0; i<j; i++)
	{//读入条件概率
		for(k=0; k<nodeNum-1; k++)
		{
			input>>ch;
			if(ch=='~')
			{
				input>>ch;
				for(p=0;p<nodeNum;p++)
					if(condition[p].letter==ch) break;
				condition[p].index=0;
			}
			else
			{
				for(p=0;p<nodeNum;p++)
					if(condition[p].letter==ch) break;
				condition[p].index=1;
			}
		}
		input>>ch;
		positive=TRUE;
		if(ch=='~')
		{
			positive=FALSE;
			input>>ch;
		}
		q=cal();
		input>>pro22[q];
		if(!positive) pro22[q]=-pro22[q];		
	}
	GetDlgItem(IDC_BUTTON_WORK)->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_IN)->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_OUT)->EnableWindow(TRUE);
}

long CBayesMFCDlg::power(long a, long b)
{
	long sum=1;
	while(b--)
		sum*=a;
	return sum;
}

long CBayesMFCDlg::cal()
{
	long sum=0;
	for(int i=0;i<nodeNum-1;i++)
	{
		sum*=2;
		sum+=condition[i].index;
	}
	return sum;
}



void CBayesMFCDlg::OnButtonWork() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_inStr.IsEmpty() || m_outStr.IsEmpty())return;
	int i, k, j;
	BOOL positive=TRUE;
	char ch;
	CString tempStr;
	for(i=0; i<nodeNum-1; i++)
		condition[i].index=-1;
	memset(mark,0,sizeof(mark));

	char buf[60];
	strcpy(buf, m_inStr);
	for(k=0; buf[k]!=0; k++)
	{
		if(buf[k]==' ')continue;
		if(buf[k]=='~'){ positive=FALSE; continue;}
		for(j=0; j<nodeNum-1; j++)
			if(condition[j].letter==buf[k])break;
		if(positive)
		{
			condition[j].index=1;
			mark[j].pos=TRUE;
			mark[j].iscon=TRUE;
		}
		else
		{
			condition[j].index=0;
			mark[j].pos=FALSE;
			mark[j].iscon=TRUE;
		}
		positive=TRUE;
	}
	strcpy(buf, m_outStr);
	positive=TRUE;
	for(k=0; buf[k]!=0; k++)
	{
		if(buf[k]==' ')continue;
		if(buf[k]=='~'){ positive=FALSE; continue;}
		ch=buf[k];
	}
	tempStr="P(";
	if(!positive)tempStr+="~";
	tempStr+=ch; tempStr+="|";
	BOOL first=TRUE;
	for(k=0; k<nodeNum-1; k++)
	{
		if(condition[k].index!=-1)
		{
			if(first)first=FALSE;
			else tempStr+=",";
			if(condition[k].index==0)
				tempStr+="~";
			tempStr+=condition[k].letter;
		}
	}
	tempStr+=")=";
	proba=0;
	backtrack(0);
	if(!positive)proba=1-proba;
	m_result=tempStr;
	tempStr.Format("%lf", proba);
	m_result+=tempStr;
	UpdateData(FALSE);
}

void CBayesMFCDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	input.close();
	CDialog::OnCancel();
}

void CBayesMFCDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnClose();
}

void CBayesMFCDlg::backtrack(int start)
{
	int q;
	double i,k;
	int j;
	if(start==nodeNum)				//当父节点全部出现时,计算
	{
		q=cal();
		i=1;
		for(j=0;j<nodeNum-1;j++)
		{
			if(!mark[j].iscon)
			{
				k=pro11[j];
				if(condition[j].index==0)
					k=1-k;
				i*=k;
			}
		}
		i*=pro22[q];
		proba+=i;
		return;
	}
	if(condition[start].index!=-1)
		backtrack(start+1);
	else
	{
		condition[start].index=0;
		backtrack(start+1);
		condition[start].index=1;
		backtrack(start+1);
	}
}

⌨️ 快捷键说明

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