汉诺塔(hanoi)源程序

None.gif #include < graphics.h >
None.gif
None.gif  #include
" dos.h "
None.gif
None.gif  #include 
" stdio.h "
None.gif
None.gif  #include 
" alloc.h "
None.gif
None.gif   
int  num1,num2,num3,h0,cy[ 66 ];
None.gif
None.gif  
void  plot1( int  , int  , int  , int  );
None.gif
None.gif   
void  move( char  getone, char  putone)
None.gif
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {   int x0=40,x,y,w,h,tx,ty,tw,th,x1,x2,y1,y2,xm1,ym1,xm2,ym2;
InBlock.gif
InBlock.gif    
int i,n,size;
InBlock.gif
InBlock.gif    
void *buffer,*buffer1;
InBlock.gif
InBlock.gif    
switch(getone)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif    
case 'A':num1--;break;
InBlock.gif
InBlock.gif    
case 'B':num2--;break;
InBlock.gif
InBlock.gif    
case 'C':num3--;break;
InBlock.gif
InBlock.gif    
default: exit(0);
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
switch(putone)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif    
case 'A':num1++;break;
InBlock.gif
InBlock.gif    
case 'B':num2++;break;
InBlock.gif
InBlock.gif    
case 'C':num3++;break;
InBlock.gif
InBlock.gif    
default: exit(0);
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif  
switch(getone)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif    
case 'A':x=120;y1=cy[num1+1]-h0;break;
InBlock.gif
InBlock.gif    
case 'B':x=320;y1=cy[num2+1]-h0;break;
InBlock.gif
InBlock.gif    
case 'C':x=520;y1=cy[num3+1]-h0;break;
InBlock.gif
InBlock.gif    
default: exit(0);
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif   
switch(putone)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif    
case 'A':tx=120-50;ty=cy[num1]-h0;break;
InBlock.gif
InBlock.gif    
case 'B':tx=320-50;ty=cy[num2]-h0;break;
InBlock.gif
InBlock.gif    
case 'C':tx=520-50;ty=cy[num3]-h0;break;
InBlock.gif
InBlock.gif    
default: exit(0);
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif  x1
=x-50;
InBlock.gif
InBlock.gif  x2
=x+50;
InBlock.gif
InBlock.gif  y2
=y1+h0;
InBlock.gif
InBlock.gif  xm1
=(x1+tx)/2;
InBlock.gif
InBlock.gif  ym1
=(y1+ty)/2;
InBlock.gif
InBlock.gif  xm2
=xm1+100;
InBlock.gif
InBlock.gif  ym2
=ym1+h0;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif  size
=imagesize(x1,y1,x2,y2);
InBlock.gif
InBlock.gif  buffer
=malloc(size);
InBlock.gif
InBlock.gif  getimage(x1,y1,x2,y2,buffer);
InBlock.gif
InBlock.gif  setfillstyle(SOLID_FILL,
3);
InBlock.gif
InBlock.gif  bar(x1,y1,x2,y2);
InBlock.gif
InBlock.gif  setcolor(YELLOW);
InBlock.gif
InBlock.gif  line((x1
+x2)/2,y1,(x1+x2)/2,y2);
InBlock.gif
InBlock.gif  buffer1
=malloc(size);
InBlock.gif
InBlock.gif  n
=abs(tx-x1);
InBlock.gif
InBlock.gif  
for(i=0;i<n;i+=3)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif
InBlock.gif  xm1
=x1+(float)(tx-x1)/n*i;
InBlock.gif
InBlock.gif  ym1
=y1+(float)(ty-y1)/(tx-x1)*(xm1-x1);
InBlock.gif
InBlock.gif  xm2
=xm1+100;
InBlock.gif
InBlock.gif  ym2
=ym1+h0;
InBlock.gif
InBlock.gif  getimage(xm1,ym1,xm2,ym2,buffer1);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  putimage(xm1,ym1,buffer,COPY_PUT);
/**//* delay(10); */
InBlock.gif
InBlock.gif  putimage(xm1,ym1,buffer1,COPY_PUT);
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  putimage(tx,ty,buffer,COPY_PUT);
/**//* delay(50);*/
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif  free(buffer1);
InBlock.gif
InBlock.gif  free(buffer);
InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedBlockEnd.gif  }

None.gif
None.gif 
None.gif
None.gif  
void  hanoi( int  n, char  one, char  two, char  three)
None.gif
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif
InBlock.gif   
if(n==1) move(one,three);
InBlock.gif
InBlock.gif   
else
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif
InBlock.gif     hanoi(n
-1,one,three,two);
InBlock.gif
InBlock.gif     move(one,three);
InBlock.gif
InBlock.gif     hanoi(n
-1,two,one,three);
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif   }

None.gif
None.gif  main()
None.gif
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif
InBlock.gif  
int gdriver,gmode,x0=40,y0=400,x1,x2,x3,y1,y2,y3,x,y,w0=100;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif  
int i,num,w,w1;
InBlock.gif
InBlock.gif  printf(
"Please input the number of plate(<=64):");
InBlock.gif
InBlock.gif  scanf(
"%d",&num);
InBlock.gif
InBlock.gif  
if(num>65)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{printf ("number great than 64 ",exit(0));}
InBlock.gif
InBlock.gif  h0
=(y0-80)/num;
InBlock.gif
InBlock.gif  w1
=(100-10)/num;
InBlock.gif
InBlock.gif  num1
=num;
InBlock.gif
InBlock.gif  num2
=0;
InBlock.gif
InBlock.gif  num3
=0;
InBlock.gif
InBlock.gif  gdriver
=DETECT;
InBlock.gif
InBlock.gif  initgraph(
&gdriver,&gmode,"");
InBlock.gif
InBlock.gif  setfillstyle(SOLID_FILL,
3);
InBlock.gif
InBlock.gif  bar(
0,0,640,480);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  setcolor(
15);             /**//* white */
InBlock.gif
InBlock.gif  line(
1,1,637,1);
InBlock.gif
InBlock.gif  line(
1,1,1,477);
InBlock.gif
InBlock.gif  line(
0,0,638,0);
InBlock.gif
InBlock.gif  line(
0,0,0,478);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  setcolor(
8);      /**//*darkgray */
InBlock.gif
InBlock.gif  line(
638,2,638,478);
InBlock.gif
InBlock.gif  line(
639,1,639,479);
InBlock.gif
InBlock.gif  line(
632,380,632,472);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  setcolor(
14);     /**//* yellow */
InBlock.gif
InBlock.gif  line(x0,
401,640-x0,401);
InBlock.gif
InBlock.gif  line(x0
+80,40,x0+80,400);
InBlock.gif
InBlock.gif  line(x0
+280,40,x0+280,400);
InBlock.gif
InBlock.gif  line(x0
+480,40,x0+480,400);
InBlock.gif
InBlock.gif  x1
=x0+80;
InBlock.gif
InBlock.gif  x2
=x0+280;
InBlock.gif
InBlock.gif  x3
=x0+480;
InBlock.gif
InBlock.gif  
for(x=x1,y=y0,w=w0,i=1;i<=num;i++,y=y-h0-1,w=w-w1)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif     plot1(x,y,w,h0);
InBlock.gif
InBlock.gif     cy[i]
=y;
InBlock.gif
ExpandedSubBlockEnd.gif     }

InBlock.gif
InBlock.gif     setcolor(
14);
InBlock.gif
InBlock.gif  line(x0
+80,40,x0+80,400);
InBlock.gif
InBlock.gif  getch();
InBlock.gif
InBlock.gif  hanoi(num,
'A','B','C');
InBlock.gif
InBlock.gif  getch();
InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gif 
void  plot1( int  x, int  y, int  w, int  h)
None.gif
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {int x1,x2,y1,y2,xc,yc,a,b;
InBlock.gif
InBlock.gif  x1
=x-w/2;
InBlock.gif
InBlock.gif  x2
=x+w/2;
InBlock.gif
InBlock.gif  y1
=y-h;
InBlock.gif
InBlock.gif  y2
=y;
InBlock.gif
InBlock.gif  xc
=(x1+x2)/2;
InBlock.gif
InBlock.gif  yc
=(y1+y2)/2;
InBlock.gif
InBlock.gif  a
=w/2;
InBlock.gif
InBlock.gif  b
=h/2;
InBlock.gif
InBlock.gif   setfillstyle(SOLID_FILL,
14);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//*  bar(x1,y1,x2,y2); */
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   setcolor(
14); /**//* yellow */
InBlock.gif
InBlock.gif   setlinestyle(
0,0,1);
InBlock.gif
InBlock.gif   ellipse(xc,yc,
0,360,a,b);
InBlock.gif
InBlock.gif       floodfill(xc
-2,yc,YELLOW);
InBlock.gif
InBlock.gif   floodfill(xc
+2,yc,YELLOW);
InBlock.gif
InBlock.gif   setcolor(
1);
InBlock.gif
InBlock.gif   ellipse(xc,yc,
0,360,a,b);
InBlock.gif
InBlock.gif   ellipse(xc,yc,
0,360,a-3,b-3);
InBlock.gif
InBlock.gif    setfillstyle(SOLID_FILL,
12);
InBlock.gif
InBlock.gif    floodfill(xc
-2,yc+b-2,BLUE);
InBlock.gif
InBlock.gif   floodfill(xc
+2,yc+b-3,BLUE);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*   line(x1,y1,x1,y2);
InBlock.gif
InBlock.gif   line(x1,y2,x2,y2);
InBlock.gif
InBlock.gif   line(x2,y2,x2,y1);
InBlock.gif
InBlock.gif   line(x2,y1,x1,y1);
InBlock.gif
ExpandedSubBlockEnd.gif    
*/

InBlock.gif
ExpandedBlockEnd.gif  }

None.gif
None.gif
posted on 2006-02-28 20:43 Aween's Blog 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/aween/archive/2006/02/28/339894.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值