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

📄 sortdlg.cpp

📁 这 里 是 用mfc 做的 一些排序 ,各种 排序都有
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// sortDlg.cpp : implementation file
//

#include "stdafx.h"
#include "sort.h"
#include "sortDlg.h"
#include "stdlib.h"
#include "time.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
const int RADIX = 10;
const int WIDTH = 10;
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
//const int RADIX = 10;
//const int WIDTH = 10;
// 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)
	//}}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)
	DDX_Control(pDX, IDC_BUTTON6, m_clear);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSortDlg, CDialog)
	//{{AFX_MSG_MAP(CSortDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, Onshellsort)
	ON_BN_CLICKED(IDC_BUTTON3, OnBubbleSort)
	ON_BN_CLICKED(IDC_BUTTON4, OnInsertSort)
	ON_BN_CLICKED(IDC_BUTTON5, OnMergeSort)
	ON_BN_CLICKED(IDC_BUTTON6, OnClear)
	ON_BN_CLICKED(IDC_BUTTON7, OnCardinaNumberSort)
	ON_BN_CLICKED(IDC_BUTTON8, OnHeapSort)
	ON_BN_CLICKED(IDC_BUTTON9, OnDirectChooseSort)
	ON_BN_CLICKED(IDC_BUTTON10, OnBinaryInsertSort)
	ON_BN_CLICKED(IDC_BUTTON11, 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::OnButton1() 
{
	// TODO: Add your control notification handler code here
	// TODO: Add your control notification handler code here
	srand((unsigned)time(NULL));   //初始化随机函数发生器 
    CString s; 

int a[20]={IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,IDC_EDIT4,IDC_EDIT5,IDC_EDIT6
,IDC_EDIT7,IDC_EDIT8,IDC_EDIT9,IDC_EDIT10};
	 

    for(int i=0;i<10;i++ ) 
    {         
        s.Format("%d",rand()); 
		
        SetDlgItemText(a[i] , s) ; //将数字转为字符串后显示在编辑框 
		
    } 
        m_clear.EnableWindow(true); //m_clear是”清空数据”按扭所联系的变量 
	
}

void CSortDlg::Onshellsort() //希尔排序
{
	// TODO: Add your control notification handler code here
	int x[10];         CString s; 

int a[20]={IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,IDC_EDIT4,IDC_EDIT5,IDC_EDIT6
	 ,IDC_EDIT7,IDC_EDIT8,IDC_EDIT9,IDC_EDIT10,IDC_EDIT11,IDC_EDIT12,IDC_EDIT13,
	 IDC_EDIT14,IDC_EDIT15,IDC_EDIT16,IDC_EDIT17,IDC_EDIT18,IDC_EDIT19,IDC_EDIT20};

    for(int i=0;i<10;i++ ) 
    {     
	
	       GetDlgItem(a[i] )->GetWindowText(s); 
           x[i ]= atoi(s); //将编辑框中的字符转化为数字后对应的赋给包含10个元素的数组 
	
    } 
	
Shellsort(x);



	for(i=0;i<10;i++ ) 
    {   
	
	      s.Format("%d",x[i]); 
          GetDlgItem(a[i+10] )->SetWindowText(s); 
	
    }                   //将排序后的数组元素赋给右边的编辑框 
        m_clear.EnableWindow(TRUE);   //启用”清空数据”按扭 

}

void CSortDlg::OnBubbleSort()//冒泡排序 
{
	// TODO: Add your control notification handler code here
	 int x[10];         CString s; 
int a[20]={IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,IDC_EDIT4,IDC_EDIT5,IDC_EDIT6
	 ,IDC_EDIT7,IDC_EDIT8,IDC_EDIT9,IDC_EDIT10,IDC_EDIT11,IDC_EDIT12,IDC_EDIT13,
	 IDC_EDIT14,IDC_EDIT15,IDC_EDIT16,IDC_EDIT17,IDC_EDIT18,IDC_EDIT19,IDC_EDIT20};

    for(int i=0;i<10;i++ ) 
    {     
	
	       GetDlgItem(a[i] )->GetWindowText(s); 
           x[i ]= atoi(s); //将编辑框中的字符转化为数字后对应的赋给包含10个元素的数组 
	
    } 
	
bubble_sort( x);



	for(i=0;i<10;i++ ) 
    {   
		
	      s.Format("%d",x[i]); 
          GetDlgItem(a[i+10] )->SetWindowText(s); 
	
    }                   //将排序后的数组元素赋给右边的编辑框 
        m_clear.EnableWindow(TRUE);   //启用”清空数据”按扭 

}

void CSortDlg::OnInsertSort() //直接插入排序
{
	// TODO: Add your control notification handler code here
	int x[10];         CString s; 
	int a[20]={IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,IDC_EDIT4,IDC_EDIT5,IDC_EDIT6
	 ,IDC_EDIT7,IDC_EDIT8,IDC_EDIT9,IDC_EDIT10,IDC_EDIT11,IDC_EDIT12,IDC_EDIT13,
	 IDC_EDIT14,IDC_EDIT15,IDC_EDIT16,IDC_EDIT17,IDC_EDIT18,IDC_EDIT19,IDC_EDIT20};

    for(int i=0;i<10;i++ ) 
    {     
	
	       GetDlgItem(a[i])->GetWindowText(s); 
           x[i ]= atoi(s); //将编辑框中的字符转化为数字后对应的赋给包含10个元素的数组 
	
    } 
	
InsertionSort(x);

	for(i=0;i<10;i++ ) 
    {  
	      s.Format("%d",x[i]); 
          GetDlgItem(a[i+10] )->SetWindowText(s); 
	
    }                   //将排序后的数组元素赋给右边的编辑框 
        m_clear.EnableWindow(TRUE);   //启用”清空数据”按扭 

}

void CSortDlg::OnMergeSort() //归并排序
{
	// TODO: Add your control notification handler code here
	int x[10];         CString s; 
	int a[20]={IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,IDC_EDIT4,IDC_EDIT5,IDC_EDIT6
	 ,IDC_EDIT7,IDC_EDIT8,IDC_EDIT9,IDC_EDIT10,IDC_EDIT11,IDC_EDIT12,IDC_EDIT13,
	 IDC_EDIT14,IDC_EDIT15,IDC_EDIT16,IDC_EDIT17,IDC_EDIT18,IDC_EDIT19,IDC_EDIT20};
    for(int i=0;i<10;i++ ) 
    {     
		
	       GetDlgItem(a[i] )->GetWindowText(s); 
           x[i ]= atoi(s); //将编辑框中的字符转化为数字后对应的赋给包含10个元素的数组 
	
    } 
	
MergeSort(x);

	for(i=0;i<10;i++ ) 
    {   
	
	      s.Format("%d",x[i]); 
          GetDlgItem(a[i+10] )->SetWindowText(s); 
	
    }                   //将排序后的数组元素赋给右边的编辑框 
        m_clear.EnableWindow(TRUE);   //启用”清空数据”按扭 
}



void CSortDlg::OnCardinaNumberSort() //基数排序
{
	// TODO: Add your control notification handler code here
		int x[10];         CString s; 
		int a[20]={IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,IDC_EDIT4,IDC_EDIT5,IDC_EDIT6
	 ,IDC_EDIT7,IDC_EDIT8,IDC_EDIT9,IDC_EDIT10,IDC_EDIT11,IDC_EDIT12,IDC_EDIT13,
	 IDC_EDIT14,IDC_EDIT15,IDC_EDIT16,IDC_EDIT17,IDC_EDIT18,IDC_EDIT19,IDC_EDIT20};
    for(int i=0;i<10;i++ ) 
    {     
	
	       GetDlgItem(a[i] )->GetWindowText(s); 
           x[i ]= atoi(s); //将编辑框中的字符转化为数字后对应的赋给包含10个元素的数组 
	
    } 
	
RadixSort(x,0,9);



	for(i=0;i<10;i++ ) 
    {   
		
	      s.Format("%d",x[i]); 
          GetDlgItem(a[i+10] )->SetWindowText(s); 
	
    }                   //将排序后的数组元素赋给右边的编辑框 
        m_clear.EnableWindow(TRUE);   //启用”清空数据”按扭 
}

void CSortDlg::OnHeapSort() //堆排序
{
	// TODO: Add your control notification handler code here
	int x[10];         CString s; 
	int a[20]={IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,IDC_EDIT4,IDC_EDIT5,IDC_EDIT6
	 ,IDC_EDIT7,IDC_EDIT8,IDC_EDIT9,IDC_EDIT10,IDC_EDIT11,IDC_EDIT12,IDC_EDIT13,
	 IDC_EDIT14,IDC_EDIT15,IDC_EDIT16,IDC_EDIT17,IDC_EDIT18,IDC_EDIT19,IDC_EDIT20};
    for(int i=0;i<10;i++ ) 
    {     
	
	       GetDlgItem(a[i] )->GetWindowText(s); 
           x[i ]= atoi(s); //将编辑框中的字符转化为数字后对应的赋给包含10个元素的数组 
    } 
	
HeapSort(x,10);



	for(i=0;i<10;i++ ) 
    {  
	      s.Format("%d",x[i]); 
          GetDlgItem(a[i+10] )->SetWindowText(s); 
	
    }                   //将排序后的数组元素赋给右边的编辑框 
        m_clear.EnableWindow(TRUE);   //启用”清空数据”按扭 
}

void CSortDlg::OnDirectChooseSort() 
{
	// TODO: Add your control notification handler code here
		int x[10];         CString s; 
	int a[20]={IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,IDC_EDIT4,IDC_EDIT5,IDC_EDIT6
	 ,IDC_EDIT7,IDC_EDIT8,IDC_EDIT9,IDC_EDIT10,IDC_EDIT11,IDC_EDIT12,IDC_EDIT13,
	 IDC_EDIT14,IDC_EDIT15,IDC_EDIT16,IDC_EDIT17,IDC_EDIT18,IDC_EDIT19,IDC_EDIT20};
    for(int i=0;i<10;i++ ) 
    {     
		
	       GetDlgItem(a[i])->GetWindowText(s); 
           x[i ]= atoi(s); //将编辑框中的字符转化为数字后对应的赋给包含10个元素的数组 
		
    } 
	
SelectSort(x);



	for(i=0;i<10;i++ ) 

⌨️ 快捷键说明

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