巧用C语言“栈”实现Hanoi塔的动态显示
程序清单:
#include <graphics.h>
#include <stdlib.h>
void init_h(void);
void pop_push();
void push_h();
int pop_h();
int *block;
int H,i,n,ErrorCode;
int *bos,*tos;
/*金片移动的递归调用函数*/
movetower(h,f,t,u)
int h,f,t,u;
{
if (h==1) {
i++;
pop_push(f,t);
getch();
}
else {
movetower(h-1,f,u,t);
i++;
pop_push(f,t);
getch();
movetower(h-1,u,t,f);
}
}
/*金片移动函数*/
void pop_push(a,b)
int a,b;
{
int k;
k=pop_h(a);
push_h(b,k);
}
/*金片弹出显示函数*/
pop_h(a)
int a;
{
int r,hh;
tos--;
r=*tos;
hh=tos-bos+1;
putimage(a*100,300-hh*10,block,XOR_PUT);
return(r);
}
/*金片压入显示函数*/
void push_h(b,k)
int b,k;
{
int r,hh;
*tos=k;
tos++;
hh=tos-bos;
putimage(b*100,300-hh*10,block,XOR_PUT);
}
/*图形及栈初始化函数*/
void init_h(vlid)
{
int gd=DETECT,gm=0,size,i,x1,x2,y1,y1,hh;
initgraph(&gd,&gm,"");
size=imagesize(0,0,100,10);
for (i=1;i<=h;i++)
block=malloc(size);
ErrorCode=graphresult();
if (grOK!=ErrorCode) {
printf("graphics System Error:%s\n",grapherrormsg(ErrorCode));
exit(1);
}
cleardevice();
setcolor(15);
line(0,301,100,301);
line(50,301,50,150);
for (i=H;i>0;i--) {
x1=50-i*10/2;
y1=310-(H+1-i)*10;
x2=50+i*10/2;
y2=302-(h+1-i)*10;
setfillstyle(1,(H+1-i));
bar(x1,y1,x2,y2);
getimage(0,y1,99,y2,block);
}
cleardevice();
line(0,301,300,301);
line(50,301,50,150);
line(150,301,150,150);
line(250,301,250,150);
for (i=0;i<3;i++) {
bos=(int *)malloc(H*sizeof(int));
tos=bos;
}
for (i=H;i>0;i--) {
*tos=i;
hh=tos-bos;
putimage(0,290-hh*10,block,XOR_PUT);
tos++;
}
getch();
}
main()
{
int a=0,b=1,c=2;
i=0;
printf("intput n(n<=8):");
scanf("%d",&n);
H=n;
init_h();
movetower(n,a,b,c);
printf("\t The End !\n\n");
closegraph();
}
(黑龙江 马友华)