📄 sortview.cpp
字号:
// sortView.cpp : implementation of the CSortView class
//
#include "stdafx.h"
#include "sort.h"
#include "sortDoc.h"
#include "sortView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSortView
IMPLEMENT_DYNCREATE(CSortView, CView)
BEGIN_MESSAGE_MAP(CSortView, CView)
//{{AFX_MSG_MAP(CSortView)
ON_COMMAND(ID_MYINPUT, OnMyinput)
ON_COMMAND(ID_MYSORT, OnMysort)
ON_COMMAND(ID_MYSTEPSORT, OnMystepsort)
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSortView construction/destruction
CSortView::CSortView()
{
rectSort=CRect(50,100,550,160);
rectInfo=CRect(50,180,550,240);
round=0;
compare=0;
exchange=0;
m_nCount=10;
index=m_nCount-1;
for(int i=0;i<10;i++)
m_nNew[i]=m_nOld[i]=10-i;
iStep=0;
}
CSortView::~CSortView()
{
}
BOOL CSortView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CSortView drawing
void CSortView::OnDraw(CDC* pDC)
{
CSortDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->Rectangle(rectSort);
pDC->Rectangle(rectInfo);
pDC->TextOut(60,110,"数组序号:");
pDC->TextOut(60,140,"排序序列:");
CString str;
str.Format("当前:第%d轮,第%d次.",round+1,m_nCount-index);
pDC->TextOut(60,190,str);
str.Format("比较总次数:%d,交换次数:%d.",compare,exchange);
pDC->TextOut(60,220,str);
for(int i=0;i<m_nCount;i++)
{
str.Format("%d",i);
pDC->TextOut(150+i*40,110,str);
str.Format("%d",m_nNew[i]);
pDC->TextOut(150+i*40,140,str);
}
}
/////////////////////////////////////////////////////////////////////////////
// CSortView printing
BOOL CSortView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSortView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSortView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CSortView diagnostics
#ifdef _DEBUG
void CSortView::AssertValid() const
{
CView::AssertValid();
}
void CSortView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSortDoc* CSortView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSortDoc)));
return (CSortDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSortView message handlers
void CSortView::OnMyinput()
{
CInputDlg dlg;
int nRet=dlg.DoModal();
if(nRet=IDOK)
{
m_nCount=dlg.m_nNum;
m_nOld[0]=dlg.m_nX1;
m_nOld[1]=dlg.m_nX2;
m_nOld[2]=dlg.m_nX3;
m_nOld[3]=dlg.m_nX4;
m_nOld[4]=dlg.m_nX5;
m_nOld[5]=dlg.m_nX6;
m_nOld[6]=dlg.m_nX7;
m_nOld[7]=dlg.m_nX8;
m_nOld[8]=dlg.m_nX9;
m_nOld[9]=dlg.m_nX10;
for(int i=0;i<10;i++)
m_nNew[i]=m_nOld[i];
index=m_nCount-1;
Invalidate();
}
}
void CSortView::OnMysort()
{
SetTimer(1,1000,NULL);
round=0;
compare=0;
exchange=0;
iStep=0;
index=m_nCount-1;
for(int i=0;i<m_nCount;i++)
m_nNew[i]=m_nOld[i];
Invalidate();
}
void CSortView::OnMystepsort()
{
iStep=1;
round=0;
compare=0;
exchange=0;
index=m_nCount-1;
for(int i=0;i<m_nCount;i++)
m_nNew[i]=m_nOld[i];
}
void CSortView::OnTimer(UINT nIDEvent)
{
if(round<m_nCount)
{
if(index>round)
{
if(m_nNew[index-1]>m_nNew[index])
{
int temp=m_nNew[index-1];
m_nNew[index-1]=m_nNew[index];
m_nNew[index]=temp;
exchange++;
}
compare++;
index--;
}
else
{
index=m_nCount-1;
round++;
}
}
else
{
KillTimer(1);
MessageBox("冒泡排序完成!");
}
Invalidate();
CView::OnTimer(nIDEvent);
}
void CSortView::OnLButtonDown(UINT nFlags,CPoint point)
{
if(iStep==1)
OnTimer(1);
CView::OnLButtonDown(nFlags,point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -