汉诺塔 图形界面 C++ 源码 交大 项目.doc_第1页
汉诺塔 图形界面 C++ 源码 交大 项目.doc_第2页
汉诺塔 图形界面 C++ 源码 交大 项目.doc_第3页
汉诺塔 图形界面 C++ 源码 交大 项目.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/*汉诺塔 图形界面 C+ 源码 VC 6.0运行正常。*/include #include#include#include#include #includestruct T int h; /塔的高度 int x; /塔的第一个盘的x坐标 ,y坐标等于(7+n-塔的高度h) int l; /塔的第一个盘的长度 ta3=10,15,2,0,0,0,0,0,0;char c1879,hnt1879= , 汉诺塔动画演示 , , , , , , , , , , , , , , , , ;int n; int sum=0; int computerorpeople=1; int speed=0; void count_ta_1(int ta_1)/重新计算塔的第一个盘的x坐标和塔的高度 int i; int j; int k=0; for(i=7;i7+n;i+) for(j=4+ta_1*24;j28+ta_1*24;j+) if(hntij!= ) for(;hntij+k!= ;k+); tata_1.x=j; tata_1.l=k; return; void move(int ta_1,int ta_3) int i; sum+; tata_3.h+; tata_3.x=tata_1.x+(ta_3-ta_1)*24;/每个塔之间相隔4 tata_3.l=tata_1.l; for(i=0;itata_1.l;i+) hnt7+n-tata_3.htata_3.x+i=hnt7+n-tata_1.htata_1.x+i; hnt7+n-tata_1.htata_1.x+i= ; tata_1.h-; count_ta_1(ta_1); system(cls); for(i=0;i7+n;i+) couthntiendl; couthnt17; coutendlendl 从char(ta_1+65)塔移动一个盘到char(ta_3+65)塔endl; coutendl 步数:sumendlendl; if(computerorpeople=2) system(pause); else for(i=0;i200000*speed;i+); void hanoi(int ta_1,int ta_2,int ta_3,int m) /递归,ta_1/ta_2/ta_3分别为3座塔的代号,m为盘子数 if(m=1) move(ta_1,ta_3); /如果盘子为1,将这个盘子从塔座A移动到塔座C else hanoi(ta_1,ta_3,ta_2,m-1); /将塔座A的前m-1个盘子移到塔座B move(ta_1,ta_3); /将塔座A的第m个盘子移到塔座C hanoi(ta_2,ta_1,ta_3,m-1); /将塔座B的m-1个盘子移到塔座C void main() int i,b=1; for(i=0;i18;i+) for(int j=0;j=78;j+) cij=hntij; while(b=1) coutn; cout请选择:输入1由电脑自动控制;endl; cout 输入2由人控制。endl; coutcomputerorpeople; if(computerorpeople!=1&computerorpeople!=2) computerorpeople=1; if(computerorpeople=1) coutspeed; if(n10) n=10; ta0.h=n; system(cls);/清除屏幕 for(i=0;i7+n;i+) couthntiendl; couthnt17endl; system(pause);/ system(pause)就是从程序里调用pause命令; /而pause这个系统命令的功能很简单,就是在命令行上输出一行类似于Press any key to exit的字,等待用户按一个键,然后返回。 hanoi(0,1,2,n); if(computerorpeople=1) system(pause); system(cls); for(i=0;i18;i+) for(int j=0;j=78;j+) hntij=cij; cout是否继续游

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论