简体   繁体   English

取消此goto语句

[英]Elimination of this goto statement

Was searching from ACM programming contest archives and found a solution to the coconuts program: It has a goto in it- how do I eliminate this? 在从ACM编程竞赛档案中搜索时,找到了一个椰子程序的解决方案:它有一个goto,如何消除这个问题? Is there a template or procedure to follow to do so. 有没有遵循的模板或过程。 Thanks 谢谢

/*
1997 East-Central ACM regional programming contest
Held on November 8, 1997
Coconuts, Revisited -- Problem 3
Sample solution by Ed Karrels, Ed@tool.com
November 1997
*/
#include <stdio.h>
/* return 1 if this number of coconuts can be divided up
properly between this number of people */
int SplitCoco(long n_coco, long n_people) {
long i;
for (i=0; i<n_people; i++) {
/* check that the coconuts divide by the number of people
plus one remainder */
    if (n_coco % n_people != 1) return 0;
    /* remove 1 for the monkey, and one person's share */
        n_coco = n_coco - 1 - (n_coco / n_people);
    }
    /* check that the remaining coconuts divide evenly among
    the people */
    return (n_coco % n_people) == 0;
}


int main() {
    long n_coco;
    long n_people;
    long i, j, k;

    FILE *inf = stdin;
    while (fscanf(inf, "%ld", &n_coco), n_coco!=-1) {
    /* starting at the # of coconuts-1, count down until
    a number of people is found that works */
        for (n_people=n_coco-1; n_people > 1; n_people--) {
            if (SplitCoco(n_coco, n_people)) {
                 printf("%ld coconuts, %ld people and 1 monkey\n",
                 n_coco, n_people);
                 goto found;
                 /* OK, so yea, I put a 'goto' in my code :-)
                it was quick and it works. I don't do
                it often, I swear. */
            }
        }
     /* if no number of people greater than 1 works, there is
     no solution */
        printf("%ld coconuts, no solution\n", n_coco);
        found:
    }
    return 0;
}

In your case, you could make a separate routine countaining the while and replace the goto found with a return . 在您的情况下,您可以制作一个单独的例程来计算while并用return替换goto foundgoto found

In general, you might replace each goto with a flag and some while loop. 通常,您可以将每个goto替换为一个标志和一些while循环。 That does not make the code easier to read. 这不会使代码更易于阅读。

replace 更换

goto found;

with

 break;

replace 更换

 printf("%ld coconuts, no solution\n", n_coco);

with: 与:

if(n_people <= 1)
    printf("%ld coconuts, no solution\n", n_coco);

Another answer has already suggested this but also suggested it was less readable - I disagree - I find it reads like what it means. 另一个答案已经暗示了这一点,但也暗示了它的可读性较差-我不同意-我发现它读起来像是什么意思。 Either way like it or not it deserves illustrating as a possible solution. 无论喜欢与否,都应将其说明为可能的解决方案。

My solution requires a definition of a boolean type. 我的解决方案需要一个布尔类型的定义。 I have assumed the the C99 <stdbool.h> definition (or C++ compilation) 我假设使用C99 <stdbool.h>定义(或C ++编译)

This is just the body of the outer while loop: 这只是外部while循环的主体:

    bool found = false ;
    for (n_people=n_coco-1; n_people > 1 && !found; n_people--) 
    {
        found = SplitCoco(n_coco, n_people)
        if( found ) 
        {
            printf("%ld coconuts, %ld people and 1 monkey\n", n_coco, n_people);
        }
    }

    if( !found ) 
    {
        /* if no number of people greater than 1 works, there is no solution */
        printf("%ld coconuts, no solution\n", n_coco);
    }

In some cases it is possible that the additional per-loop test is prohibitive, but I would suggest that in most cases it is insignificant. 在某些情况下,附加的每个循环测试可能会令人望而却步,但我建议在大多数情况下,它是微不足道的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM