📄 quicksortdlg.cpp
字号:
// QuickSortDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ArithmeticDemo.h"
#include "QuickSortDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CQuickSortDlg dialog
CQuickSortDlg::CQuickSortDlg(CWnd* pParent /*=NULL*/)
: CDialog(CQuickSortDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CQuickSortDlg)
m_nSize = 0;
//}}AFX_DATA_INIT
m_BKBrush.CreateSolidBrush(RGB(171,187,222));
}
void CQuickSortDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CQuickSortDlg)
DDX_Text(pDX, IDC_NUM, m_nSize);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CQuickSortDlg, CDialog)
//{{AFX_MSG_MAP(CQuickSortDlg)
ON_BN_CLICKED(IDC_STARTSORT, OnStartsort)
ON_BN_CLICKED(IDC_CLEANBTN, OnCleanbtn)
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CQuickSortDlg message handlers
void CQuickSortDlg::GetElements(CString str,int nSize,CStringArray& list)
{
str.TrimLeft();
str.TrimRight();
list.RemoveAll();
list.SetSize(nSize);
for(int i=0;i<nSize;i++)
{
list.SetAt(i,'0');
}
int nIndex;
int nTotalNum=0;
CString strTemp;
while(str.GetLength()>0&&nTotalNum<nSize)
{
nIndex=str.Find(' ');
if(nIndex!=-1)
{
strTemp=str.Left(nIndex);
list.SetAt(nTotalNum,strTemp);
int m=str.GetLength();
int n=strTemp.GetLength();
str=str.Right(m-n);
str.TrimLeft();
nTotalNum++;
}
else
{
list.SetAt(nTotalNum,str);
str.Empty();
nTotalNum++;
}
}
}
BOOL CQuickSortDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
((CComboBox*)GetDlgItem(IDC_TYPE))->AddString("int");
((CComboBox*)GetDlgItem(IDC_TYPE))->AddString("double");
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CQuickSortDlg::OnStartsort()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_RESULT)->SetWindowText("");
GetDlgItem(IDC_STARTSORT)->EnableWindow(FALSE);
UpdateData(TRUE);
CString strElements;
GetDlgItem(IDC_ELEMENT)->GetWindowText(strElements);
if(strElements.IsEmpty()||m_nSize<=0)
{
AfxMessageBox("请输入必要的数据");
GetDlgItem(IDC_STARTSORT)->EnableWindow(TRUE);
return;
}
int nType;
CComboBox* pTypeList=(CComboBox*)GetDlgItem(IDC_TYPE);
nType=pTypeList->GetCurSel();
CStringArray list;
GetElements(strElements,m_nSize,list);
CString strResult;
switch(nType)
{
case 0:
{
CSort Sort(m_nSize);
int m_nArraySize=Sort.m_nArraySize;
int* iList=new int[m_nArraySize];
if(iList==NULL)
{
GetDlgItem(IDC_STARTSORT)->EnableWindow(TRUE);
AfxMessageBox("分配内存失败");
return;
}
for(int i=0;i<list.GetSize();i++)
{
iList[i]=atoi(list.GetAt(i));
}
Sort.QuickSort(iList,0,m_nArraySize-1);
CString strTemp;
for(i=0;i<m_nArraySize;i++)
{
strTemp.Format("%d",iList[i]);
strResult+=strTemp;
strResult+=' ';
}
delete[] iList;
}
break;
case 1:
{
CSort Sort(m_nSize);
int m_nArraySize=Sort.m_nArraySize;
double* dList=new double[m_nArraySize];
if(dList==NULL)
{
GetDlgItem(IDC_STARTSORT)->EnableWindow(TRUE);
AfxMessageBox("分配内存失败");
return;
}
for(int i=0;i<list.GetSize();i++)
{
dList[i]=atof(list.GetAt(i));
}
Sort.QuickSort(dList,0,m_nArraySize-1);
CString strTemp;
for(i=0;i<m_nSize;i++)
{
strTemp.Format("%.3f",dList[i]);
strResult+=strTemp;
strResult+=' ';
}
delete[] dList;
}
break;
}
GetDlgItem(IDC_RESULT)->SetWindowText(strResult);
GetDlgItem(IDC_STARTSORT)->EnableWindow(TRUE);
}
void CQuickSortDlg::OnCleanbtn()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_TYPE)->SetWindowText("");
GetDlgItem(IDC_NUM)->SetWindowText("");
GetDlgItem(IDC_ELEMENT)->SetWindowText("");
GetDlgItem(IDC_RESULT)->SetWindowText("");
}
HBRUSH CQuickSortDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(nCtlColor==CTLCOLOR_DLG)
return (HBRUSH)m_BKBrush.GetSafeHandle();
// TODO: Return a different brush if the default is not desired
return hbr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -