⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 汉诺塔演示.txt

📁 本程序的功能就是通过输入特定的塔的个数
💻 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 + -