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

📄 sort.cpp

📁 包含八种排序算法的界面演示多线程程序,以线段形式表示数据大小,排序一清二楚
💻 CPP
字号:
// Sort.cpp: implementation of the CSort class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "sort.h"
#include "Sort.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSort::CSort()
{
	p=NULL;
	IsCreated=FALSE;
	CanSleep=TRUE;
	IsASC=TRUE;
}

CSort::~CSort()
{
	if (p) delete[] p;
}

void CSort::Draw()
{
//	if (!p) return;
	if (!IsCreated) return;
	int w=(rect.bottom-rect.top)/(2*n);
	if (w==0) w=1;
	if (w>4) w=4;

	tdc.BitBlt(0,0,rect.right-rect.left+6,rect.bottom-rect.top+TextHeight,NULL,0,0,WHITENESS);
	if (n<rect.bottom-rect.top)
	for (int i=0;i<n;i++)
	{
		int length=(p[i]-base+1)*(rect.right-rect.left)/width;
		int red=(p[i]-base)*255/width;
		int green=255-red;
		int y=(rect.bottom-rect.top)*i/n;
		CPen pen;
		pen.CreatePen(PS_SOLID,w,RGB(red,green,0));
		(tdc).SelectObject(&pen);
		if (IsASC)
		{
			(tdc).MoveTo(2,y);
			(tdc).LineTo(length,y);
		}
		else
		{
			tdc.MoveTo(2,rect.bottom-rect.top-y);
			tdc.LineTo(length,rect.bottom-rect.top-y);
		}
	}
	else
	for (int i=0;i<rect.bottom-rect.top;i++)
	{
		int y=n*i/(rect.bottom-rect.top);
		int length=(p[y]-base+1)*(rect.right-rect.left)/width;
		int red=(p[y]-base)*255/width;
		int green=255-red;
		CPen pen;
		pen.CreatePen(PS_SOLID,w,RGB(red,green,0));
		(tdc).SelectObject(&pen);
		if (IsASC)
		{
			(tdc).MoveTo(2,i);
			(tdc).LineTo(length,i);
		}
		else
		{
			tdc.MoveTo(2,rect.bottom-rect.top-i);
			tdc.LineTo(length,rect.bottom-rect.top-i);
		}
	}
	if (State2!=2) text1.Format("%s排序未完成",name);else text1.Format("%s排序已完成",name);
	text2.Format("用时%d毫秒",NowTime);
	tdc.TextOut(0,rect.bottom-rect.top+1,text1);
	tdc.TextOut(0,rect.bottom-rect.top+16,text2);
	pdc->BitBlt(rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top+TextHeight,&tdc,0,0,SRCCOPY);

}

void CSort::Set(CWnd *parent, int nn, RECT *r, int b, int l,int sleep)
{
	base=b;
	width=l;
	pdc=new CClientDC(parent);
	if (p&&n!=nn) {delete []p;p=NULL;}
	n=nn;
	if (!p)	p=new int[n];
	memcpy(&rect,r,sizeof(rect));
	NowTime=0;
	State=0;
	if (bitmap.m_hObject) bitmap.DeleteObject();
	bitmap.CreateCompatibleBitmap(pdc,rect.right-rect.left+10,rect.bottom-rect.top+10+TextHeight);
	if (tdc.m_hDC==NULL) tdc.CreateCompatibleDC(pdc);
	tdc.SelectObject(&bitmap);
	IsCreated=TRUE;
	Sleeptime=sleep;
}

void CSort::SetExitEvent()
{
	State2=-1;
}

void CSort::SetStartEvent()
{
	State2=1;
}

void CSort::Sleep()
{
//	int a=0;
//	for (int i=0;i<10000;i++) a+=1;
	::Sleep(Sleeptime);
}


void CSort::SetRect(RECT *r)
{
	memcpy(&rect,r,sizeof(rect));
}

void CSort::SetData(int *data)
{
	memcpy(p,data,4*n);
}

BOOL CSort::IsRunning()
{
	return (State2==1);
}

void CSort::SetTime(DWORD t)
{
	NowTime=t;
}

void CSort::SetState2_3()
{
	State2=3;
}

void CSort::EnableSleep(BOOL b)
{
	CanSleep=b;
}

⌨️ 快捷键说明

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