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

📄 sortdlg.cpp

📁 用Visual C++实现排序算法大全
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 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)));
	//将待排序的对象分布在一个圆上
	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
			);
		//显示待排序对象名
		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 ) );
	//产生SORT_OBJECT_NUM个3位或4位的数字
	for( int i = 0; i < SORT_OBJECT_NUM;i++ )
	{
		//产生3位或4位数字
		int temp;
		temp = rand();
		if(temp%2==0)  //产生3位数字
		{
			while(temp%1000 < 100)
			{
				temp = rand();				
			}
			sortObject[i].iNumber = temp%1000;
		}
		else           //产生4位数字
		{ 
			while(temp%10000 < 1000)
			{
				temp = rand();				
			}
			sortObject[i].iNumber = temp%10000;
		}   
		//赋予名字和初始序号
		sortObject[i].iName = i;
		sortObject[i].iSeq = i;
		//显示获得的数字
		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
			);
		//显示待排序对象名
		CString strName;
		strName.Format("%d",i);		
		dc.TextOut(objectCoord[i].x-5,
			objectCoord[i].y-8,
			strName);	
		//显示获得的数字
		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]; //每个位置对应的待排序对象名
	//初始化每个位置对应的待排序对象名
	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) 
			{ 				
				//交换序号
				int iTemp;
				iTemp = sortObject[objectName[j-1]].iSeq; 
				sortObject[objectName[j-1]].iSeq = sortObject[objectName[j]].iSeq; 
				sortObject[objectName[j]].iSeq = iTemp; 
				//交换对象名
				iTemp = objectName[j];
				objectName[j] = objectName[j-1];
                objectName[j-1] = iTemp;
                //显示变化
				CString strName;
				CString strNum;				
				//显示j-1位置的对象
				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位置的对象
				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
				{

⌨️ 快捷键说明

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