【发布时间】:2021-10-06 07:10:53
【问题描述】:
我被要求做一个简单的 20x20 海龟图形程序,但由于某种原因,我遇到了一个可能与第 42 行到第 150 行相关的问题(只有他们最初在帖子上,但我编辑了它,因为有人让我这样做cmets):
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
int floor[20][20], step, p1 = 0, p2 = 0;
char compass[6] = "westm", com[1], pen[3] = "up";
int main()
{
com[0] = 'x';
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
floor[i][j] = 0;
}
}
while (com[0] != 'e' && com[0] != 'E')
{
com[0] = 'x';
printf("Enter a command \n");
scanf("%c", &com[0]);
getchar();
if (com[0] == 'e' || com[0] == 'E')
{
printf("End \n");
}
else
{
if (com[0] == 'u' || com[0] == 'U')
{
strncpy(pen, "up", 3);
printf("The pen was turned up \n");
}
if (com[0] == 'd' || com[0] == 'D')
{
strncpy(pen, "do", 3);
floor[p1][p2] = 1;
printf("The pen was turned down \n");
}
if (com[0] == 'r' || com[0] == 'R')
{
if (!strcmp(compass, "westm"))
{
strncpy(compass, "south", 6);
}
if (!strcmp(compass, "south"))
{
strncpy(compass, "eastm", 6);
}
if (!strcmp(compass, "eastm"))
{
strncpy(compass, "north", 6);
}
if (!strcmp(compass, "north"))
{
strncpy(compass, "westm", 6);
}
printf("The turtle turned right \n");
}
if (com[0] == 'l' || com[0] == 'L')
{
if (!strcmp(compass, "westm"))
{
strncpy(compass, "north", 6);
}
if (!strcmp(compass, "south"))
{
strncpy(compass, "westm", 6);
}
if (!strcmp(compass, "eastm"))
{
strncpy(compass, "south", 6);
}
if (!strcmp(compass, "north"))
{
strncpy(compass, "eastm", 6);
}
printf("The turtle turned left \n");
}
if (com[0] == 'w' || com[0] == 'W')
{
step = 2147483647;
if (!strcmp(compass, "westm"))
{
while (step + p2 > 19)
{
printf("Type a valid number of steps \n");
scanf("%d", &step);
getchar();
}
if (!strcmp(pen, "do"))
{
for (int i = 0; i <= p2 + step; i++)
{
floor[p1][p2 + i] = 1;
}
}
p2 = floor + p2;
}
if (!strcmp(compass, "north"))
{
while (p1 - step < 0)
{
scanf("%d", &step);
getchar();
}
if (!strcmp(pen, "do"))
{
for (int i = 0; i <= p1 - step; i++)
{
floor[p1 - i][p2] = 1;
}
}
p1 = p1 - step;
}
if (!strcmp(compass, "eastm"))
{
while (p2 - step < 0)
{
scanf("%d", &step);
getchar();
}
if (!strcmp(pen, "do"))
{
for (int i = 0; i <= p2 - step; i++)
{
floor[p1][p2 - i] = 1;
}
}
p2 = p2 - step;
}
if (!strcmp(compass, "south"))
{
while (step + p2 > 19)
{
scanf("%d", &step);
getchar();
}
if (!strcmp(pen, "do"))
{
for (int i = 0; i <= p1 + step; i++)
{
floor[p1 + i][p2] = 1;
}
}
p1 = p1 + step;
}
}
if (com[0] == 'p' || com[0] == 'P')
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (floor[i][j] == 0)
{
printf(". ");
}
else
{
printf("* ");
}
}
printf("\n");
}
}
}
}
}
基本上,在代码的开头有一个“while”等待用户输入,直到'e'和'E'都不同,它似乎在大多数情况下都可以正常工作
问题在于,当我在右转或左转后行走(通过在字符“r”或“l”之后输入“w”)或尝试多次使用“w”输入时,程序会不断要求输入(对于变量 com,而不是 step)由于某种原因没有读取 'w'
任何其他输入,如“p”、“e”甚至“l”和“r”都可以正常工作,但“w”特别不起作用,如果我使用“p”(“e”不'不计数,因为它会停止重复)然后'w'输入也将被识别
所有这些 if 的算法有点糟糕,但这是我自己能想到的最简单和最容易解释的方法
【问题讨论】:
-
可能不相关,但使用字符串进行状态跟踪、比较和覆盖它们极其效率低下且难以编写。
-
更不用说使用一系列独立的
if语句用于互斥条件集,而不是普通的if/else if链。 -
什么是
step,为什么初始化为INT_MAX?什么是p1、p2? Signed integer overflow 是未定义的行为,但真的不确定while (step + p2 > 19)是否会发生这种情况。看起来很危险。 -
@yano step是一个int,表示乌龟要走多少帧,p1和p2是int,表示乌龟在矩阵floor[20][20]上的当前位置,p1是该行和 p2 是我在 while 开始时将 step 设置为可能的最高值的列,以避免用户输入的输入可能会使 p1 或 p2 高于 19,同时输入任何一个'w' 时间
-
@andreas-wenzel ummmm 我应该用整个代码编辑原始帖子,还是可以在我的问题或评论上放置一个 pastebin 链接?我是 stackoverflow 的新手,所以我真的不知道是否允许这样做
标签: c input graphics output repeat