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

📄 ourworkdlg.cpp

📁 模式识别的一些算法
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// OurWorkDlg.cpp : implementation file
//

#include "stdafx.h"
#include "OurWork.h"
#include "OurWorkDlg.h"
#include <math.h>
#include "DlgCAverSeg.h"
#include "DlgSetIsoData.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()

/////////////////////////////////////////////////////////////////////////////
// COurWorkDlg dialog

COurWorkDlg::COurWorkDlg(CWnd* pParent /*=NULL*/)
: CDialog(COurWorkDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COurWorkDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_nGroupCount=0;
	m_bFlag=FALSE;
	m_nType=0;
	m_bIsType=FALSE;
//////////////////ISODATA的基本参数////////////
	m_fBottom = 0.000f;
	m_nCenterNum = 0;
	m_nLeastNum = 0;
	m_nMostCountNum = 0;
	m_nMostPair = 0;
	m_nTypeNum = 0;
	m_fUpper =0.000f;

	m_fAllAver=0.000f;

	m_nCountOfDieDai=0;

	m_bIsSplitter=FALSE;

	m_bIsFinish=FALSE;///是否迭代成功

	m_bIsIsoData=FALSE;//是否已经进行了ISODATA算法分类
	m_bIsC=FALSE;//是否已经进行C均值算法
	///////////////////////////////////
}

void COurWorkDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COurWorkDlg)
	DDX_Control(pDX, IDC_LIST_DATASHOW, m_ListShow);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COurWorkDlg, CDialog)
//{{AFX_MSG_MAP(COurWorkDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_MAKENEW, OnButtonMakenew)
ON_BN_CLICKED(IDC_BUTTON_CJUNZHI, OnButtonCjunzhi)
	ON_WM_DESTROY()
	ON_WM_ERASEBKGND()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON_ISODATA, OnButtonIsodata)
	ON_BN_CLICKED(IDC_BUTTON_MODIFYC, OnButtonModifyc)
	ON_BN_CLICKED(IDC_BUTTON_CANCELC, OnButtonCancelc)
	ON_BN_CLICKED(IDC_BUTTON_OKC, OnButtonOkc)
	ON_BN_CLICKED(IDC_BUTTON_MODIFYISODATA, OnButtonModifyisodata)
	ON_BN_CLICKED(IDC_BUTTON_CANCELISODATA, OnButtonCancelisodata)
	ON_BN_CLICKED(IDC_BUTTON_OKISODATA, OnButtonOkisodata)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COurWorkDlg message handlers

BOOL COurWorkDlg::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
	
	CString strTemp=_T("");
	strTemp.Format(_T("C均值,ISODATA算法:费绍敏,罗仕强,李宾,傅孝韬,李兴梅的模式识别作业"));
	SetWindowText(strTemp);
	
	
	////初始化list////////////////////////////////////////////////////////
	m_ListShow.SetExtendedStyle(m_ListShow.GetExtendedStyle()||LVS_EX_GRIDLINES);
    m_ListShow.InsertColumn(0,_T("序号"),LVCFMT_LEFT,40);
	m_ListShow.InsertColumn(1,_T("特征值"),LVCFMT_LEFT,120);
	////////////////////////////////////////////////////////////////////////
	MakeRnd();
	
 /*
	m_fX[0]=4.844;
	m_fX[1]=1.519;
	m_fX[2]=5.968;
	m_fX[3]=4.015;
	m_fX[4]=5.419;

	m_fY[0]=2.415;
	m_fY[1]=4.022;
	m_fY[2]=4.225;
	m_fY[3]=0.189;
	m_fY[4]=0.314;*/

	ShowData();

	////////////产生随机颜色//////////////////
	MakeRndColor(m_nColor);
	while(JudgeColorRepeate(m_nColor))
	{
		MakeRndColor(m_nColor);
	}
	///////////////////////////////////////
	//Invalidate(TRUE);
	
	EnableControlC(FALSE);
	EnableControlISOData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void COurWorkDlg::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 COurWorkDlg::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
	{
		
		DrawOrder();		
		ShowDot();		
		DrawISODataOrder();
		//ShowISODataDot();
		
		if(m_bIsType)
		{
			DrawPic();
			DrawOrder();
			DrawISODataOrder();
			//m_bIsType=FALSE;
		}
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR COurWorkDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



void COurWorkDlg::MakeRnd()
{
	
	srand(GetTickCount());
	for(int n=0;n<NCOUNT;n++)
	{
		int f1,f2;
		f1=rand()%10000;
		float fTemp1,fTemp2;
		fTemp1=(float)f1/1000;
		f2=rand()%10000;
		fTemp2=(float)f2/1000;
		if(!JudgeRepeate(fTemp1,fTemp2,n))
		{
			n--;
			continue;
		}
		m_fX[n]=fTemp1;
		m_fY[n]=fTemp2;
		
	}
	
	
}

BOOL COurWorkDlg::JudgeRepeate(float fTemp1,float fTemp2,int n)
{
	
	for( int i=0;i<n;i++)
	{
		if(m_fX[i]==fTemp1)
		{
			if(m_fY[i]==fTemp2)
			{
				return FALSE;
			}
		}
	}
	return true;
}


void COurWorkDlg::ShowData()
{
	m_ListShow.DeleteAllItems();
	for(int n=0;n<NCOUNT;n++)
	{
		CString strTemp=_T("");
		strTemp.Format(_T("%d"),n);
		m_ListShow.InsertItem(n,strTemp);
		strTemp.Format(_T("(%0.3f,%0.3f)"),m_fX[n],m_fY[n]);
		m_ListShow.SetItemText(n,1,strTemp);
		
	}
}

void COurWorkDlg::OnButtonMakenew() 
{
	Invalidate(TRUE);
	m_bIsType=FALSE;
	m_nType=0;
	m_bIsIsoData=FALSE;
	m_bIsC=FALSE;
	MakeRnd();
	ShowData();
	
	ShowDot();
	DrawOrder();
	DrawISODataOrder();
	
}
void COurWorkDlg::DrawOrder()
{
	CClientDC dc(this);
	
    dc.SetBkColor(GetSysColor(COLOR_3DFACE));
	///绘制横坐标//////////////////
	dc.MoveTo(YUANDIAN_X,YUANDIAN_Y);
	dc.LineTo(XEND_X,XEND_Y);
	///////////////////////////////	
	///绘制纵坐标//////////////////
	dc.MoveTo(YUANDIAN_X,YUANDIAN_Y);
	dc.LineTo(YEND_X,YEND_Y);
	///////////////////////////////	
	///绘制横坐标箭头//////////////////
	dc.MoveTo(XEND_X,XEND_Y);
	dc.LineTo(XEND_X-14,XEND_Y-5);
	dc.MoveTo(XEND_X,XEND_Y);
	dc.LineTo(XEND_X-14,XEND_Y+5);	
	
	dc.TextOut(XEND_X-5,XEND_Y+5,_T("X"));
	///////////////////////////////	
	///绘制纵坐标箭头//////////////////
	dc.MoveTo(YEND_X,YEND_Y);
	dc.LineTo(YEND_X-5,YEND_Y+14);
	dc.MoveTo(YEND_X,YEND_Y);
	dc.LineTo(YEND_X+5,YEND_Y+14);
	
	dc.TextOut(YEND_X-5,YEND_Y-20,_T("Y"));
	///////原点////////////////////
	CString strTemp=_T("0");
	dc.TextOut(YUANDIAN_X,YUANDIAN_Y+2,strTemp);
	///////////////////////////////
	///////绘制X刻度////////////////
	dc.MoveTo(XEND_X-30-9,XEND_Y+2);
	dc.LineTo(XEND_X-30-9,XEND_Y-5);
	dc.TextOut(XEND_X-30-9,XEND_Y+2+2,_T("10"));
	for(int k=1;k<=9;k++)
	{
		CString strTemp=_T("");
		strTemp.Format(_T("%d"),k);
		dc.TextOut((XEND_X-30-YUANDIAN_X-9)/10*k+YUANDIAN_X,XEND_Y+2+2,strTemp);
		dc.MoveTo((XEND_X-30-YUANDIAN_X-9)/10*k+YUANDIAN_X,XEND_Y+2);
		dc.LineTo((XEND_X-30-YUANDIAN_X-9)/10*k+YUANDIAN_X,XEND_Y-5);
	}
	
	///////////////////////////////
	///////绘制y刻度////////////////
	strTemp=_T("10");
	dc.TextOut(YEND_X-20,YEND_Y+30,strTemp);
	dc.MoveTo(YEND_X,YEND_Y+30);
	dc.LineTo(YEND_X+5,YEND_Y+30);
	
	
	for(int n=1;n<=9;n++)
	{
		strTemp=_T("");
		strTemp.Format(_T("%d"),n);
		dc.TextOut(YEND_X-20,YUANDIAN_Y-(YUANDIAN_Y-YEND_Y-30)/10*n,strTemp);
		dc.MoveTo(YEND_X,YUANDIAN_Y-(YUANDIAN_Y-YEND_Y-30)/10*n);
		dc.LineTo(YEND_X+5,YUANDIAN_Y-(YUANDIAN_Y-YEND_Y-30)/10*n);
	}
	///////////////////////////////
}

void COurWorkDlg::ShowDot()
{
	CPaintDC dc(this);
    CPen NewPen(PS_SOLID,1,RGB(0,0,0));
	CPen *pOldPen=dc.SelectObject(&NewPen);
	CBrush *pOldBrush;
	float fY=float((float(YUANDIAN_Y)-float(YEND_Y+30))/float(10000));//实际将坐标分成了10000分,该计算得出每分的长度
	float fX=float((float(XEND_X-30-9)-float(YUANDIAN_X))/float(10000));
	for(int n=0;n<NCOUNT;n++)
	{
		int nX=(int )(m_fX[n]*1000);
		int nY=(int )(m_fY[n]*1000);
		int nTempX=(int)(YUANDIAN_X+fX*nX);
		int nTempY=(int)(YUANDIAN_Y-fY*nY);
		CBrush brush;
		brush.CreateSolidBrush(RGB(0,0,0));
		pOldBrush=dc.SelectObject(&brush);
		dc.Ellipse(nTempX-2,nTempY-2,nTempX+2,nTempY+2);
		
	}
	dc.SelectObject(pOldPen);
	dc.SelectObject(pOldBrush);


	////////////////在ISODate坐标中画点/////////////////
	 CPen NewPen1(PS_SOLID,1,RGB(255,0,0));
	 pOldPen=dc.SelectObject(&NewPen1);
	 fY=float((float(YUANDIAN_Y1)-float(YEND_Y1+30))/float(10000));//实际将坐标分成了10000分,该计算得出每分的长度
	 fX=float((float(XEND_X1-30-9)-float(YUANDIAN_X1))/float(10000));
	for( n=0;n<NCOUNT;n++)
	{
		int nX=(int )(m_fX[n]*1000);
		int nY=(int )(m_fY[n]*1000);
		int nTempX=(int)(YUANDIAN_X1+fX*nX);
		int nTempY=(int)(YUANDIAN_Y1-fY*nY);
		CBrush brush;
		brush.CreateSolidBrush(RGB(255,0,0));
		pOldBrush=dc.SelectObject(&brush);
		dc.Ellipse(nTempX-2,nTempY-2,nTempX+2,nTempY+2);
		
		
	}
	dc.SelectObject(pOldPen);
	dc.SelectObject(pOldBrush);
	

}

void COurWorkDlg::OnButtonCjunzhi() 
{
	UpdateData(TRUE);	
	CString strTemp=_T("");
	m_bIsC=TRUE;
	CDlgCAverSeg dlg;	
	int nCount=0;
	if(dlg.DoModal()==IDOK)
	{
		m_nGroupCount=(int)atoi(dlg.m_strGroupCount);
		m_strGroupCount=dlg.m_strGroupCount;
		SetDlgItemText(IDC_EDIT_NUMC,dlg.m_strGroupCount);
		if(m_nGroupCount>NCOUNT)
		{
			MessageBox(_T("分类的数目大于了总的样本数目,请输入正确的分类数目!"),_T("友情提示"));
			return;
		}

		////////////产生随机颜色//////////////////
		/*MakeRndColor(m_nColor);
		while(JudgeColorRepeate(m_nColor))
		{
			MakeRndColor(m_nColor);
		}*/
		///////////////////////////////////////
		m_nType=1;
		ClearArray();////清空所有的数组
		CString strTemp=_T("");
		for(int n=0;n<m_nGroupCount;n++)
		{
			strTemp.Format(_T("%0.3f"),m_fX[n]);
			m_strArrayCenterX.Add(strTemp);
			strTemp.Format(_T("%0.3f"),m_fY[n]);
			m_strArrayCenterY.Add(strTemp);

⌨️ 快捷键说明

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