gpt4 book ai didi

c - 功能有一半时间失败

转载 作者:行者123 更新时间:2023-11-30 18:26:40 25 4
gpt4 key购买 nike

我为我的项目编写了一个随机路径生成器,当它工作时,它会按预期工作。然而它只是有时有效。

在我的主函数中,我有一个简单的 switch 语句来调用该项目的三个任务。

while(Continue)
{

switch(Example_number)
{
default: printf("No such program exists.\n");
break;
case 1: path();
break;
case 2: Caesar_cipher();
break;
case 3: anagram();
break;
}

printf("Would you like to test another?(Y/N)\n");
scanf("\n%c",&ch);
if(ch == 'Y' || ch == 'y')
{
NULL;
}
else
{
Continue = false;
}
}

当您输入 1 时,它会调用此函数,该函数会创建一个数组并调用其他两个函数。

void path(void)
{
//Creates the array walk.
char walk[10][10] = {{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'}};

//Creates a randomly generated path to travel along walk.
generate_path(walk);
print_array(walk);
}

生成路径的函数。

void generate_path(char walk[10][10])
{
int move_n = 0;
int row, column, i;
const char alph[] = {'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
//Array that holds all previous data.
int move[3][25] = {0};
bool block = false;

//Allows for a random variable.
srand((unsigned) time(NULL));

row = rand() % 10;
column = rand() % 10;

while(!block)
{
walk[row][column] = 'A';
for(i = 0; i < 25; i++)
{
//goto comes here
restart:
move_n = rand() % move_dir;
//If space is open continue the alph array one row below.
if(move_n == 0 && walk[row+1][column] == '.' && row+1 < 10)
{
row += 1;
move[0][i] = i;
move[1][i] = row;
move[2][i] = column;
walk[row][column] = alph[i];
}
else if(move_n == 1 && walk[row][column+1] == '.' && column+1 < 10) //If space is open continue the alph array one column to the right.
{
column += 1;
move[0][i] = i;
move[1][i] = row;
move[2][i] = column;
walk[row][column] = alph[i];
}
else if(move_n == 2 && walk[row-1][column] == '.' && row-1 >= 0) //If space is open continue the alph array one row above.
{
row -= 1;
move[0][i] = i;
move[1][i] = row;
move[2][i] = column;
walk[row][column] = alph[i];
}
else if(move_n == 3 && walk[row][column-1] == '.' && column-1 >= 0) //If space is open continue the alph array one column to the left.
{
column -= 1;
move[0][i] = i;
move[1][i] = row;
move[2][i] = column;
walk[row][column] = alph[i];
}
else if((walk[row][column-1] == '.') || (walk[row-1][column] == '.') || (walk[row][column+1] == '.') || (walk[row+1][column] == '.'))
{
if(i == 25)
break;
goto restart;
}
else
{
//Resets data to point such that the path can continue.
row = move[1][i-1];
column = move[2][i-1];
i--;
walk[row][column] = '.';
}
}
block = true;
}
}

以及打印数组函数。

void print_array(char walk[10][10])
{
int i = 0;
int k = 0;
for(i = 0; i < 10; i++)
{
for(k = 0; k < 10; k++)
{
printf(" %c", walk[i][k]);
}
printf("\n");
}
}

但由于某种原因,在 switch 语句中输入 1 时,它有时只能起作用。每次调用任何其他函数都可以完美地工作。

最佳答案

您的问题很可能出在generate_path() 中的if 语句上。

您访问 walk[row+1][column]walk[row][column+1] 等。rowcolumn 是用 rand()%10 生成的。因此,在某个时刻,rowcolumn可以是 09访问索引row-1 column+1等,它将访问数组边界之外,这可能会导致各种问题。

关于c - 功能有一半时间失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15166030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com