1、void error_debug(char *a) if(errs9)return; errerrs=(char *)malloc(strlen(a)+1); strcpy(errerrs,a); printf(a); errs+;void report_debug() if(!errs)return;errs;i+) printf(erri); free(erri); #else #define error(x) #define report() #define initerror()#endif about stack#define STACK_SIZE 31typedef struct
2、int dataSTACK_SIZE; int top;stack;int clear(stack *a);int create(stack *a);int push(stack *a,int data);int pop(stack *a,int *data);int gettop(stack *a,int *data);int dispose(stack *a);int pop(stack *a,int *data) if(a-top) *data=a-data-a-top; return 1; else error(pop(stack *,int *):stack empty!n); re
3、turn 0;int push(stack *a,int data)topdataa-top+=data; else push(stack *,int):stack full!int create(stack *a) *a=(stack *)malloc(sizeof(stack); if(*a)return clear(*a); elsecreate(stack *):create error! Not enough momery!int clear(stack *a) if(a)top=0;clear(stack *):stack not exist!int gettop(stack *a
4、,int *data)top-1;gettop(stack *,int *):int dispose(stack *a) free(a);dispose(stack *):/*/ about Hanoi the gamegraphics.hdos.h#define MAX_LEVEL STACK_SIZEint positionMAX_LEVEL+1;stack *theStack3;int depth;int mode;int print;int initgame(int d) int x,y; int h=5; int w; initerror(); if(mode) int gdrive
5、r = DETECT, gmode, errorcode; /* initialize graphics mode */ initgraph(&gdriver, &gmode, setfillstyle(1,7);3;i+)create(&theStacki) break; if(i!=3) for(;i=0;i-)dispose(theStacki);initgame(int):can not init stack! depth=d; for(i=d;i;i-) push(theStack0,i); y=200+100-theStack0-top*(h+1); w=i*10; x=150-w
6、/2; setcolor(i); setfillstyle(1,i); bar(x,y,x+w,y+h); positioni=0; setcolor(15); rectangle(150+i*150-1,120,150+i*150+1,300); line(50,300,500,300);int endgame() int i=2; printf(report: report(); if(mode)closegraph();void show(int p,int from,int to) int newx,newy; int w=p*10; y=200+100-(theStackfrom-t
7、op+1)*(h+1); x=from*150+150-w/2; newx=to*150+150-w/2; newy=200+100-theStackto- while(y100) setcolor(0); setfillstyle(1,0); y-=(h+1); rectangle(150+from*150-1,120,150+from*150+1,300); setcolor(p); setfillstyle(1,p); delay(10); while(x!=newx) (xnewx)?x-:x+; delay(2); while(y,&t)move(int):the stack is
8、emptyn if(t=p) pop(theStackpositionpt);mode&print)printf(%c - ,A+positionp); /* another important core line */ s=(positionp+1+(depth%2?p%2:(p+1)%2) )%3; if(gettop(theStacks,&t)&t+positionp,conio.hvoid main() unsigned long i; unsigned long N=10; unsigned long p,q;Welcome to HanoinNote that this Hanoi
9、 is not write by recurrence!And not calculate with any stack.nbut i want to check if the a is right.ni use 3 stack to show if there is any violent move happens.:)nnEnter a number as level(1 to 30): scanf(%dN); if(N30)error: not 1 to 30n return;n Select show mode(c in TEXT g in GRAPHICS)nNote that if
10、 the level is to big youd better not use for speed.n19 is about 20 seconds. 20 is about double of that. etc.nI test on a intel 166mmx cpu. 30 may be 40*1024 seconds.nwish you succeed! switch(getch() case :do you want to show the result?(y/n)nprint result will be slow! do mode=getch(); if(mode=y)print=1;n)print=0; while(mode!=mode! mode=0;mode=1;break; default:printf( neither nor return;processing.n initgame(N); /* core here! only 8 lines, ha! here get the level queue as 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 */ for(i=1;(1L q=1L=1; p-; if(mode|print)move(p); else move2(p);okn getch(); endgame();