gpt4 book ai didi

c - 在 C 中搜索道路时如何替换代码中的 goto

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

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

#define SPECKLE '.'
#define WALL '#'


/*char a[1000][1000] = {
{'#','.','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','.','.','.','#','.','.','.','.','.','.','.','.','.','.','.','#'},
{'#','.','#','#','#','.','#','#','#','#','#','.','#','.','#','#','#'},
{'#','.','.','.','.','.','.','.','.','.','#','.','#','.','.','.','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','.','#'},

};*/
#define SIZE 1000

void print_arr()
{
char a[SIZE][SIZE];
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 7; ++j)
{
printf("%c",a[i][j]);
}
printf("\n");
}

}


int main(int argc, char const *argv[])
{
int x = 0, y = 1;
char a[SIZE][SIZE];

for(int i=0;i<SIZE;i++)
{
scanf("%s",&a[i]);
}

continue_in_road:
while(1)
{
if(y == 16 && x == 4)
break;
if(a[x][y] == SPECKLE)
{
a[x][y] = '1';
y++;
continue;
}
else if(a[x][y] == WALL)
{
y--;
goto go_back_and_check;
}

go_back_and_check:
while(1)
{
if(a[x+1][y] == SPECKLE)
{
x++;
goto continue_in_road;
}
else if(a[x-1][y] == SPECKLE)
{
x--;
goto continue_in_road;
}
else if(a[x][y-1] == WALL)
{
a[x][y] = '2';
x--;
continue;
}
else if(a[x][y-1] == '1')
{
a[x][y] = '2';
y--;
continue;

}
else if(a[x][y-1] == SPECKLE)
{
y--;
goto switch_to_left;
}
}

switch_to_left:
while(1)
{
if(a[x][y] == SPECKLE)
{
a[x][y] = '1';
y--;
continue;
}
else if(a[x][y] == WALL)
{
y++;
goto go_back_and_check;
}
}

}


//print_arr();

for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 18; ++j)
{
if(a[i][j] == '1')
a[i][j] = '*';
if(a[i][j] == '2')
a[i][j] = '.';
printf("%c",a[i][j]);
}
printf("\n\n");
}

// print_arr();

return 0;
}

该代码用于在图中查找路径或道路,如下所示:

输入: The input

输出: The output

我正在寻找 goto 命令的替代方法,因为我想教的不仅仅是这个命令如何跳转循环。有人告诉我 goto 命令很丑陋等等,所以如果你们中有人能帮助我解决这个问题,我会很高兴。谢谢大家!

最佳答案

使用函数是摆脱这些 goto 的最佳方法。不过,不要仅仅因为有人告诉你就不喜欢 goto

这是一本关于goto的好读物,当我被老师和互联网说服他们是邪恶时,它启发了我关于goto的知识。现在我喜欢goto;它只是有它的应用程序,在这些应用程序上它是最好的可用工具: https://koblents.com/Ches/Links/Month-Mar-2013/20-Using-Goto-in-Linux-Kernel-Code/

能力越大,责任越大:)

现在你的代码:

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


#define SPECKLE '.'
#define WALL '#'
#define SIZE 1000


#if 01
static char aa[1000][1000] = {
{'#','.','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','.','.','.','#','.','.','.','.','.','.','.','.','.','.','.','#'},
{'#','.','#','#','#','.','#','#','#','#','#','.','#','.','#','#','#'},
{'#','.','.','.','.','.','.','.','.','.','#','.','#','.','.','.','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','.','#'}
};
#endif
static int x, y;


static void print_arr(void);
static void continue_in_road(char a[SIZE][SIZE]);
static bool check(char a[SIZE][SIZE]);
static void switch_to_left(char a[SIZE][SIZE]);


/*
* Note that int main(void) is completely valid, and in fact is what you need,
* because you're not using the arguments.
*/
int main(void)
{
int i, j;
// char a[SIZE][SIZE];

x = 0;
y = 1;

// for (i = 0; i < SIZE; i++)
// scanf(" %s", &a[i]);

continue_in_road(aa);

print_arr();

for (i = 0; i < 5; i++) {
for (j = 0; j < 18; j++) {
if(aa[i][j] == '1')
aa[i][j] = '*';
if(aa[i][j] == '2')
aa[i][j] = '.';
printf("%c",aa[i][j]);
}
printf("\n\n");
}

print_arr();

return 0;
}


static void continue_in_road(char a[SIZE][SIZE])
{
bool initial_chk = true;

while (1) {
if (initial_chk) {
if ((y == 16) && (x == 4))
return;

if (a[x][y] == SPECKLE) {
a[x][y] = '1';
y++;
continue;
} else if (a[x][y] == WALL) {
y--;
}
}

if (check(a)) {
initial_chk = true;
continue;
}

initial_chk = false;
switch_to_left(a);
}
}

static bool check(char a[SIZE][SIZE])
{

while (1) {
if (a[x+1][y] == SPECKLE) {
x++;
return true;
} else if (a[x-1][y] == SPECKLE) {
x--;
return true;
} else if (a[x][y-1] == WALL) {
a[x][y] = '2';
x--;
} else if (a[x][y-1] == '1') {
a[x][y] = '2';
y--;
} else if (a[x][y-1] == SPECKLE) {
y--;
return false;
}
}
}

static void switch_to_left(char a[SIZE][SIZE])
{

while (1) {
if (a[x][y] == SPECKLE) {
a[x][y] = '1';
y--;
} else if(a[x][y] == WALL) {
y++;
return;
}
}
}

/* What is this supposed to do? */
static void print_arr(void)
{
char a[SIZE][SIZE];
int i, j;

for (i = 0; i < 5; ++i) {
for (j = 0; j < 7; ++j)
printf("%c",a[i][j]);
printf("\n");
}
}

如果你像这样编译它就会运行。我已对输入循环进行了注释,以使用您创建的 static const 数组初始值设定项,以便轻松测试。

但是我认为你应该重新考虑更改函数的结构,因为很难理解它在做什么。如果我理解它的作用,我可以改进更多的代码,但由于我不太理解它,所以我限制自己摆脱那些 goto

关于c - 在 C 中搜索道路时如何替换代码中的 goto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53539220/

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