如何替换c中的goto部分
首先,输入正方形顶点的坐标。每个坐标不能超过1000。 其次,输入当前位置的坐标。坐标位置不能超过正方形。 我想找到从坐标到正方形的最小距离。 这就是我想做的内容。所以我编写了下面的代码。
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
void min_comparison(int a, int b, int c, int d);
typedef struct coordinate {
unsigned int x; unsigned int y;
unsigned int w; unsigned int h;
}coord;
int main()
{
int nomi1;
int nomi2;
coord cod;
presentcoord:
printf("enter the coordinates of the vertex of the square: ");
scanf("%d %d", &cod.x, &cod.y);
if (cod.x >= 1001 || cod.y >= 1001)
{
printf("The vertex coordinates cannot exceed 1000.\n");
goto presentcoord;
}
presentloca:
printf("enter the current location of the coordinates: ");
scanf("%d %d", &cod.w, &cod.h);
if (cod.w >= cod.x || cod.h >= cod.y)
{
printf("location of the coordinates can't excced %d, %d.\n",cod.x-1,cod.y-1);
goto presentloca;
}
printf("\n");
nomi1 = cod.x - cod.w;
nomi2 = cod.y - cod.h;
min_comparison(cod.x, cod.y, nomi1, nomi2);
}
void min_comparison(int a,int b,int c,int d)
{
int min; int min1; int result;
if (a > c)
min = c;
else min = a;
if (b > d)
min1 = d;
else min1 = b;
if (min > min1)
result = min1;
else result = min;
printf("minimum distance from coordinates to square is %d.\n", result);
return 0;
}
我在 Google 上查了一下,它说“goto”是一个错误的代码。
所以我想知道如何替换代码中的“goto”部分。
英语不是我的母语。 所以也指出错误的英文表达。
如果您让我知道,我将不胜感激。
First, enter the coordinates of the vertex of the square. and each coordinate can not exceed 1000.
Second, enter the current location of the coordinates. location of the coordinate cannot exceed square.
and i want to find the minimum distance from coordinates to square.
This is the content that i want to make. So i made the code below.
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
void min_comparison(int a, int b, int c, int d);
typedef struct coordinate {
unsigned int x; unsigned int y;
unsigned int w; unsigned int h;
}coord;
int main()
{
int nomi1;
int nomi2;
coord cod;
presentcoord:
printf("enter the coordinates of the vertex of the square: ");
scanf("%d %d", &cod.x, &cod.y);
if (cod.x >= 1001 || cod.y >= 1001)
{
printf("The vertex coordinates cannot exceed 1000.\n");
goto presentcoord;
}
presentloca:
printf("enter the current location of the coordinates: ");
scanf("%d %d", &cod.w, &cod.h);
if (cod.w >= cod.x || cod.h >= cod.y)
{
printf("location of the coordinates can't excced %d, %d.\n",cod.x-1,cod.y-1);
goto presentloca;
}
printf("\n");
nomi1 = cod.x - cod.w;
nomi2 = cod.y - cod.h;
min_comparison(cod.x, cod.y, nomi1, nomi2);
}
void min_comparison(int a,int b,int c,int d)
{
int min; int min1; int result;
if (a > c)
min = c;
else min = a;
if (b > d)
min1 = d;
else min1 = b;
if (min > min1)
result = min1;
else result = min;
printf("minimum distance from coordinates to square is %d.\n", result);
return 0;
}
I looked it up on Google and it said "goto" is a bad code.
so i want to know how i replace the "goto" part of my code.
and English is not my first language.
So point out the wrong English expression, too.
I'd appreciate it if you let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以轻松地转换您的代码。
这部分
可以简化为这种通用形式:
可以转换为通用循环:
在您的情况下,这将是
You can easily transform your code.
This part
can be reduced to this generic form:
That can be transformed to a generic loop:
In your case this would be
Gerhardh 提出的
do-while
版本很好。另一个更紧凑的替代方案是:The
do-while
version proposed by Gerhardh is fine. Another more compact alternative is this:这应该有效(未经测试)。
continue
将像goto
一样返回到 while 循环的开头,而break
将像goto
一样返回到 while 循环的开头。 while 循环结束。This should work, (not tested).
continue
will act like agoto
back to the beginning of the while loop andbreak
will act like agoto
to the end of the while loop.这个答案是一个简短的代码审查,但我认为它受到了OP的欢迎。我不会触及循环,其他答案已涵盖该循环。
最初,我的印象是
coord
更像是一个 轴对齐框,但看到它实际上是两个独立的坐标。我希望他们能得到一分。这是在二维上工作,然后可以写,
min_comparison
或minimum_distance
理想情况下应该是独立的(nomi1 = cod.x - cod.w< /code> 应该在函数中)并返回最小距离,而不是打印它。如果你想要有符号的距离(向量?),那么我认为有符号的坐标会很好。
必须检查
scanf
。这里,返回值为:EOF
、0、1 或 2。即使只是这样,在遇到意外输入时也会好得多。
This answer is kind of a brief code review, but I think it is welcomed by the OP. I'm not going to touch on the loop, which is covered by the other answers.
Initially, I was under the impression that
coord
is more of an axis-aligned box, but saw that it is actually two separate coördinates. I would expect them to be one point. This is working with two-dimensions,Then one could write,
min_comparison
orminimum_distance
should ideally be self-contained (nomi1 = cod.x - cod.w
should be in the function) and return the minimum distance, not print it. If you want signed distances (vectors?), then I think a signed coödinate would be good.One has to check the return values of
scanf
. Here, the return value is:EOF
, 0, 1, or 2. Even just,would be much better when faced with unexpected inputs.