?? sortdlg.cpp
字號(hào):
// sortDlg.cpp : implementation file
//
#include "stdafx.h"
#include "sort.h"
#include "sortDlg.h"
#include "SortObject.h"
#include <math.h>
#include <time.h>
#include <afxmt.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define PI 3.1415926
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CSortDlg dialog
CSortDlg::CSortDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSortDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSortDlg)
// 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);
}
void CSortDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSortDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSortDlg, CDialog)
//{{AFX_MSG_MAP(CSortDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(IDM_Disperse_Member, OnDisperseMember)
ON_COMMAND(IDM_CREAT_NUMBER, OnCreatNumber)
ON_COMMAND(IDM_Muster_Member, OnMusterMember)
ON_COMMAND(IDM_Bubble_Sort, OnBubbleSort)
ON_COMMAND(IDM_Exchange_Sort, OnExchangeSort)
ON_COMMAND(IDM_Selection_Sort, OnSelectionSort)
ON_COMMAND(IDM_INSERT_SORT, OnInsertSort)
ON_COMMAND(IDM_QUICK_SORT, OnQuickSort)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSortDlg message handlers
BOOL CSortDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CSortDlg::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 CSortDlg::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 CSortDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSortDlg::OnDisperseMember()
{
CRect rect;
GetClientRect (&rect) ;
CClientDC dc (this) ;
dc.SetBkColor(RGB(100,100,255));
dc.FillRect(&rect,&CBrush(RGB(100,100,255)));
//將待排序的對(duì)象分布在一個(gè)圓上
for(int i=0;i<SORT_OBJECT_NUM;i++)
{
//繪制外框
dc.DrawEdge(&CRect(
rect.right/2+150*cos(2*PI*i/10.0)-10,
rect.bottom/2+150*sin(2*PI*i/10.0)-10,
rect.right/2+150*cos(2*PI*i/10.0)+10,
rect.bottom/2+150*sin(2*PI*i/10.0)+10),
BDR_RAISEDINNER,
BF_RECT
);
//顯示待排序?qū)ο竺? CString strName;
strName.Format("%d",i);
dc.TextOut(rect.right/2+150*cos(2*PI*i/10.0)-5,
rect.bottom/2+150*sin(2*PI*i/10.0)-8,
strName);
}
}
void CSortDlg::OnCreatNumber()
{
// TODO: Add your command handler code here
CRect rect;
GetClientRect (&rect) ;
CClientDC dc (this) ;
dc.FillRect(&CRect(0,0,250,150),&CBrush(RGB(100,100,255)));
dc.SetBkColor(RGB(100,100,255));
// Seed the random-number generator with current time
srand( (unsigned)time( NULL ) );
//產(chǎn)生SORT_OBJECT_NUM個(gè)3位或4位的數(shù)字
for( int i = 0; i < SORT_OBJECT_NUM;i++ )
{
//產(chǎn)生3位或4位數(shù)字
int temp;
temp = rand();
if(temp%2==0) //產(chǎn)生3位數(shù)字
{
while(temp%1000 < 100)
{
temp = rand();
}
sortObject[i].iNumber = temp%1000;
}
else //產(chǎn)生4位數(shù)字
{
while(temp%10000 < 1000)
{
temp = rand();
}
sortObject[i].iNumber = temp%10000;
}
//賦予名字和初始序號(hào)
sortObject[i].iName = i;
sortObject[i].iSeq = i;
//顯示獲得的數(shù)字
CString strNum;
strNum.Format("%4d",sortObject[i].iNumber);
dc.TextOut(rect.right/2+150*cos(2*PI*i/10.0)-15,
rect.bottom/2+150*sin(2*PI*i/10.0)-30,
strNum);
}
}
void CSortDlg::OnMusterMember()
{
CClientDC dc (this) ;
dc.FillRect(&CRect(0,0,250,150),&CBrush(RGB(100,100,255)));
CRect rect;
GetClientRect(&rect) ;
InitObjectCoord(rect);
dc.SetBkColor(RGB(100,100,255));
dc.FillRect(&rect,&CBrush(RGB(100,100,255)));
for(int i=0;i<SORT_OBJECT_NUM;i++)
{
//繪制外框
dc.DrawEdge(&CRect(
objectCoord[i].x-10,
objectCoord[i].y-10,
objectCoord[i].x+10,
objectCoord[i].y+10),
BDR_RAISEDINNER,
BF_RECT
);
//顯示待排序?qū)ο竺? CString strName;
strName.Format("%d",i);
dc.TextOut(objectCoord[i].x-5,
objectCoord[i].y-8,
strName);
//顯示獲得的數(shù)字
CString strNum;
strNum.Format("%4d",sortObject[i].iNumber);
dc.TextOut(objectCoord[i].x-15,
objectCoord[i].y-30,
strNum);
}
}
void CSortDlg::OnBubbleSort()
{
CClientDC dc (this) ;
dc.SetBkColor(RGB(100,100,255));
dc.FillRect(&CRect(0,0,250,150),&CBrush(RGB(100,100,255)));
int objectName[SORT_OBJECT_NUM]; //每個(gè)位置對(duì)應(yīng)的待排序?qū)ο竺? //初始化每個(gè)位置對(duì)應(yīng)的待排序?qū)ο竺? for(int i=0;i<SORT_OBJECT_NUM;i++)
{
objectName[i] = i;
}
TRACE("\n");
for(int w=0;w<SORT_OBJECT_NUM;w++)
{
TRACE("%d ",sortObject[objectName[w]].iNumber);
}
//冒泡排序
for(i=1;i<SORT_OBJECT_NUM;i++)
{
for(int j=SORT_OBJECT_NUM-1;j>=i;j--)
{
if(sortObject[objectName[j]].iNumber < sortObject[objectName[j-1]].iNumber)
{
//交換序號(hào)
int iTemp;
iTemp = sortObject[objectName[j-1]].iSeq;
sortObject[objectName[j-1]].iSeq = sortObject[objectName[j]].iSeq;
sortObject[objectName[j]].iSeq = iTemp;
//交換對(duì)象名
iTemp = objectName[j];
objectName[j] = objectName[j-1];
objectName[j-1] = iTemp;
//顯示變化
CString strName;
CString strNum;
//顯示j-1位置的對(duì)象
strName.Format("%d",sortObject[objectName[j-1]].iName);
dc.TextOut(objectCoord[sortObject[objectName[j-1]].iSeq].x-5,
objectCoord[sortObject[objectName[j-1]].iSeq].y-8,
strName);
if(sortObject[objectName[j-1]].iNumber<1000)
{
strNum.Format(" %d",sortObject[objectName[j-1]].iNumber);
}
else
{
strNum.Format("%d",sortObject[objectName[j-1]].iNumber);
}
dc.TextOut(objectCoord[sortObject[objectName[j-1]].iSeq].x-15,
objectCoord[sortObject[objectName[j-1]].iSeq].y-30,
strNum);
//顯示j位置的對(duì)象
strName.Format("%d",sortObject[objectName[j]].iName);
dc.TextOut(objectCoord[sortObject[objectName[j]].iSeq].x-5,
objectCoord[sortObject[objectName[j]].iSeq].y-8,
strName);
if(sortObject[objectName[j]].iNumber<1000)
{
strNum.Format(" %d",sortObject[objectName[j]].iNumber);
}
else
{
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -