如何替换c中的goto部分

发布于 2025-01-12 04:58:20 字数 1608 浏览 0 评论 0原文

首先,输入正方形顶点的坐标。每个坐标不能超过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱的十字路口 2025-01-19 04:58:20

您可以轻松地转换您的代码。
这部分

    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;
    }

可以简化为这种通用形式:

label:
   <do something>
   <Check if we are done>
   <If not, goto label>

可以转换为通用循环:

   bool done = false;
   do 
   {
      <do something>
      <check if we are done>
      <if yes: done=true;>
      <else: Print message>
   }while (!done);

在您的情况下,这将是

    bool done = false;
    do 
    {
      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");
      }
      else 
      {
        done = true;
      }
    } while (!done);

You can easily transform your code.
This part

    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;
    }

can be reduced to this generic form:

label:
   <do something>
   <Check if we are done>
   <If not, goto label>

That can be transformed to a generic loop:

   bool done = false;
   do 
   {
      <do something>
      <check if we are done>
      <if yes: done=true;>
      <else: Print message>
   }while (!done);

In your case this would be

    bool done = false;
    do 
    {
      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");
      }
      else 
      {
        done = true;
      }
    } while (!done);
绿光 2025-01-19 04:58:20

Gerhardh 提出的 do-while 版本很好。另一个更紧凑的替代方案是:

while(1)
{
  printf("enter the coordinates of the vertex of the square: ");
  scanf("%d %d", &cod.x, &cod.y);

  if (cod.x <= 1000 && cod.y <= 1000)
    break; // stop if input ok

  printf("The vertex coordinates cannot exceed 1000.\n");
}

The do-while version proposed by Gerhardh is fine. Another more compact alternative is this:

while(1)
{
  printf("enter the coordinates of the vertex of the square: ");
  scanf("%d %d", &cod.x, &cod.y);

  if (cod.x <= 1000 && cod.y <= 1000)
    break; // stop if input ok

  printf("The vertex coordinates cannot exceed 1000.\n");
}
荒岛晴空 2025-01-19 04:58:20

这应该有效(未经测试)。 continue 将像 goto 一样返回到 while 循环的开头,而 break 将像 goto 一样返回到 while 循环的开头。 while 循环结束。

while(1){
    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");
        continue;
    }

    break;
}

This should work, (not tested). continue will act like a goto back to the beginning of the while loop and break will act like a goto to the end of the while loop.

while(1){
    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");
        continue;
    }

    break;
}
七堇年 2025-01-19 04:58:20

这个答案是一个简短的代码审查,但我认为它受到了OP的欢迎。我不会触及循环,其他答案已涵盖该循环。

最初,我的印象是 coord 更像是一个 轴对齐框,但看到它实际上是两个独立的坐标。我希望他们能得到一分。这是在二维上工作,

struct coord { unsigned x, y; };

然后可以写,

unsigned minimum_distance(const struct coord point, const struct coord enclosing_square); 

min_comparisonminimum_distance 理想情况下应该是独立的(nomi1 = cod.x - cod.w< /code> 应该在函数中)并返回最小距离,而不是打印它。如果你想要有符号的距离(向量?),那么我认为有符号的坐标会很好。

必须检查 scanf。这里,返回值为:EOF、0、1 或 2。即使只是这样,

if(scanf("%d %d", &cod.x, &cod.y) != 2) exit(EXIT_FAILURE);

在遇到意外输入时也会好得多。

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,

struct coord { unsigned x, y; };

Then one could write,

unsigned minimum_distance(const struct coord point, const struct coord enclosing_square); 

min_comparison or minimum_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,

if(scanf("%d %d", &cod.x, &cod.y) != 2) exit(EXIT_FAILURE);

would be much better when faced with unexpected inputs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文