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

📄 感知器dlg.cpp

📁 模式识别分类器
💻 CPP
字号:
// 感知器Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "感知器.h"
#include "感知器Dlg.h"
#include <fstream.h>

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

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_userx1 = 0.0f;
	m_userx2 = 0.0f;
	m_userx3 = 0.0f;
	m_userx4 = 0.0f;
	m_show = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Text(pDX, IDC_USERX1_EDIT, m_userx1);
	DDX_Text(pDX, IDC_USERX2_EDIT, m_userx2);
	DDX_Text(pDX, IDC_USERX3_EDIT, m_userx3);
	DDX_Text(pDX, IDC_USERX4_EDIT, m_userx4);
	DDX_Text(pDX, IDC_SHOW_EDIT, m_show);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)
	ON_BN_CLICKED(IDC_OPEN_BUTTON, OnOpenButton)
	ON_BN_CLICKED(IDC_RECO_BUTTON, OnRecoButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
}

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

void CMyDlg::OnExitButton() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CMyDlg::OnOpenButton() 
{
	// TODO: Add your control notification handler code here
	CString s[15];

	for (int i = 0; i < 5; i++)
	{
		w1[i] = 0;
		w2[i] = 0;
		w3[i] = 0;
	}
	m_show = "            类别Ⅰ                                类别Ⅱ                                类别Ⅲ          \r\n";
	m_show += "序号  x1    x2    x3    x4            序号  x1    x2    x3    x4            序号  x1    x2    x3    x4 \r\n";

	CString strpathName;
	CFileDialog filedlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, "*.txt|*.txt||");
    if (filedlg.DoModal() == IDOK)
       strpathName = filedlg.GetPathName();
	ifstream mySample(strpathName);
	
	for (i = 0; i < 150; i++)
	{
	    sample[i] = allData + i * 5;
		for (int j = 0; j < 4; j++)
			mySample >> /*allData[i * 5 + j];*/*(sample[i] + j);
		*(sample[i] + 4) = 1;
	}

	for (i = 0; i < 50; i++)
	{
		s[0].Format("%3d", i + 1);
		s[1].Format("%2.1f", allData[i * 5]);
		s[2].Format("%2.1f", allData[i * 5 + 1]);
		s[3].Format("%2.1f", allData[i * 5 + 2]);
		s[4].Format("%2.1f", allData[i * 5 + 3]);
		s[5].Format("%3d", i + 51);
		s[6].Format("%2.1f", allData[i * 5 + 250]);
		s[7].Format("%2.1f", allData[i * 5 + 251]);
		s[8].Format("%2.1f", allData[i * 5 + 252]);
		s[9].Format("%2.1f", allData[i * 5 + 253]);
		s[10].Format("%3d", i + 101);
		s[11].Format("%2.1f", allData[i * 5 + 500]);
		s[12].Format("%2.1f", allData[i * 5 + 501]);
		s[13].Format("%2.1f", allData[i * 5 + 502]);
		s[14].Format("%2.1f", allData[i * 5 + 503]);
		m_show += s[0] + "   " + s[1] + "   " + s[2] + "   " + s[3] + "   " + s[4] + "           " +
			s[5] + "   " + s[6] + "   " + s[7] + "   " + s[8] + "   " + s[9] + "           " +
			s[10] + "   " + s[11] + "   " + s[12] + "   " + s[13] + "   " + s[14] + "\r\n";
	}
	/*for (i = 0; i < 50; i++)
	{
		m_show += t.Format("%2d", i + 1) + "    " + s.Format("%2.1f", allData[
	s.Format("%2.1f", allData[1]);
	m_show += s;*/
	//char* ptr;
	//gcvt(allData[1], 2, ptr);
	//m_show += ptr;
	/*float aaa1,aaa2,aaa3,aaa4;
	mySample >>aaa1>>aaa2>>aaa3>>aaa4;
	
	m_show.Format("%5.1f" , aaa1);
	CFile f;
	if (!f.Open (strpathName,CFile::modeRead))
	{
		return;
	}
	LONG nCount;
	nCount = f.GetLength();
	LPBYTE lpBuf;
	lpBuf = new BYTE[nCount];
//	f.Read(lpBuf,nCount);*/
//	char aaa[10];
//	mySample >> aaa;
//	m_show = aaa;
//	UpdateData(FALSE);
	Iteration();
	AfxMessageBox("OK!");
	/*m_userx1 = w1[0];
	m_userx2 = w1[1];
	m_userx3 = w1[2];
	m_userx4 = w1[3];*/
	UpdateData(FALSE);
}

void CMyDlg::OnRecoButton() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	float dMax;
	float x[5] = {m_userx1, m_userx2, m_userx3, m_userx4, 1};
	float d1 = Dist(w1, x);
	float d2 = Dist(w2, x);
	float d3 = Dist(w3, x);

	if (d1 == d2 || d2 == d3 || d1 == d3)
		AfxMessageBox("此样本在决策面上,不可分!");
	else
	{
		dMax = d1 > d2 ? d1 : d2;
		dMax = dMax > d3 ? dMax : d3;
		if (dMax == d1)
			AfxMessageBox("此样本属于Ⅰ类!");
		else
		{
			if (dMax == d2)
				AfxMessageBox("此样本属于Ⅱ类!");
			else
				AfxMessageBox("此样本属于Ⅲ类!");
		}
		/*switch (dMax)
		{
			case 1: AfxMessageBox("此样本属于Ⅰ类!"); break;
			case 2: AfxMessageBox("此样本属于Ⅱ类!"); break;
			case 3: AfxMessageBox("此样本属于Ⅲ类!"); 
		}*/
	}
}

//判别函数
float CMyDlg::Dist(float* w, float* x)
{
    float d = 0;
	for (int i = 0; i < 5; i++)
		d += w[i] * x[i];
	return d;
}

void CMyDlg::Iteration()
{
    int m = 0;
	int n = 0;

	while (m < 148)
	{
		int a, b, c;
		float d[3];
		float* w[3] = {w1, w2, w3};

		if (n < 50)
		{
			a = 0;
			b = 1;
			c = 2;
		}
		else
		{
			if (n < 100)
			{
				a = 1;
				b = 0;
				c = 2;
			}
			else
			{
				a = 2;
				b = 0;
				c = 1;
			}
		}
		d[a] = Dist(w[a], sample[n]);
		d[b] = Dist(w[b], sample[n]);
		d[c] = Dist(w[c], sample[n]);

		//修正权向量
		if (d[a] <= d[b] || d[a] <= d[c])
		{
			m = 0;

			for (int i = 0; i < 5; i++)
				*(w[a] + i) += *(sample[n] + i);
			if (d[a] <= d[b])
			{
				for (i = 0; i < 5; i++)
					*(w[b] + i) -= *(sample[n] + i);
			}
			if (d[a] <= d[c])
			{
				for (i = 0; i < 5; i++)
					*(w[c] + i) -= *(sample[n] + i);
			}
		}
		else 
			m++;

		n++;
		if (n == 133)
			n = 134;
		if (n == 138)
			n = 139;
		if (n == 150)
			n = 0;
	}
}


⌨️ 快捷键说明

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