/*stack_hanoi.h 汉诺塔演示程序专用头文件,其他内容同纪念版的栈。 */ typedef struct { ElementType *buffer; int max; int top; char name; int row; int col; }Stack; void PutStack(Stack *sp) { int i; *(char far *)(0xb8000000+160*sp->row+2*sp->col)=sp->name; *(char far *)(0xb8000000+160*sp->row+2*sp->col+1)=0x07; for(i=0;i<=sp->top;i++) { *(char far *)(0xb8000000+160*(sp->row-i-1)+2*sp->col)=sp->buffer[i]; *(char far *)(0xb8000000+160*(sp->row-i-1)+2*sp->col+1)=7; } } /*hanoi.c*/ #include"stack_hanoi.h" void move(int n,Stack *x,Stack *y,Stack *z) { char a; if(n<1) return; if(n==1) { Pop(x,&a); Push(z,a); getch(); clrscr(); PutStack(x); PutStack(y); PutStack(z); return; } move(n-1,x,z,y); Pop(x,&a); Push(z,a); getch(); clrscr(); PutStack(x); PutStack(y); PutStack(z); move(n-1,y,x,z); } void main() { Stack *spa,*spb,*spc; spa=CreateStack(10); spb=CreateStack(10); spc=CreateStack(10); spa->name='a'; spa->row=10; spa->col=10; spb->name='b'; spb->row=10; spb->col=20; spc->name='c'; spc->row=10; spc->col=30; Push(spa,'3'); Push(spa,'2'); Push(spa,'1'); clrscr(); PutStack(spa); PutStack(spb); PutStack(spc); move(3,spa,spb,spc); }