大家一起来做做这到经典的海龟作图的编程题!

LEE_coding 2008-02-11 04:54:45
设想有一只机械海龟,它在c语言的控制下在屋里四处爬行。海龟拿了一支笔,这只笔或朝
上或朝下,当笔朝下时,海龟用笔画下自己的轨迹,当笔朝上时,海龟在移动过程中什么也不画。
使用一个50x50的数组,并把数组初始化为0。从一个装有命令的数组中读取各种命令。不
管是笔朝上还是笔朝下,都要跟踪海龟的当前位置。假定海龟总是从地板上(0,0)出发
,并且开始时笔是朝上的。程序必须处理的一组命令如下:
命令 含义
1 笔朝上
2 笔朝下
3 右转弯
4 左转弯
5,10 向前走10格(或其他的格数)
6 打印50x50的数组
9 数据结束(标记)

...全文
943 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ltc_mouse 2008-02-12
  • 打赏
  • 举报
回复
怎么感觉lz只是求程序的呀,并不是什么一起做,:)

5楼已经给出了一个不错的实现呀,只要稍加修改就行了吧
LEE_coding 2008-02-12
  • 打赏
  • 举报
回复
用*号打印出作出的图
LEE_coding 2008-02-12
  • 打赏
  • 举报
回复
c how to programm上的题
我是小牧 2008-02-12
  • 打赏
  • 举报
回复
都不明白题目如何理解,不清不楚的
我啃 2008-02-12
  • 打赏
  • 举报
回复
记得是某外国父子编写的某语言大学教程上的题吧~
我啃 2008-02-12
  • 打赏
  • 举报
回复
果然对于我这种只会灌水的新人这题太难了~
xiao_ke 2008-02-12
  • 打赏
  • 举报
回复
学习了
visame 2008-02-12
  • 打赏
  • 举报
回复
向boxban学习了!编程规范很好很好!
boxban 2008-02-11
  • 打赏
  • 举报
回复


#include <stdio.h>
#include <stdlib.h>

// Direction
enum DIR{
DIR_X = 0, // +X
DIR_Y, // +Y
DIR_NX, // -X
DIR_NY, // -Y

DIR_NUM
};
/*
Coordinates

O----------> X
|
|
|
|
Y v

*/

#define INIT_DIR DIR_X

// Commands
#define CMD_NODRAW 1
#define CMD_DRAW 2
#define CMD_TURN_RIGHT 3
#define CMD_TURN_LEFT 4
#define CMD_MOVE 5
#define CMD_PRINT 6
#define CMD_END 9

#define CMD_MOVE_STEPS(steps) (5 | ((int)steps << 8))
#define GET_CMD(data) (data & 0xFF)
#define GET_STEPS(data) (data >> 8)

// Grid
#define MAX_X 20
#define MAX_Y 10

// tortoise status
struct status
{
int x;
int y;
int dir;
int isDrawing;
};

static void init(struct status* status);
static void handler_move(struct status* status, int cmd);
static void handler_print(const struct status* status);
static void run(int* cmds);
// Global data
int g_grid[MAX_X][MAX_Y];


/*********************************************
* Main logic *
*********************************************/
static void init(struct status* status)
{
status->x = 0;
status->y = 0;
status->dir = INIT_DIR;
status->isDrawing = 0;

memset(g_grid, 0, sizeof(g_grid));
}

static void handler_move(struct status* status, int cmd)
{
int steps = GET_STEPS(cmd);
int xNew = status->x;
int yNew = status->y;

switch (status->dir){
case DIR_X: xNew += steps; break;
case DIR_NX: xNew -= steps; break;
case DIR_Y: yNew += steps; break;
case DIR_NY: yNew -= steps; break;
}

if (xNew < 0 || xNew >= MAX_X ||
yNew < 0 || yNew >= MAX_Y){
printf("invalid cmd:%d for status:x=%d,y=%d,dir=%d\n",
cmd, status->x, status->y, status->dir);
exit(-1);
}

// Track movement
if (status->isDrawing){
int i = 0;
switch (status->dir){
case DIR_X: {for (; i < steps; ++i) g_grid[status->x + i][status->y] = 1;} break;
case DIR_NX: {for (; i < steps; ++i) g_grid[status->x - i][status->y] = 1;} break;
case DIR_Y: {for (; i < steps; ++i) g_grid[status->x][status->y + i] = 1;} break;
case DIR_NY: {for (; i < steps; ++i) g_grid[status->x][status->y - i] = 1;} break;
}
}

// Update postion
status->x = xNew;
status->y = yNew;
}

static void handler_print(const struct status* status)
{
int i, j;

printf("====== print begin =====\n");
for (i = 0; i < MAX_Y; ++i){
for (j = 0; j < MAX_X; ++j)
printf("%d", g_grid[j][i]);
printf("\n");
}
printf("====== print end =====\n");
}

static void run(int* cmds)
{
int cmd;
struct status status;

init(&status);
while ( (cmd = GET_CMD(*cmds)) != CMD_END){
switch (cmd){
case CMD_NODRAW:
status.isDrawing = 0;
break;
case CMD_DRAW:
status.isDrawing = 1;
break;
case CMD_TURN_RIGHT:
status.dir = (status.dir + 1) % DIR_NUM;
break;
case CMD_TURN_LEFT:
status.dir = (status.dir + DIR_NUM - 1) % DIR_NUM;
break;
case CMD_MOVE:
handler_move(&status, *cmds);
break;
case CMD_PRINT:
handler_print(&status);
break;
}
cmds++;
}
}

/*********************************************
* Test case *
*********************************************/
void test1()
{
int cmds[] = {
CMD_DRAW,
CMD_MOVE_STEPS(5),
CMD_TURN_RIGHT,
CMD_MOVE_STEPS(4),
CMD_PRINT,
CMD_NODRAW,
CMD_TURN_LEFT,
CMD_MOVE_STEPS(10),
CMD_DRAW,
CMD_TURN_LEFT,
CMD_MOVE_STEPS(2),
CMD_PRINT,
CMD_END
};

run(cmds);
}

int main()
{
test1();

return 0;
}

LEE_coding 2008-02-11
  • 打赏
  • 举报
回复
ding
LEE_coding 2008-02-11
  • 打赏
  • 举报
回复
不难就做啊
arong1234 2008-02-11
  • 打赏
  • 举报
回复
这个题目难点在哪啊?要让别人一起做:)
LEE_coding 2008-02-11
  • 打赏
  • 举报
回复
自己顶一下!

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧