C++邻域查找

来源:互联网 发布:腾讯代理吃鸡会优化么 编辑:程序博客网 时间:2024/05/18 17:04

// TestCSFADlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestCSFA.h"
#include "TestCSFADlg.h"
#include<iostream>
#include<string>
#include<sstream>
#include <vector>
#include <algorithm>

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


# define THRESHOLD 0.3
const int CREATE_DATA_ROW_COUNT = 24;
const int CREATE_DATA_COLUMN_COUNT = 16;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

using namespace std;
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()

/////////////////////////////////////////////////////////////////////////////
// CTestCSFADlg dialog

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

void CTestCSFADlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CTestCSFADlg)
 DDX_Control(pDX, IDC_BUTTON_MIN, m_Min);
 DDX_Control(pDX, IDC_BUTTON_MAX, m_Max);
 DDX_Control(pDX, IDC_LIST4, m_list);
 DDX_Control(pDX, IDC_BUTTON_CREATE, m_Create);
 DDX_Control(pDX, IDC_BUTTON_MAXMIN, m_Run);
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestCSFADlg, CDialog)
 //{{AFX_MSG_MAP(CTestCSFADlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_BN_CLICKED(IDC_BUTTON_MAXMIN, OnButtonMaxMin)
 ON_BN_CLICKED(IDC_BUTTON_CREATE, OnButtonCreate)
 ON_BN_CLICKED(IDC_BUTTON_MAX, OnButtonMax)
 ON_BN_CLICKED(IDC_BUTTON_MIN, OnButtonMin)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestCSFADlg message handlers

BOOL CTestCSFADlg::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);
  }
 }

 //m_list.SetBkImage("d://test.bmp");
 
    m_list.SetTextBkColor(CLR_NONE);


 m_list.SetExtendedStyle(
  
       LVS_EX_FLATSB              
  
       // |LVS_EX_FULLROWSELECT   
  
        |LVS_EX_HEADERDRAGDROP   
  
        |LVS_EX_ONECLICKACTIVATE
  
        |LVS_EX_GRIDLINES    
  
        );
 

 
 CRect rect4;
 m_list.GetClientRect(rect4); 
 CString strColName = "Col-";
 CString strCount;

 for(int i = 0; i < CREATE_DATA_COLUMN_COUNT;i++)
 {
   strCount.Format(_T("%d"),i+1);
   m_list.InsertColumn(i,strColName+strCount);
   m_list.SetColumnWidth(i,rect4.Width()/CREATE_DATA_COLUMN_COUNT);

 }

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

 m_Create.EnableWindow(true);
 m_Max.EnableWindow(false);
 m_Min.EnableWindow(false);
 m_Run.EnableWindow(false);
 
 // TODO: Add extra initialization here
 
 return TRUE;  // return TRUE  unless you set the focus to a control
}

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


//////////////////////////////////////////////////////////////////////////
 
 int arr[CREATE_DATA_ROW_COUNT][CREATE_DATA_COLUMN_COUNT]={0};
 bool bArr[CREATE_DATA_ROW_COUNT][CREATE_DATA_COLUMN_COUNT] = {false};
 bool bArr2[CREATE_DATA_ROW_COUNT][CREATE_DATA_COLUMN_COUNT] = {false};

 int g_nMaxValue = 0;
 int g_nMinValue = 1000;

 int g_nMaxCount = 0;
 int g_nMinCount = 0;
 int g_nMaxMinCount = 0;

 struct  MyPoint  
 {  
  int   x;  
  int   y;  
 };

 MyPoint arMaxPoint[CREATE_DATA_ROW_COUNT * CREATE_DATA_COLUMN_COUNT] = {NULL};
 MyPoint arMinPoint[CREATE_DATA_ROW_COUNT * CREATE_DATA_COLUMN_COUNT] = {NULL};
 MyPoint arMaxMinPoint[CREATE_DATA_ROW_COUNT * CREATE_DATA_COLUMN_COUNT] = {NULL};

 //////////////////////////////////////////////////////////////////////////
 void CTestCSFADlg::OnButtonCreate()
 { 
  CString csTemp;
  CString csTemp1;
  CString csTemp2;
  
  g_nMaxValue = 0;
  g_nMinValue = 1000;
  
  g_nMaxCount = 0;
  g_nMinCount = 0;
  g_nMaxMinCount = 0;

     m_list.DeleteAllItems();

  for (int m = 0;m < CREATE_DATA_ROW_COUNT;m++)
  {
   for (int n = 0;n < CREATE_DATA_COLUMN_COUNT;n++)
   {    
    bArr[m][n] = false;
    bArr2[m][n] = false;   
   }
  }

  srand(unsigned(time(0)));
  for (int k = 0;k < CREATE_DATA_ROW_COUNT; k++)
  {
   m_list.InsertItem(k,"***");
  }
  
  for (int i = 0; i < CREATE_DATA_ROW_COUNT; i++)
  {
   for (int l = 0; l < CREATE_DATA_COLUMN_COUNT; l++)
   { 
    arr[i][l] = rand()%1401 - 401;
    csTemp.Format(_T("%d"),arr[i][l]);
    m_list.SetItemText(i,l,csTemp);

    if (arr[i][l] > g_nMaxValue)
    {
     g_nMaxValue = arr[i][l];
     arMaxPoint[0].x = i;
     arMaxPoint[0].y = l;
    }
    
    if (arr[i][l] < g_nMinValue)
    {
     g_nMinValue = arr[i][l];
     arMinPoint[0].x = i;
     arMinPoint[0].y = l;    
    }
   }
  }

  g_nMaxCount++;
  g_nMinCount++;

  csTemp.Format(_T("%d"),g_nMaxValue);
  csTemp1.Format(_T("%d"),arMaxPoint[0].x);
  csTemp2.Format(_T("%d"),arMaxPoint[0].y);
  //AfxMessageBox("Max value:" + csTemp + " " + "Position :" +"(" + csTemp1 + "," + csTemp2 +")");

  csTemp.Format(_T("%d"),g_nMinValue);
  csTemp1.Format(_T("%d"),arMinPoint[0].x);
  csTemp2.Format(_T("%d"),arMinPoint[0].y);
  //AfxMessageBox("Min value:" + csTemp + " " + "Position :" +"(" + csTemp1 + "," + csTemp2 +")");

  m_list.SetTextColor(0x0000);
  m_Create.EnableWindow(false);
  m_Max.EnableWindow(true);
  //m_Min.EnableWindow(true);
  //m_Run.EnableWindow(true);
}

 void CTestCSFADlg::OnButtonMax()
 {

  CString csTemp;

  SearchMaxArea();

  csTemp.Format(_T("%d"),g_nMaxCount);
  AfxMessageBox("Max points:" + csTemp);

  for (int nMax = 0;nMax < g_nMaxCount;nMax++)
  {
   m_list.SetItemText(arMaxPoint[nMax].x, arMaxPoint[nMax].y,
      m_list.GetItemText(arMaxPoint[nMax].x, arMaxPoint[nMax].y));
  }


  m_list.SetTextColor(0x00aa);

  m_Max.EnableWindow(false);
  m_Min.EnableWindow(true);
  m_Create.EnableWindow(true);
 }

 void CTestCSFADlg::OnButtonMin()
 {
  CString csTemp;

  SearchMinArea();

  csTemp.Format(_T("%d"),g_nMinCount);
  AfxMessageBox("Min points:" + csTemp);

  for (int nMin = 0;nMin < g_nMinCount;nMin++)
  {
   m_list.SetItemText(arMinPoint[nMin].x, arMinPoint[nMin].y,
      m_list.GetItemText(arMinPoint[nMin].x, arMinPoint[nMin].y));
  }

   m_list.SetTextColor(0xff00);

  m_Min.EnableWindow(false);
  m_Run.EnableWindow(true);
  m_Create.EnableWindow(true);
  
 }

 void CTestCSFADlg::OnButtonMaxMin()
 {
  CString csTemp;

  SearchMaxMinArea();

  csTemp.Format(_T("%d"),g_nMaxMinCount);
  AfxMessageBox("Max&Min points:" + csTemp);
  
  for (int nMaxMin = 0; nMaxMin < g_nMaxMinCount; nMaxMin++)
  {
   m_list.SetItemText(arMaxMinPoint[nMaxMin].x,arMaxMinPoint[nMaxMin].y,
    m_list.GetItemText(arMaxMinPoint[nMaxMin].x,arMaxMinPoint[nMaxMin].y));
  }
  
  m_list.SetTextColor(0xAAFF);

  m_Run.EnableWindow(false);
  m_Create.EnableWindow(true);
  
 }

 

 void CTestCSFADlg::SearchMaxArea()
 {
  CString csTemp1;

  int nMaxX = arMaxPoint[0].x;
  int nMaxY = arMaxPoint[0].y;
  bArr[nMaxX][nMaxY] = true;
  for (int p = 1;p < 5; p++)
  {
   searchMaxNearest(nMaxX,nMaxY,p);
  }
 }


 void CTestCSFADlg::SearchMinArea()
 {
  CString csTemp1;  
  int nMinX = arMinPoint[0].x;
  int nMinY = arMinPoint[0].y;
  bArr2[nMinX][nMinY] = true;
  for (int p = 1;p < 5; p++)
  {
   searchMinNearest(nMinX,nMinY,p);
  }
 }


 void CTestCSFADlg::SearchMaxMinArea()
 {
  for (int i = 0; i < CREATE_DATA_ROW_COUNT; i++)
  {
   for (int l = 0; l < CREATE_DATA_COLUMN_COUNT; l++)
   {
    if (bArr[i][l] && bArr2[i][l])
    {
     arMaxMinPoint[g_nMaxMinCount].x = i;
     arMaxMinPoint[g_nMaxMinCount].y = l;
     g_nMaxMinCount++;    
    } 
   }
  }
 }

 

 void CTestCSFADlg::searchMaxNearest(int nXPos,int nYPos,int nDirection)
 { 
  
  CString scTemp;
  
  switch(nDirection)
  {
  case 1:
   if(nYPos < 1) return;
   nYPos--;
   break;
  case 2:
   if(nXPos < 1) return;
   nXPos--;
   break;  
  case 3:
   if(nYPos > CREATE_DATA_COLUMN_COUNT- 2) return;
   nYPos++;
   break;
  case 4:
   if(nXPos > CREATE_DATA_ROW_COUNT - 2) return;
   nXPos++;
   break;
  }
   

  if (arr[nXPos][nYPos] > g_nMaxValue * THRESHOLD)
  {
   if (bArr[nXPos][nYPos])
   {
    return;
   }
   bArr[nXPos][nYPos] = true;
   arMaxPoint[g_nMaxCount].x = nXPos;
   arMaxPoint[g_nMaxCount].y = nYPos;
   g_nMaxCount++;

   for (int i = 1; i < 5;i++ )
   {
    searchMaxNearest(nXPos,nYPos,i);      
   }
  }
  else
  { 
   return;
  }
 }


 void CTestCSFADlg::searchMinNearest(int nXPos,int nYPos,int nDirection)
 { 
  CString scTemp; 
  switch(nDirection)
  {
  case 1:
   if(nYPos < 1) return;
   nYPos--;
   break;
  case 2:
   if(nXPos < 1) return;
   nXPos--;
   break;  
  case 3:
   if(nYPos > CREATE_DATA_COLUMN_COUNT- 2) return;
   nYPos++;
   break;
  case 4:
   if(nXPos > CREATE_DATA_ROW_COUNT - 2) return;
   nXPos++;
   break;
  }
   

  if (arr[nXPos][nYPos] > abs(g_nMinValue) * THRESHOLD)
  {
   if (bArr2[nXPos][nYPos])
   {
    return;
   }
   bArr2[nXPos][nYPos] = true;
   arMinPoint[g_nMinCount].x = nXPos;
   arMinPoint[g_nMinCount].y = nYPos;
   g_nMinCount++;

   for (int i = 1; i < 5;i++ )
   {
    searchMinNearest(nXPos,nYPos,i);       
   }
  }
  else
  {
   return;   
  }  

}

原创粉丝点击