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

📄 dialogcs.cpp

📁 该程序是一个对于一个对于以某一直线为边界的两类进行分类
💻 CPP
字号:
// DialogCS.cpp : implementation file
//

#include "stdafx.h"
#include "线性分类器.h"
#include "DialogCS.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDialogCS dialog
#include"MyProgressCtrl.h"

CDialogCS::CDialogCS(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogCS::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogCS)
	m_w1 = _T("");
	m_w2 = _T("");
	m_w3 = _T("");
	//}}AFX_DATA_INIT
}


void CDialogCS::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogCS)
	DDX_Control(pDX, IDC_STATICT2, m_T2);
	DDX_Control(pDX, IDC_STATICT1, m_T1);
	DDX_Control(pDX, IDC_LIST1, m_r);
	DDX_Control(pDX, IDC_BUTTON2, m_b2);
	DDX_Control(pDX, IDC_BUTTON1, m_b1);
	DDX_Text(pDX, IDC_EDIT1, m_w1);
	DDX_Text(pDX, IDC_EDIT2, m_w2);
	DDX_Text(pDX, IDC_EDIT3, m_w3);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogCS, CDialog)
	//{{AFX_MSG_MAP(CDialogCS)
	ON_WM_ERASEBKGND()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
	ON_EN_KILLFOCUS(IDC_EDIT3, OnKillfocusEdit3)
	ON_EN_KILLFOCUS(IDC_EDIT1, OnKillfocusEdit1)
	ON_EN_KILLFOCUS(IDC_EDIT2, OnKillfocusEdit2)
	ON_EN_CHANGE(IDC_EDIT3, OnChangeEdit3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogCS message handlers

BOOL CDialogCS::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	mybitmap.LoadBitmap(IDB_BITMAP1);
	CRect ClientRect;
GetDlgItem(IDC_LIST1)->GetClientRect(&ClientRect);
m_r.SetExtendedStyle (LVS_OWNERDRAWFIXED);
    m_r.InsertColumn (0, " 记录号");
    m_r.InsertColumn (1, "   W1");
    m_r.InsertColumn (2, "   W2");
    m_r.InsertColumn (3, "交叉点数");
    m_r.InsertColumn (4, "迭代次数");
    m_r.InsertColumn (5, "  识别率");
    
    m_r.SetColumnWidth (0, ClientRect.Width() / 6-1);
    m_r.SetColumnWidth (1, ClientRect.Width() / 6-1);
    m_r.SetColumnWidth (2, ClientRect.Width() / 6);
    m_r.SetColumnWidth (3, ClientRect.Width() / 6);
    m_r.SetColumnWidth (4, ClientRect.Width() / 6);
    m_r.SetColumnWidth (5, ClientRect.Width() /6 +4);
for(int i1=0;i1<7;i1++)
	{
	m_r.InsertItem (i1, " ");
	}
rli=0;
m_w3="1";
m_w1="1";
m_w2="1";
GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
UpdateData(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CDialogCS::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
		CRect rect;
		GetClientRect(rect);
       
        
//////////////////////////////////////////////////////
        CDC memdc;
		BITMAP bm1;
        mybitmap.GetObject(sizeof(BITMAP),&bm1);
		memdc.CreateCompatibleDC(pDC);
        CBitmap* oldbitmap=memdc.SelectObject(&mybitmap);
		int wide=rect.Width()/3;
		int mheight=rect.Height()/3;
	for (int j=0;j<3;j++)
	{
		for(int i=0;i<3;i++)
	{
      pDC->StretchBlt(rect.left,rect.top,wide,mheight,&memdc,0,0,bm1.bmWidth,bm1.bmHeight,SRCCOPY);
        rect.left+= wide;	
	}
	     rect.left-=wide*3;
		 rect.top+=mheight;
	}
	return true;
	return CDialog::OnEraseBkgnd(pDC);
}

void CDialogCS::OnButton1() 
{
	// TODO: Add your control notification handler code here
	int count=atoi(m_w3);
	srand( (unsigned)time( NULL ) );
	int i1=0,ix=0,iy=0;
	int x,y;
	while((i1<count))
	{
            x=(int)(rand()%350);
			y=(int)(rand()%350);
			if((y>x+2)&&(y<x+4))
			{
				*(towp+2*(i1+50))=x;
                 *(towp+2*(i1+50)+1)=y;
				 i1++;iy++;
			}
             if((y<x-2)&&(y>x-4))
			{
				*(onep+2*(i1+50))=x;
                 *(onep+2*(i1+50)+1)=y;
				 i1++;ix++;
			}
           
	}
	
	T1=ix;
	T2=iy;
	CString s1,s2;
	s1.Format("%d", T1);
	strcpy(m_T1.mychar,s1);
    s2.Format("%d", T2);
	strcpy(m_T2.mychar,s2);
CRect rectlt1,rectlt2;
GetDlgItem(IDC_STATICT1)->GetWindowRect(rectlt1);ScreenToClient(rectlt1);

GetDlgItem(IDC_STATICT2)->GetWindowRect(rectlt2);ScreenToClient(rectlt2);
InvalidateRect(rectlt2,true);InvalidateRect(rectlt1,true);
GetDlgItem(IDC_BUTTON2)->EnableWindow(true);
}

void CDialogCS::OnButton2() 
{
	// TODO: Add your control notification handler code here
	
	int tt=0;
		CMyProgressCtrl  myprogress;
	AfxGetApp()->BeginWaitCursor();
     CRect rect,rect1;
	GetClientRect(rect);
	int temp=rect.left+rect.Width()/2;
	rect.left=temp-250;
	rect.right=temp+250;
	GetDlgItem(IDC_BUTTON2)->GetWindowRect(rect1);
    ScreenToClient(rect1);
    rect.top=rect1.top-50;
    rect.bottom=rect.top+20;
   myprogress.Create(WS_CHILD|WS_VISIBLE,rect,this,1820);
   myprogress.SetRange(0,200);
   myprogress.SetPos(10);
	if(rli==6)
	{
		rli=0;
       for(int i=0;i<7;i++)
	   {
	       m_r.SetItemText (i, 0, " ");
          m_r.SetItemText (i, 1, "");
           m_r.SetItemText (i, 2, "");
         m_r.SetItemText (i, 3, "");
       m_r.SetItemText (i, 4, "");
        m_r.SetItemText (i, 5, "");
	   }
	}
	double count1=0,count2;
	int times=0;
	BOOL flag=true;
	 double w1,w2,w4,a;
	 w1=(double)atoi(m_w1);
       w2=(double)atoi(m_w2);
	   w4=0;
	   a=1;
	   
	  if(w2<=0)
	  {
		  w1=-w1;
		  w2=-w2;w4=-w4;
	  }
	while(flag)
	{
       
		times++;
		if(times<6)
myprogress.SetPos(10+20*times);
		else
myprogress.SetPos(140);
		Sleep(600);
		if(T1>=T2)
	   {
		   ///////////////////////////
		   for(int t=0;t<2*(50+T2);t++)
			 {			 
				 
				
			        if(t%2==0)
					{
                     double xt,yt,z;
			         xt=((double)(*(onep+t)))/10;
                     yt=((double)(*(onep+t+1)))/10;
             
			         z=xt*w1+yt*w2+w4;
                      if(z<=0)
					  {
				        
				        
				        w1=w1+a*xt;
				        w2=w2+a*yt;
				        w4=w4+a;
					  }
				 }
				 else
				 {
                       double xt,yt,z;
			           xt=((double)(*(towp+(t-1))))/10;
                       yt=((double)(*(towp+t)))/10;
             
			            z=xt*w1+yt*w2+w4;
			           if(z>=0)
					   {
				        
				          
				         w1=w1-a*xt;
				          w2=w2-a*yt;
				          w4=w4-a;
				
				 
					   }
				 }
				 
			 }
		   ///////////////////////////////
		    for(int t1=0;t1<(T1-T2);t1++)
			{
               double xt,yt,z;
			         xt=((double)(*(onep+2*(t1+50+T2))))/10;
                     yt=((double)(*(onep+2*(t1+50+T2)+1)))/10;
             
			         z=xt*w1+yt*w2+w4;
                      if(z<=0)
					  {
				        
				        
				        w1=w1+a*xt;
				        w2=w2+a*yt;
				        w4=w4+a;
					  }
			}
			//////////////////////////////////////////////////
			int er=0;
			for(int j=0;j<100+T1+T2;j++)
			{
				if(j<50+T1)
				{
                     double xt,yt,z;
			         xt=((double)(*(onep+2*j)))/10;
                     yt=((double)(*(onep+2*j+1)))/10;
                     if(w2<=0)
					 {
						 w1=-w1;w2=-w2;w4=-w4;
					 }
			         z=xt*w1+yt*w2+w4; 
					 if(z<=0)
					 {
						 er++;
					 }
				}
				else
				{
                   double xt,yt,z;
			         xt=((double)(*(onep+2*(j-50-T1))))/10;
                     yt=((double)(*(onep+2*(j-50-T1)+1)))/10;
                     if(w2<=0)
					 {
						 w1=-w1;w2=-w2;w4=-w4;
					 }
			         z=xt*w1+yt*w2+w4; 
					 if(z<=0)
					 {
						 er++;
					 }
				}
			}
			count2=((double)(100+T1+T2-er))/(100+T1+T2);
			if(count2>=count1)
			{
				if(count2==count1)
				{
					tt++;
				}
				count1=count2;
				if(tt==20)
				{
					flag=false;
				}
			}
			else
			{
				flag=false;
			}
	   }
	   else
	   {
		   ///////////////////////////////////////////////
		        ///////////////////////////
		   for(int t=0;t<2*(50+T1);t++)
			 {			 
				 
				
			        if(t%2==0)
					{
                     double xt,yt,z;
			         xt=((double)(*(onep+t)))/10;
                     yt=((double)(*(onep+t+1)))/10;
             
			         z=xt*w1+yt*w2+w4;
                      if(z<=0)
					  {
				        
				        
				        w1=w1+a*xt;
				        w2=w2+a*yt;
				        w4=w4+a;
					  }
				 }
				 else
				 {
                       double xt,yt,z;
			           xt=((double)(*(towp+(t-1))))/10;
                       yt=((double)(*(towp+t)))/10;
             
			            z=xt*w1+yt*w2+w4;
			           if(z>=0)
					   {
				        
				          
				         w1=w1-a*xt;
				          w2=w2-a*yt;
				          w4=w4-a;
				
				 
					   }
				 }
				 
			 }
		   ///////////////////////////////
		    for(int t1=0;t1<(T2-T1);t1++)
			{
               double xt,yt,z;
			         xt=((double)(*(towp+2*(t1+50+T1))))/10;
                     yt=((double)(*(towp+2*(t1+50+T1)+1)))/10;
             
			         z=xt*w1+yt*w2+w4;
                      if(z<=0)
					  {
				        
				        
				        w1=w1+a*xt;
				        w2=w2+a*yt;
				        w4=w4+a;
					  }
			}
			//////////////////////////////////////////////////
myprogress.SetPos(150);
			int er=0;
			for(int j=0;j<100+T1+T2;j++)
			{
				if(j<50+T1)
				{
                     double xt,yt,z;
			         xt=((double)(*(onep+2*j)))/10;
                     yt=((double)(*(onep+2*j+1)))/10;
                     z=xt*w1+yt*w2+w4; 
					 if(w2<=0)
					 {
						 z=-z;
					 }
			         
					 if(z<=0)
					 {
						 er++;
					 }
				}
				else
				{
                   double xt,yt,z;
			         xt=((double)(*(onep+2*(j-50-T1))))/10;
                     yt=((double)(*(onep+2*(j-50-T1)+1)))/10;
                     if(w2<=0)
					 {
						 z=-z;
					 }
			         z=xt*w1+yt*w2+w4; 
					 if(z<=0)
					 {
						 er++;
					 }
				}
			}
			count2=((double)(100+T1+T2-er))/(100+T1+T2);
			if(count2>=count1)
			{
				if(count2==count1)
				{
					tt++;
				}
				count1=count2;
				if(tt==20)
				{
					flag=false;
				}
			}
			else
			{
				flag=false;
			}
		   ///////////////////////////////////////////////
	   }
	}
	count2=count1;
	count1=0;
/////////////////////////////////
myprogress.SetPos(180);
Sleep(600);
int t1=0;
	   for(int i=0;i<200;i++)
		  {
	        double xt,yt,z;
			  
	        if(i<100)
			{
	         xt=((double)(*(onetp+2*i)))/10;
             yt=((double)(*(onetp+2*i+1)))/10;
	         
	         if(w2>0)
			 {
              z=w1*xt+w2*yt+w4;
			 }
	          else
			  {
                z=-w1*xt-w2*yt-w4;
			  }
	             if(z<=0)
				 {
		            t1++;
				 }
			}
	             else
				 {
		           if(i<200)
				   {
                      xt=((double)(*(towtp+2*(i-100))))/10;
                        yt=((double)(*(towtp+2*(i-100)+1)))/10;
			            
			            if(w2>0)
						{
				         z=w1*xt+w2*yt+w4;
						}
			            else
						{
				           z=-w1*xt-w2*yt-w4;
						}
			          if(z>=0)
					  {
				        t1++;
					  }
					}
				}
		}
count1=((100+T1+T2)*count2+(double)(200-t1))/(200+100+T1+T2);
	////////////////////////////////////
      CString str1,str2,str3,str4;
	  str1.Format("%d", rli+1);
	  str2.Format("%d", times-1);
int intn=int(count2*100);
       
        str3.Format("%d", intn);
		str3=str3+".";
 
    int dot=int(count2*100000-intn*1000+0.5);

str4.Format("%d", dot);
str3=str3+str4+"%";
         m_r.SetItemText (rli, 0, str1);
          m_r.SetItemText (rli, 1, m_w1);
           m_r.SetItemText (rli, 2, m_w2);
         m_r.SetItemText (rli, 3, m_w3);
       m_r.SetItemText (rli, 4, str2);
        m_r.SetItemText (rli, 5, str3);
		rli++;
myprogress.SetPos(200);
Sleep(600);
}

void CDialogCS::OnChangeEdit1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	CString temp=m_w1;
	UpdateData(true);
	if((m_w1=="0")&&(m_w2=="0"))
  
	{
	  m_w1=temp;	UpdateData(false);
	  

  }
	// TODO: Add your control notification handler code here
	
}

void CDialogCS::OnChangeEdit2() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	CString temp=m_w2;
	UpdateData(true);
	if((m_w1=="0")&&(m_w2=="0"))
  
	{
	  m_w2=temp;	UpdateData(false);
	  

  }
	// TODO: Add your control notification handler code here
	
}

void CDialogCS::OnKillfocusEdit3() 
{
	// TODO: Add your control notification handler code here
	if (m_w3.IsEmpty( ))
	{
		m_w3="1";UpdateData(false);
	}
}

void CDialogCS::OnKillfocusEdit1() 
{
	// TODO: Add your control notification handler code here
	if (m_w1.IsEmpty( ))
	{
		m_w1="1";UpdateData(false);
	}
}

void CDialogCS::OnKillfocusEdit2() 
{
	// TODO: Add your control notification handler code here
	if (m_w2.IsEmpty( ))
	{
		m_w2="1";UpdateData(false);
	}
}

void CDialogCS::OnChangeEdit3() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	CString temp=m_w3;
	UpdateData(true);
	if((m_w3=="0"))
  
	{
	  m_w3=temp;	UpdateData(false);
	  

  }
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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