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

📄 mfcdlg.cpp

📁 六种排序算法的比较 冒泡排序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MFCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFC.h"
#include "MFCDlg.h"
#include "EditDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

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

/////////////////////////////////////////////////////////////////////////////
// CMFCDlg dialog

CMFCDlg::CMFCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMFCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMFCDlg)
	m_compare = _T("");
	m_change = _T("");
	m_compare1 = _T("");
	m_change1 = _T("");
	m_compare2 = _T("");
	m_change2 = _T("");
	m_size = 10;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMFCDlg)
	DDX_Control(pDX, IDC_SHUZU, m_list);
	DDX_Text(pDX, IDC_EDIT1, m_compare);
	DDX_Text(pDX, IDC_EDIT2, m_change);
	DDX_Text(pDX, IDC_EDIT3, m_compare1);
	DDX_Text(pDX, IDC_EDIT4, m_change1);
	DDX_Text(pDX, IDC_EDIT5, m_compare2);
	DDX_Text(pDX, IDC_EDIT6, m_change2);
	DDX_Text(pDX, IDC_EDIT8, m_size);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMFCDlg, CDialog)
	//{{AFX_MSG_MAP(CMFCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_COMPARE, OnCompare)
	ON_BN_CLICKED(IDC_MAKERAND, OnMakerand)
	ON_BN_CLICKED(IDC_INSERTSORT, OnInsertsort)
	ON_BN_CLICKED(IDC_CLEARRESULT, OnClearresult)
	ON_BN_CLICKED(IDC_CLEARALL, OnClearall)
	ON_BN_CLICKED(IDC_SELECTSORT, OnSelectsort)
	ON_BN_CLICKED(IDC_BUBBLESORT, OnBubblesort)
	ON_BN_CLICKED(IDC_SHELLSORT, OnShellsort)
	ON_BN_CLICKED(IDC_QUICKSORT, OnQuicksort)
	ON_BN_CLICKED(IDC_HEAPSORT, OnHeapsort)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFCDlg message handlers

BOOL CMFCDlg::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
	Arrary = new int*[3];
	max = 50;
	Rand_Biao = 0;
	for(int i = 0 ; i < 2;i++)
	{
		for(int j = 0;j < 7;j++)
		{
			for(int k = 0;k < 3;k++)
				result[k][i][j] = 0;
		}
	}
	compare[0] = 0;
	compare[1] =0;
	shuzu[0] = "第一组";
	shuzu[1] = "第二组";
	shuzu[2] = "第三组";
	jilu[0] = "比较次数";
	jilu[1] = "交换次数";
	temp1 = new int[max];
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	m_list.InsertColumn(0,"序列",LVCFMT_LEFT,40);
	m_list.InsertColumn(1,"第一组",LVCFMT_LEFT,50);
	m_list.InsertColumn(2,"排序结果",LVCFMT_LEFT,60);
	m_list.InsertColumn(3,"第二组",LVCFMT_LEFT,50);
	m_list.InsertColumn(4,"排序结果",LVCFMT_LEFT,60);
	m_list.InsertColumn(5,"第三组",LVCFMT_LEFT,50);
	m_list.InsertColumn(6,"排序结果",LVCFMT_LEFT,60);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMFCDlg::OnCompare() 
{
	// TODO: Add your control notification handler code here	
	UpdateData(true);
	max = m_size;
	if(max == 0)
	{
		MessageBox("请输入随机数个数!");
		return;
	}
	OnMakerand();
	OnQuicksort();
	OnInsertsort();
	OnSelectsort();
	OnBubblesort();
	OnShellsort();
	OnQuicksort();
    OnHeapsort();
	CEditDlg ed;
	ed.mm = 10;
	ed.shuzu = shuzu;
	ed.jilu = jilu;
    for(int i = 0 ; i < 2;i++)
	{
		for(int j = 0;j < 7;j++)
		{
			for(int k = 0;k < 3;k++)
				ed.result[k][i][j].Format("%d",result[k][i][j]);
		}
	}
	ed.DoModal();
//	OnClearall();
	
}

void CMFCDlg::OnMakerand() 
{
	// TODO: Add your control notification handler code here
	//对于已经产生了随机数的情况,先清空然后再生成随机数
	if(Rand_Biao!=0&&max!=0) 
	{
		OnClearall();
	}
	static int m = 0;
	UpdateData(true);
	max = m_size;
	if(max == 0)
	{
		MessageBox("请输入随机数个数!");
	}
	for(int j = 0;j < 3&&Rand_Biao==0&&max!=0;j++)
	{
		m++;
	    srand(m + (unsigned)time(NULL));
		arrary = new int[max];
		Arrary[j] = arrary;
	    for(int i = 0 ; i < max;i++)
		{
		    arrary[i] = rand()%100;
		}
	}
	for(int i = 0 ; i < max&&Rand_Biao==0&&max!=0;i++)
	{
		aa.Format("%d",i);
        m_list.InsertItem(i,aa);
	    aa.Format("%d",*(Arrary[0]+i));
	    m_list.SetItemText(i,1,aa);    
		aa.Format("%d",*(Arrary[1]+i));
		m_list.SetItemText(i,3,aa);
		aa.Format("%d",*(Arrary[2]+i));
		m_list.SetItemText(i,5,aa);
	}
	if(max!=0)
		Rand_Biao = 1;
}

void CMFCDlg::Copy(int *arrary,int *temp,int n)
{
	for(int i = 0;i < n;i++)
	{
		temp[i] = arrary[i];
	}
}
void CMFCDlg::OnInsertsort() 
{
	// TODO: Add your control notification handler code here
	static int pass = 0;
	int *temp,kk;
	for(kk = 0;kk < 3;kk++)
	{
		result[kk][0][1] = 0;
		result[kk][1][1] = 0;
	}
	
	if(Rand_Biao == 0)
	{
		MessageBox("请先产生随机数");
	}
	else
	{
		m_list.SetTextBkColor(RGB(200,200,255));
	}
	for(kk = 0;kk < 3&&Rand_Biao!=0;kk++)
	{
		temp1 = Arrary[kk];
		arrary = new int[max];
		Copy(temp1,arrary,max);
		temp = new int[max+1];
	    temp[0] = 0;
		int i = 0;
	    for(i = 1;i < max+1;i++)
		{
		    temp[i] = arrary[i-1];
		}
        for(i = 2;i <= max ;i++)
		{
		    result[kk][0][1]+=2;
		    if(temp[i] < temp[i-1])
			{
		    	temp[0] = temp[i];
		    	temp[i] = temp[i-1];
				int j = 0;
		    	for(j = i-2;temp[0]<temp[j];j--)
				{
			    	result[kk][0][1]++;
			    	temp[j+1] = temp[j];
				}
		    	temp[j + 1] = temp[0];
			}
		}
	    for(i = 0;i < max;i++)
	    	arrary[i] = temp[i+1];
	    for(i = 0 ; i < max;i++)
		{
	    	aa.Format("%d",arrary[i]);
			m_list.Update(i);
	    	m_list.SetItemText(i,2*(kk+1),aa);
		}
	}
	m_compare.Format("%d", result[0][0][1]);
	m_change.Format("%d", result[0][1][1]);
	m_compare1.Format("%d",result[1][0][1]);
	m_change1.Format("%d",result[1][1][1]);
    m_compare2.Format("%d",result[2][0][1]);
	m_change2.Format("%d",result[2][1][1]);
	UpdateData(false);
	
}

void CMFCDlg::OnClearresult() 
{
	// TODO: Add your control notification handler code here
	for(int k = 1;k <= 3;k++)
	{
		for(int i = 0; i <max;i++)
		{
			m_list.SetItemText(i,2*k," ");
		}
	}

	
}

void CMFCDlg::OnClearall() 

⌨️ 快捷键说明

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