对于我的任务,我正在尝试在C中重新创建海龟图形。基本上,当你输入一系列命令时,我的程序会处理这些命令,并相应地在50乘50的数组(floor(中绘制一个形状。当我运行我的程序时,它一直给我分段错误,我不知道这意味着什么,所以我无法找出我的错误。
这是我的代码:
int facing = 3; //start by facing east
int pen = 1; //start by pen up
int copyToArrays(int commandCheck[2][2], const int commands);
int readCommand(int commandCheck[]);
void performCommand(const int command, const int factor, int floor[50][50]);
void draw(const int floor[50][50]);
// y and x coordinates of turtle
int posX =0;
int posY =0;
int main(void)
{
//INSTRUCTIONS
printf("Command Key: nn");
printf("1 ---- pen upn");
printf("2 ---- pen downn");
printf("3 ---- turn rightn");
printf("4 ---- turn leftn");
printf("5, N ---- move forward N spacesn");
printf("6 ---- Print 50 by 50 Floorn");
printf("9 ---- End Datann");
printf("Enter your commands.n");
int floor[50][50] = {{0}, {0}};// floor of length and width of 50
int commandArrays[100][2] = {{0}, {0}};// arrays for the commands
int numberOfCommands = 0;
numberOfCommands = copyToArrays(commandArrays, 100);// calculate how many commands to process
//for loop to go through the commands and perform them one by one
for(size_t i = 0; i < numberOfCommands; i++)
{
performCommand(commandArrays[i][0], commandArrays[i][1], floor);
}
}
// function to copy commands into arrays and return the number of commands
int copyToArrays(int commandArray[2][2], const int commands)
{
int i;
int arr[2];
for ( i = 0; i <commands && readCommand(arr); i++)
{
commandArray[i][0] = arr[0];
commandArray[i][1] = arr[1];
}
return i;
}
// Function to take input commands from user
int readCommand(int commandCheck[])
{
scanf("%d,%d", &commandCheck[0], &commandCheck[1]);
if(commandCheck[0] != 5)
{
commandCheck[1] = 0;
}
if(commandCheck[0] == 9)
{
return 0;
}
else
{
return 1;
}
}
// follow command and perform task accordingly
void performCommand(const int command, const int factor, int floor[50][50])
{
int j;
switch(command)
{
case 1: pen = 1;
break;
case 2: pen = 0;
floor[posX][posY] = 1;
break;
case 3: if (facing == 3)
{
facing = 6;
}
else if (facing == 6)
{
facing = 9;
}
else if (facing == 9)
{
facing = 12;
}
else
{
facing = 3;
}
break;
case 4: if (facing == 3)
{
facing = 12;
}
else if (facing == 12)
{
facing = 9;
}
else if (facing == 9)
{
facing = 6;
}
else
{
facing = 3;
}
break;
case 5: if (facing == 3)
{
for (j = 1; j <= factor; j++)
{
posX++;
if(pen == 0)
{
floor[posX][posY] = 1;
}
}
}
else if (facing == 6)
{
for(j = 1; j <= factor; j++)
{
posY--;
if(pen == 0)
{
floor[posX][posY] = 1;
}
}
}
else if (facing == 9)
{
for(j = 1; j <= factor; j++)
{
posX--;
if(pen == 0)
{
floor[posX][posY] = 1;
}
}
}
else if(facing == 12)
{
for(j = 1; j <= factor; j++)
{
posY++;
if(pen == 0)
{
floor[posX][posY] = 1;
}
}
}
break;
case 6: draw(floor);
break;
default:
break;
}
}
//FUntion to draw on the floor
void draw(const int floor[50][50])
{
printf("n");
for (size_t i = 0; i <=49; i++)
{
for(size_t j = 49; j >= 0; j--)
{
if(floor[i][j] == 1)
{
printf("%s", "*");
}
else
{
printf(" ");
}
}
puts("");
}
}
这是错误的:
for(size_t j = 49; j >= 0; j--)
当j==0时,进入循环。当j递减时,它不会变为-1,因为size_t是一个无符号类型。打开编译器警告。当我编译你的代码时,编译器告诉我:
a.c:181:30: warning: comparison of unsigned expression in '>= 0' is always true [-Wtype-limits]
181 | for(size_t j = 49; j >= 0; j--)
如果你的编译器没有告诉你,那就学习如何获得诊断。(例如,对于gcc,总是使用-Wall-Wextra编译,并注意它告诉你什么。(