【发布时间】:2021-03-08 10:01:15
【问题描述】:
对于我的任务,我试图在 C 中重新创建海龟图形。基本上,当您输入一系列命令时,我的程序会处理这些命令并相应地在 50 x 50 数组(地板)中绘制一个形状。当我运行我的程序时,它不断给我分段错误错误,我不知道这是什么意思,所以我无法发现我的错误。
这是我的代码:
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: \n\n");
printf("1 ---- pen up\n");
printf("2 ---- pen down\n");
printf("3 ---- turn right\n");
printf("4 ---- turn left\n");
printf("5, N ---- move forward N spaces\n");
printf("6 ---- Print 50 by 50 Floor\n");
printf("9 ---- End Data\n\n");
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("");
}
}
【问题讨论】:
-
我应该补充一点,我的任务要求我在不使用指针的情况下执行此操作。
-
通过编辑将其添加到问题中
-
不,请不要改进您发布的代码,否则人们不会关注您提出的问题。除非你没有复制足够多的代码。这不是一个“交互式”调试论坛。
-
文字墙!减少您的代码,通过调试器运行它并找出问题所在。你有没有努力查明问题出在哪里?
-
如果您遇到段错误,最好的办法(就学习机会而言)是获取核心转储并学习如何使用该核心转储来告诉您确切的位置发生段错误。
标签: c error-handling segmentation-fault