欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

easyx实现动态绘图——排序过程

程序员文章站 2024-03-18 21:24:58
...

是动态显示排序过程的~

白底:实时排序
黄底:已经完成的排序
无底色:还未完成

插入排序

easyx实现动态绘图——排序过程

冒泡排序

easyx实现动态绘图——排序过程

选择排序

easyx实现动态绘图——排序过程
easyx实现动态绘图——排序过程

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int len=0;
#define MAXSIZE 13
int n;

int main()
{
	int a[10];
	n=10;
	void create_array(int *a);
	void out(int *a);
	void InsertSort(int a[]);
	void BuddleSort(int R[]);
	void SELECTSORT(int R[]);
	void menu(int *a);

	create_array(a);
	//out(a);
	//getchar();
	//cleardevice();
	//background();
	//BuddleSort(a);
	//InsertSort(a);
	//SELECTSORT(a);
	menu(a);
	getchar();
	return 0;
}


void InsertSort(int a[])
{
	void out(int *a);
	void InsertSort_draw(int *a,int len_ok,int insert_number);
	initgraph(800, 600);
	int i, j;
	for (i = 2; i < n; i++)
	{
		if (a[i] < a[i - 1])
		{
			a[0] = a[i];
			for (j = i - 1; (a[j]>a[0])&&j>=0; j--)
			{
				a[j + 1] = a[j];
			}
			a[j + 1] = a[0];
			InsertSort_draw(a, i, j + 1);
		}
		else
		{
			InsertSort_draw(a, i, i);
		}
		Sleep(2000);
	}
}


/*随机生成0-100的整数*/
void create_array(int *a)
{
	srand(time(NULL));
	int i;
	for(i=1;i<=10;i++)
	{
		a[i]=rand()%101;
	}
	len=10;

}

void background()
{
	setbkcolor(RGB(211, 51, 29));
	cleardevice();

	setfillcolor(WHITE);
	fillrectangle(50, 240, 729, 250);
	fillrectangle(50, 300, 729, 310);
	fillrectangle(40, 240, 50, 310);

	fillrectangle(93, 240, 103, 310);
	fillrectangle(146, 240, 156, 310);
	fillrectangle(199, 240, 209, 310);
	fillrectangle(252, 240, 262, 310);
	fillrectangle(305, 240, 315, 310);
	fillrectangle(358, 240, 368, 310);
	fillrectangle(411, 240, 421, 310);
	fillrectangle(464, 240, 474, 310);
	fillrectangle(517, 240, 527, 310);
	fillrectangle(570, 240, 580, 310);
	fillrectangle(623, 240, 633, 310);
	fillrectangle(676, 240, 686, 310);
	fillrectangle(729, 240, 739, 310);

	settextcolor(WHITE);
	setbkcolor(RGB(211, 51, 29));
	settextstyle(36, 18, _T("宋体"));

	setlinecolor(RGB(211, 51, 29));
	setfillcolor(RGB(221, 100, 83));
	solidroundrect(275, 410, 525, 500, 80, 160);
	setbkcolor(RGB(221, 100, 83));
	setfillcolor(RGB(221, 100, 83));
	outtextxy(310, 437, "返回主菜单");

	settextstyle(18, 8, _T("宋体"));
	setbkcolor(RGB(211, 51, 29));
	settextcolor(WHITE);
	setlinecolor(RGB(211, 51, 29));
	setfillcolor(RGB(211, 51, 29));
}

void InsertSort_draw(int *a,int len_ok,int insert_number)
{
	//MOUSEMSG m;
	char s[5];
	int i;
	int x,y;
	void menu(int *a);
	FlushMouseMsgBuffer();
	x=0;
	y=0;
	cleardevice();
	background();
	for(i=1;i<=n;i++)
	{
		sprintf(s, "%d", a[i]);
		x = 64 + (i-1) * 53;
		y = 270;
		if (i <= len_ok)
		{
			if (i == insert_number)
			{
				setbkcolor(WHITE);
				settextcolor(RED);
				outtextxy(x, y, s);
			}
			else
			{
				setbkcolor(YELLOW);
				settextcolor(BLACK);
				outtextxy(x, y, s);
			}

		}
		else
		{
			setbkcolor(RGB(211, 51, 29));
			settextcolor(WHITE);
			outtextxy(x, y, s);
		}
		
	}

	settextcolor(WHITE);
	settextstyle(36,18, _T("宋体"));
	setbkcolor(RGB(211,51,29));
	outtextxy(300,150,"插入排序");

}



int check_button(int x,int y)
{
	if(x>150 && x<650 && y>100 && y<200)
		return 1;
	else if(x>150 && x<650 && y>200 && y<300)
		return 2;
	else if(x>185 && x<615 && y>300 && y<400)
		return 3;
	else if(x>400 && x<600 && y>400 && y<500)
		return 4;
	else 
		return -1;
} 
 
void event_mouse(int *a)
{
	void create_array(int *a);
	void out(int *a);
	void InsertSort(int a[]);
	void BuddleSort(int R[]);
	void SELECTSORT(int R[]);
	void out(int *a);
	void menu(int *a);
	MOUSEMSG m;

	while(1)
	{
	m=GetMouseMsg();                         
	if(m.uMsg == WM_LBUTTONDOWN) 
		if(check_button(m.x,m.y)==1)
			{
				create_array(a);
				InsertSort(a);
				out(a);
				//menu();
				//closegraph();
				break;		
			}
		else if(check_button(m.x,m.y)==2)
			{
				create_array(a);
				BuddleSort(a);
				out(a);
				break;
			}
		else if(check_button(m.x,m.y)==3)
			{
				create_array(a);
				SELECTSORT(a);
				out(a);
				break;
			}
		else if(check_button(m.x,m.y)==4)
			{
				exit(0);
				closegraph();
				break;
			}
		
	}
}

void menu(int *a)
{
	void event_mouse(int *a);
	initgraph(800,600);
	setbkcolor(WHITE); 
	cleardevice();
	//MOUSEMSG m;
	//FlushMouseMsgBuffer();
	setfillcolor(RGB(191,191,191));
	setlinecolor(RGB(191,191,191)); 
	ellipse(150,100,650,500);
	line(150,300,650,300);
	line(185,200,615,200);
	line(185,400,615,400);
	settextcolor(WHITE);
	settextstyle(36,18, _T("宋体"));


	setfillcolor(RGB(250,209,150));
	setlinecolor(RGB(250,209,150)); 
	floodfill(250,250,RGB(191,191,191));
	setbkcolor(RGB(250,209,150));
	outtextxy(270,230,"2.冒泡排序");


	setfillcolor(RGB(158,213,232));
	setlinecolor(RGB(158,213,232)); 
	floodfill(380,160,RGB(191,191,191));
	setbkcolor(RGB(158,213,232));
	outtextxy(270,140,"1.直接插入排序");

	setfillcolor(RGB(136,177,163));
	setlinecolor(RGB(136,177,163)); 
	floodfill(500,350,RGB(191,191,191));
	setbkcolor(RGB(136,177,163));
	outtextxy(270,330,"3.直接选择排序");

	setfillcolor(RGB(249,146,145));
	setlinecolor(RGB(249,146,145)); 
	floodfill(450,450,RGB(191,191,191));
	setbkcolor(RGB(249,146,145));
	outtextxy(270,430,"4.退出");

	setfillcolor(RGB(213,23,23));
	fillrectangle(750,0,800,50);
	setfillcolor(WHITE);
	POINT pts1[] = { {751,0},{800,49},{799, 50}, {750,1},{751,0}};
	solidpolygon(pts1, 4);
	POINT pts2[] = { {799,0},{800,1},{751, 50}, {750,49},{799,0}};
	solidpolygon(pts2, 4);
	event_mouse(a);
}


void BuddleSort(int a[])
{
	void BuddleSort_draw(int *a,int len_ok,int max_temp);
	int i,j,flag;
	initgraph(800, 600);
	for (i = n-1; i >0; i--)
	{
		flag = 0;
		for (j = 1; j <= i; j++)
		{
			if (a[j] >= a[j + 1])
			{
				a[0] = a[j];
				a[j] = a[j + 1];
				a[j + 1] = a[0];
				flag = 1;
				BuddleSort_draw(a,i+1,j+1);
				Sleep(2000);
			}
		}
		if (flag == 0)
			break;
	}
}


void BuddleSort_draw(int *a,int len_ok,int max_temp)
{
	MOUSEMSG m;
	char s[5];
	int i;
	int x,y;
	void menu(int *a);
	FlushMouseMsgBuffer();
	x=0;
	y=0;
	cleardevice();
	background();
	for(i=1;i<=n;i++)
	{
		sprintf(s, "%d", a[i]);
		x = 64 + (i-1) * 53;
		y = 270;
	
		if(i == max_temp)
		{
			setbkcolor(WHITE);
			settextcolor(RED);
			outtextxy(x, y, s);
		}
		else if(i>len_ok)
		{
			setbkcolor(YELLOW);
			settextcolor(BLACK);
			outtextxy(x, y, s);
		}

		else
		{
			setbkcolor(RGB(211, 51, 29));
			settextcolor(WHITE);
			outtextxy(x, y, s);
		}
		
	}

	settextcolor(WHITE);
	settextstyle(36,18, _T("宋体"));
	setbkcolor(RGB(211,51,29));
	outtextxy(300,150,"冒泡排序");

}



void SELECTSORT(int a[])
{
	int i,j;
	void SELECTSORT_draw(int *a,int len_ok,int max_temp);
	initgraph(800, 600);
	int k;
	for (i = 1; i < n; i++)
	{
		k = i;
		for (j = i + 1; j <= n; j++)
		{
			if (a[j] < a[k])
			{
				k = j;
			}
			SELECTSORT_draw(a,i,k);
			Sleep(1000);
		}
		if (i != k)
		{
			a[0] = a[i];
			a[i] = a[k];
			a[k] = a[0];
		}
		SELECTSORT_draw(a,i,i);
		Sleep(2000);
	}
}

void SELECTSORT_draw(int *a,int len_ok,int min_temp)
{
	MOUSEMSG m;
	char s[5];
	int i;
	int x,y;
	void menu(int *a);
	FlushMouseMsgBuffer();
	x=0;
	y=0;
	cleardevice();
	background();
	for(i=1;i<=n;i++)
	{
		sprintf(s, "%d", a[i]);
		x = 64 + (i-1) * 53;
		y = 270;
	
		if(i == min_temp)
		{
			setbkcolor(WHITE);
			settextcolor(RED);
			outtextxy(x, y, s);
		}
		else if(i>=0&&i<len_ok)
		{
			setbkcolor(YELLOW);
			settextcolor(BLACK);
			outtextxy(x, y, s);
		}
		else
		{
			setbkcolor(RGB(211, 51, 29));
			settextcolor(WHITE);
			outtextxy(x, y, s);
		}
		
	}

	settextcolor(WHITE);
	settextstyle(36,18, _T("宋体"));
	setbkcolor(RGB(211,51,29));
	outtextxy(300,150,"选择排序");

}



void out(int *a)
{
	char s[5];
	int i;
	int x,y;
	void menu(int *a);
	x=0;
	y=0;

	initgraph(800,600);
	setbkcolor(RGB(211,51,29)); 
	cleardevice();
	MOUSEMSG m;
	FlushMouseMsgBuffer();

	setfillcolor(WHITE);
	fillrectangle(50,240,729,250);
	fillrectangle(50,300,729,310);
	fillrectangle(40,240,50,310);

	fillrectangle(93,240,103,310);
	fillrectangle(146,240,156,310);
	fillrectangle(199,240,209,310);
	fillrectangle(252,240,262,310);
	fillrectangle(305,240,315,310);
	fillrectangle(358,240,368,310);
	fillrectangle(411,240,421,310);
	fillrectangle(464,240,474,310);
	fillrectangle(517,240,527,310);
	fillrectangle(570,240,580,310);
	fillrectangle(623,240,633,310);
	fillrectangle(676,240,686,310);
	fillrectangle(729,240,739,310);

	settextcolor(WHITE);
	setbkcolor(RGB(211,51,29));
	settextstyle(36,18, _T("宋体"));

	setlinecolor(RGB(211,51,29));
	setfillcolor(RGB(221,100,83));
	solidroundrect(275,410,525,500,80,160);
	setbkcolor(RGB(221,100,83));
	setfillcolor(RGB(221,100,83));
	outtextxy(310,437,"返回主菜单");

	settextstyle(18, 8, _T("宋体"));
	setbkcolor(RGB(211,51,29));
	settextcolor(WHITE);
	setlinecolor(RGB(211,51,29)); 
	setfillcolor(RGB(211,51,29));
	for(i=0;i<len;i++)
	{
		sprintf(s, "%d", a[i]);
		x = 64+i*53;
		y = 270;
		outtextxy(x,y,s);
	}

	settextcolor(WHITE);
	settextstyle(36,18, _T("宋体"));
	setbkcolor(RGB(211,51,29));
	outtextxy(300,150,"完成排序");

	while(1)
	{
		while (MouseHit())
		{
			m = GetMouseMsg();
			if(m.x<=525 && m.x>=275 && m.y<=500 && m.y>=410)
			{
				switch(m.uMsg)	
				{
					case WM_LBUTTONDOWN: 
						menu(a);
						break;
					default:
						break;
				}

			}
		}		
	}
	

}
相关标签: 作业