?? 漢諾塔演示.txt
字號:
#include <graphics.h>
struct H
{
int data[15];/*存放每個盤的代號*/
int top;/*每個塔的具體高度*/
}num[3];/*三個塔*/
void move(char x,char y,struct H num[3]);/*移動的具體過程*/
void hanoi(char x,char y,char z,int n,struct H num[3]);/*遞歸*/
chu();/*初始化1*/
void Init(void);
void chushihua(void);/*初始化2*/
void Close(void);/*圖形關閉*/
int computer;/*自動控制與手動控制的標志*/
int speed=1;/*全局變量speed主要是演示過程的速度*/
void main(void)
{ int value=1;
char m;
chu();
while(value)
{chushihua();/*初始狀態*/
printf("\n\n\n\n\n\n\n\t\tDo you want gagin? Y or N");
m=getch();
if(m=='y'||m=='Y')value=1;
else value=0;
}
Close();/*圖形關閉*/
exit(0);
}
void Init(void)
{int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
chu()
{int i,j;
Init();
setcolor(2);
setfillstyle(SOLID_FILL,3);
for(i=0;i<8;i++)
{bar3d(25,50+i*50,50,80+i*50,15,1);}
for(i=1;i<10;i++)
{bar3d(40+i*50,50,65+i*50,80,15,1);}
for(i=1;i<10;i++)
{bar3d(40+i*50,400,65+i*50,430,15,1);}
for(i=0;i<8;i++)
{bar3d(550,50+i*50,575,80+i*50,15,1);}
settextstyle(1,0,4);
outtextxy(150,150,"Welcome to use");
outtextxy(100,200,"wen zhan kun's program");
settextstyle(1,0,3);
outtextxy(160,300,"press any key to continue");
getch();
closegraph();
}
void chushihua(void)/*初始化*/
{ int i,n,color;
Init();
setbkcolor(1);
settextstyle(1,0,2);
outtextxy(30,55,"please input the number of the plate");
printf("\n\n\n\n\n\t\t\t");
scanf("%d",&n);
settextstyle(1,0,1);
outtextxy(30,120,"Please choice the game's type:");
outtextxy(30,150,"Play by computer: please into 1:");
outtextxy(30,180,"Play by hand: please into 2:");
printf("\n\n\n\n\n\n\n\t\t");
scanf("%d",&i);
if(i==2)/*選擇手動控制標志為0*/
computer=0;
else computer=1;
if(n<1||n>10)
n=10;/*越界的話n當10處理*/
if(computer)
{printf("please input the speed :");
scanf("%d",&speed);
if(speed<1)speed=1;
}
Init();
setbkcolor(15);
for(i=0;i<3;i++)
num[i].top=-1;/*三個地方的高度開始都為-1*/
for(i=0;i<n;i++)/*畫一開始的塔座A上的盤子*/
{
num[0].top++;/*棧的高度加1*/
num[0].data[num[0].top]=i; /*最大的盤子代號為0,依次為1,2,…n-1*/
color=num[0].data[num[0].top]+1;/*盤子的顏色代碼為棧頂盤子代號加1*/
setfillstyle(SOLID_FILL,color);
bar(100-(33-3*num[0].data[num[0].top]),400-20*i-8,100+
(33-3*num[0].data[num[0].top]),400-20*i+8); /*畫矩形*/
}
setcolor(YELLOW);
outtextxy(180,450,"any key to continue");
settextstyle(0,0,2);
outtextxy(90,420,"A"); /*塔座標志*/
outtextxy(240,420,"B");
outtextxy(390,420,"C");
getch();/*接收字符后就執行遞歸操作*/
hanoi('a','b','c',n,num);
}
void move(char x,char y,struct H num[3])/*移動的具體過程*/
{
int i;
char num1[3],num2[3];
sprintf(num1,"%c",x-32);/*將小寫變成大寫,并轉換成字符串輸出*/
sprintf(num2,"%c",y-32);
setfillstyle(SOLID_FILL,BLACK);/*把原來的地方移去涂黑*/
bar(0,0,640,60);
setcolor(RED);
outtextxy(150,30,num1);/*輸出移動過程*/
outtextxy(200,30,"--->");
outtextxy(310,30,num2);
settextstyle(0,0,2);
setfillstyle(SOLID_FILL,BLACK);/*把原來的地方移去涂黑*/
bar(100+150*(x-97)-(33-3*num[x-97].data[num[x-97].top]),
400-20*num[x-97].top-8,100+150*(x-97)+(33-3*
num[x-97].data[num[x-97].top]),400-20*num[x-97].top+8);
num[y-97].top++;/*入棧,目標點的top加1*/
num[y-97].data[num[y-97].top]=num[x-97].data[num[x-97].top];/*在目標點盤子的代號與源點盤子的代號相同*/
num[x-97].top--;/*出棧,原來地方的top減1*/
setfillstyle(SOLID_FILL,num[y-97].data[num[y-97].top]+1);/*盤子顏色代碼是棧頂盤子代號加1*/
bar(100+150*(y-97)-(33-3*num[y-97].data[num[y-97].top]),
400-20*num[y-97].top-8,100+150*(y-97)+
(33-3*num[y-97].data[num[y-97].top]),400-20*num[y-97].top+8);
if(computer)/*自動控制就用delay*/
sleep(speed);/*延時函數*/
else
getch();/*手動控制的話就自己按鍵盤來控制*/
}
void hanoi(char one,char two,char three,int n,struct H num[3])/*遞歸n為盤子數,num為堆棧*/
{
if(n==1)
move(one,three,num);/*如果盤子為1,將這個盤子從塔座A移動到塔座C*/
else
{
hanoi(one,three,two,n-1,num);/*將塔座A的前n-1個盤子移到塔座B*/
move(one,three,num);/*將塔座A的第n個盤子移到塔座C*/
hanoi(two,one,three,n-1,num); /*將塔座B的n-1個盤子移到塔座C*/
}
}
void Close(void)/*圖形關閉*/
{
getch();
closegraph();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -