| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6253 人关注过本帖, 1 人收藏
标题:花了一个晚上做的汉诺塔动画,请大家看看!
取消只看楼主 加入收藏
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏(1)
 问题点数:0 回复次数:9 
花了一个晚上做的汉诺塔动画,请大家看看!

做得不好不要骂我...
毕竟刚接触不久......

/* ================================= Program Description ================================= */
/* Program Name : Hanoi.cpp */
/* Program Purpose : Simulate Hanoi */
/* Environment : TC3.0 */
/* Operating System : Windows XP */
/* Written By Lydolphin. */
/* ======================================================================================= */

/* --------------------------------------------------------------------------------------- */

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <dos.h>

/* --------------------------------------------------------------------------------------- */

#define PATH "D:\\Software\\Programs\\TC3.0\\BGI"
#define N 4 //修改盘数目
#define Rate 4 //修改速度

/* --------------------------------------------------------------------------------------- */

int x[N][2], y[N][2] ;
int countA, countB, countC ;

/* --------------------------------------------------------------------------------------- */

void init(int numT) ;
void Hanoi(char from, char to, char aux, int n) ;
void movTower(int n, char from, char to) ;
void movup(int arrayE, int x1, int x2) ;
void movdown(int arrayE, int x1, int x2, int countN) ;
void movright(int arrayE, int x, int n) ;
void movleft(int arrayE, int x, int n) ;

/* --------------------------------------------------------------------------------------- */

void main()
{
int gdriver=DETECT, gmode ;

registerbgidriver(EGAVGA_driver) ;
initgraph(&gdriver, &gmode, PATH) ;

countA=N ;
countB=0 ;
countC=0 ;

init(N) ;
getch() ;
Hanoi('A', 'C', 'B', N) ;

getch() ;
closegraph() ;
}

/* --------------------------------------------------------------------------------------- */

void init(int numT)
{
int i ;
int count=0 ;

bar(10, 70, 629, 409) ;// the border
setfillstyle(1, 0) ;
bar(30, 359, 609, 369) ;//the bottom
bar(121, 150, 131, 358) ;//the lef
bar(314, 150, 324, 358) ;//the mid
bar(507, 150, 517, 358) ;//the rig
setcolor(8) ;
for(i=numT-1 ; i>=0 ; i--)
{
x[count][0]=121-(numT-i)*4 ;
x[count][1]=131+(numT-i)*4 ;
y[count][0]=348-i*11 ;
y[count][1]=358-i*11 ;
rectangle(x[count][0], y[count][0], x[count][1], y[count][1] ) ;
count++ ;
}
}

/* --------------------------------------------------------------------------------------- */

void Hanoi(char from, char to, char aux, int n)
{
if(n==1)
movTower(1, from, to) ;
else
{
Hanoi(from, aux, to, n-1) ;
movTower(n, from, to) ;
Hanoi(aux, to, from, n-1) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movTower(int n, char from, char to)
{
int arrayE ;//定义二维数组元素的第一维位置

arrayE=n-1 ;

if(from=='A' && to=='C')
{
countA-- ;

movup(arrayE, 121, 131) ;

movright(arrayE, 507, n) ;

movdown(arrayE, 507, 517, countC) ;

countC++ ;
}
if(from=='A' && to=='B')
{
countA-- ;

movup(arrayE, 121, 131) ;

movright(arrayE, 314, n) ;

movdown(arrayE, 314, 324, countB) ;

countB++ ;
}
if(from=='B' && to=='A')
{
countB-- ;

movup(arrayE, 314, 324) ;

movleft(arrayE, 121, n) ;

movdown(arrayE, 121, 131, countA) ;

countA++ ;
}
if(from=='B' && to=='C')
{
countB-- ;

movup(arrayE, 314, 324) ;

movright(arrayE, 507, n) ;

movdown(arrayE, 507, 517, countC) ;

countC++ ;
}
if(from=='C' && to=='A')
{
countC-- ;

movup(arrayE, 507, 517) ;

movleft(arrayE, 121, n) ;

movdown(arrayE, 121, 131, countA) ;

countA++ ;
}
if(from=='C' && to=='B')
{
countC-- ;

movup(arrayE, 507, 517) ;

movleft(arrayE, 314, n) ;

movdown(arrayE, 314, 324, countB) ;

countB++ ;
}
}

/* --------------------------------------------------------------------------------------- */

void movup(int arrayE, int x1, int x2)
{
while(y[arrayE][1] > 140)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
setcolor(0) ;
if(y[arrayE][0] > 150)
line(x1, y[arrayE][0], x2, y[arrayE][0] ) ;
if(y[arrayE][1] > 150)
line(x1, y[arrayE][1], x2, y[arrayE][1] ) ;
y[arrayE][0] -- ;
y[arrayE][1] -- ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movdown(int arrayE, int x1, int x2, int countN)
{
int botCPos ;

botCPos=358-countN*11 ;
while(y[arrayE][1] < botCPos)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
setcolor(0) ;
if(y[arrayE][0] > 150)
line(x1, y[arrayE][0], x2, y[arrayE][0] ) ;
if(y[arrayE][1] > 150)
line(x1, y[arrayE][1], x2, y[arrayE][1] ) ;
y[arrayE][0] ++ ;
y[arrayE][1] ++ ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movright(int arrayE, int x1, int n)
{
int movCPos ;

movCPos=x1-n*4 ;
while(x[arrayE][0] < movCPos)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
x[arrayE][0] ++ ;
x[arrayE][1] ++ ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movleft(int arrayE, int x1, int n)
{
int movCPos ;

movCPos=x1-n*4 ;
while(x[arrayE][0] > movCPos)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
x[arrayE][0] -- ;
x[arrayE][1] -- ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

搜索更多相关主题的帖子: 汉诺塔 动画 
2006-02-10 05:16
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏
得分:0 
学C一学期了!
C的图形编程是寒假刚学的......

PHP:http://www.
ASP:http://www.
HTML:http://www.
2006-02-10 10:50
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏
得分:0 

谢谢各位高手的鼓励!
现在有点信心罗!!
回复11楼的兄弟:
在前面程序描述里说明了:
编辑器是TC3.0
操作系统是WINDOWS XP
在其他的系统我就不知道了!可能98以下的就不行吧!


PHP:http://www.
ASP:http://www.
HTML:http://www.
2006-02-10 15:56
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏
得分:0 
刚才用虚拟机98系统试了下!
结果运行可以,但初始化的界面只有半截!
我之前做过一个模拟WINDOWS XP动画时钟也在98下这样!
刚开始猜可能是虚拟机虚拟系统的问题!
但虚拟机XP, 2003系统却没问题!(2000系统我这没有)
如果排除虚拟机的问题的话,就是98系统初始化图形和XP有点不同吧?
请高手们教一下,到底是什么问题????
谢谢.............................

PHP:http://www.
ASP:http://www.
HTML:http://www.
2006-02-10 16:40
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏
得分:0 

16楼的兄弟,递归的算法!C语言的书上有!看一下好么?

17楼的问题:TC2.0没有//注释符!把它注释的删掉!还有把registerbgidriver(EGAVGA_driver) ;这句也删掉吧!不删掉也可以!但要做些处理,具体处理参考如下:
/* ===================================================================================== */
独立图形运行程序的建立

Turbo C对于用initgraph()函数直接进行的图形初始化程序, 在编译和链接 时并没有将相应的驱动程序(*.BGI)装入到执行程序, 当程序进行到intitgraph()语句时, 再从该函数中第三个形式参数char *path中所规定的路径中去找相应的驱动程序。若没有驱动程序, 则在C:\TC中去找, 如C:\TC中仍没有或TC不存在,将会出现错误:
BGI Error: Graphics not initialized (use 'initgraph')
因此, 为了使用方便, 应该建立一个不需要驱动程序就能独立运行的可执行图形程序,Turbo C中规定用下述步骤(这里以EGA、VGA显示器为例):
1. 在C:\TC子目录下输入命令:BGIOBJ EGAVGA
此命令将驱动程序EGAVGA.BGI转换成EGAVGA.OBJ的目标文件。
2. 在C:\TC子目录下输入命令:TLIB LIB\GRAPHICS.LIB+EGAVGA
此命令的意思是将EGAVGA.OBJ的目标模块装到GRAPHICS.LIB库文件中。
3. 在程序中initgraph()函数调用之前加上一句:
registerbgidriver(EGAVGA_driver):
该函数告诉连接程序在连接时把EGAVGA的驱动程序装入到用户的执行程序中。
经过上面处理,编译链接后的执行程序可在任何目录或其它兼容机上运行。
/* ===================================================================================== */


PHP:http://www.
ASP:http://www.
HTML:http://www.
2006-02-10 19:49
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏
得分:0 
补充一下,还要把TC的图形模式开启起来!
其他的应该问题不大。

PHP:http://www.
ASP:http://www.
HTML:http://www.
2006-02-10 19:52
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏
得分:0 

可以的!
我刚才在虚拟机中试验了一下!
可以这样定义!
如果还运行不了!就用这个吧!
我刚才修改一下在TC2.0运行了,可以运行!
/* ================================= Program Description ================================= */
/* Program Name : Hanoi.c */
/* Program Purpose : Simulate Hanoi */
/* Environment : TC2.0 */
/* Operating System : Windows.Server.2003.Standard */
/* Written By Lydolphin. */
/* ======================================================================================= */

/* --------------------------------------------------------------------------------------- */

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <dos.h>

/* --------------------------------------------------------------------------------------- */

#define PATH "D:\\Software\\Programs\\TC3.0\\BGI"
#define N 4
#define Rate 4

/* --------------------------------------------------------------------------------------- */

int x[N][2], y[N][2] ;
int countA, countB, countC ;

/* --------------------------------------------------------------------------------------- */

void init(int numT) ;
void Hanoi(char from, char to, char aux, int n) ;
void movTower(int n, char from, char to) ;
void movup(int arrayE, int x1, int x2) ;
void movdown(int arrayE, int x1, int x2, int countN) ;
void movright(int arrayE, int x, int n) ;
void movleft(int arrayE, int x, int n) ;

/* --------------------------------------------------------------------------------------- */

void main()
{
int gdriver=DETECT, gmode ;

initgraph(&gdriver, &gmode, PATH) ;

countA=N ;
countB=0 ;
countC=0 ;

init(N) ;
getch() ;
Hanoi('A', 'C', 'B', N) ;

getch() ;
closegraph() ;
}

/* --------------------------------------------------------------------------------------- */

void init(int numT)
{
int i ;
int count=0 ;

bar(10, 70, 629, 409) ;
setfillstyle(1, 0) ;
bar(30, 359, 609, 369) ;
bar(121, 150, 131, 358) ;
bar(314, 150, 324, 358) ;
bar(507, 150, 517, 358) ;
setcolor(8) ;
for(i=numT-1 ; i>=0 ; i--)
{
x[count][0]=121-(numT-i)*4 ;
x[count][1]=131+(numT-i)*4 ;
y[count][0]=348-i*11 ;
y[count][1]=358-i*11 ;
rectangle(x[count][0], y[count][0], x[count][1], y[count][1] ) ;
count++ ;
}
}

/* --------------------------------------------------------------------------------------- */

void Hanoi(char from, char to, char aux, int n)
{
if(n==1)
movTower(1, from, to) ;
else
{
Hanoi(from, aux, to, n-1) ;
movTower(n, from, to) ;
Hanoi(aux, to, from, n-1) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movTower(int n, char from, char to)
{
int arrayE ;

arrayE=n-1 ;

if(from=='A' && to=='C')
{
countA-- ;

movup(arrayE, 121, 131) ;

movright(arrayE, 507, n) ;

movdown(arrayE, 507, 517, countC) ;

countC++ ;
}
if(from=='A' && to=='B')
{
countA-- ;

movup(arrayE, 121, 131) ;

movright(arrayE, 314, n) ;

movdown(arrayE, 314, 324, countB) ;

countB++ ;
}
if(from=='B' && to=='A')
{
countB-- ;

movup(arrayE, 314, 324) ;

movleft(arrayE, 121, n) ;

movdown(arrayE, 121, 131, countA) ;

countA++ ;
}
if(from=='B' && to=='C')
{
countB-- ;

movup(arrayE, 314, 324) ;

movright(arrayE, 507, n) ;

movdown(arrayE, 507, 517, countC) ;

countC++ ;
}
if(from=='C' && to=='A')
{
countC-- ;

movup(arrayE, 507, 517) ;

movleft(arrayE, 121, n) ;

movdown(arrayE, 121, 131, countA) ;

countA++ ;
}
if(from=='C' && to=='B')
{
countC-- ;

movup(arrayE, 507, 517) ;

movleft(arrayE, 314, n) ;

movdown(arrayE, 314, 324, countB) ;

countB++ ;
}
}

/* --------------------------------------------------------------------------------------- */

void movup(int arrayE, int x1, int x2)
{
while(y[arrayE][1] > 140)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
setcolor(0) ;
if(y[arrayE][0] > 150)
line(x1, y[arrayE][0], x2, y[arrayE][0] ) ;
if(y[arrayE][1] > 150)
line(x1, y[arrayE][1], x2, y[arrayE][1] ) ;
y[arrayE][0] -- ;
y[arrayE][1] -- ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movdown(int arrayE, int x1, int x2, int countN)
{
int botCPos ;

botCPos=358-countN*11 ;
while(y[arrayE][1] < botCPos)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
setcolor(0) ;
if(y[arrayE][0] > 150)
line(x1, y[arrayE][0], x2, y[arrayE][0] ) ;
if(y[arrayE][1] > 150)
line(x1, y[arrayE][1], x2, y[arrayE][1] ) ;
y[arrayE][0] ++ ;
y[arrayE][1] ++ ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movright(int arrayE, int x1, int n)
{
int movCPos ;

movCPos=x1-n*4 ;
while(x[arrayE][0] < movCPos)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
x[arrayE][0] ++ ;
x[arrayE][1] ++ ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movleft(int arrayE, int x1, int n)
{
int movCPos ;

movCPos=x1-n*4 ;
while(x[arrayE][0] > movCPos)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
x[arrayE][0] -- ;
x[arrayE][1] -- ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */


PHP:http://www.
ASP:http://www.
HTML:http://www.
2006-02-10 20:20
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏
得分:0 

谢谢夸奖!
拜师就算了!
我也是刚学不久的菜鸟而已....
可以的话,交个朋友吧!
呵呵.................
反正大家都在这泡的......


PHP:http://www.
ASP:http://www.
HTML:http://www.
2006-02-10 21:04
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏
得分:0 

有啊!!
QQ:114606599!!



PHP:http://www.
ASP:http://www.
HTML:http://www.
2006-02-10 21:20
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏
得分:0 

呵呵!
ICQ我不用...


PHP:http://www.
ASP:http://www.
HTML:http://www.
2006-02-10 21:32
快速回复:花了一个晚上做的汉诺塔动画,请大家看看!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023988 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved