【问题标题】:Replacement for goto in switch case更换开关盒中的 goto
【发布时间】:2023-03-16 15:09:01
【问题描述】:

我编写了这段代码来执行多个数学运算,用主菜单选择运算,然后重复它直到你想要并返回到主菜单。我已经使用 switch case 和 go-to 循环了它。我想替换 go-to,并希望得到有关如何在不使用 go-to 的情况下使其以相同方式运行的建议。 谢谢。

//I want the program of be infinite until you want to exit.Thus I used goto to continuously loop it back to either main menu or the mathematical operation you are in. 
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

int a,b,i,num,n;
char rep;
void main (void)

{
//main menu-(main:-for goto function)
main:
{
printf("\nMAIN MENU\n\n1. Factorial\n2. Sum\n3. Odd/Even\n4. Prime Number\n5. Multiplication\n6. Exit\n");
scanf("%d", &n);
}
switch (n)
{
    case 1:
        {
            fact:
            {
            printf("Number- ");
            scanf("%d", &num);
            a=1;
        for(i=1;i<=num;i++)
        {
            a=a*i;
            continue;
        }
        printf("\nFactorial of %d= %d\n\n", num, a);

//This is how I loop it back to main menu or for repeating the mathematical operation using combination of conditional operators and goto.
        printf("Repeat? y/n- ");
        fflush(stdin);
        scanf("%c", &rep);
        (rep=='y')?({goto fact;}):({goto main;});
        }
        }
    case 2:
        {
            sum:
            {
                a=0;
            printf("Value of repetitions- ");
            scanf("%d", &num);
            printf("Enter Digits to sum:\n");

            for(i=1;i<=num;i++)
            {
                scanf("%d", &b);
                printf("+ ");
                a=a+b;
                continue;
            }
            printf("\nThe Sum of Digits= %d\n\n", a);
            printf("Repeat? y/n- ");
            fflush(stdin);
            scanf("%c", &rep);
            (rep=='y')?({goto sum;}):({goto main;});
            }
        }

    case 3:
        {
            oe:
            {
                printf("Enter a Number- ");
                scanf("%d", &a);
                (a%2==0)?(printf("\n%d is an Even Number\n",a)):(printf("\n%d is an Odd number\n", a));
            printf("\nRepeat? y/n- ");
            fflush(stdin);
            scanf("%c", &rep);
            (rep=='y')?({goto oe;}):({goto main;});
            }
        }
    case 4:
        {
            prime:
                {
                    printf("\nEnter a Number- ");
                    scanf("%d",&num);
                    if(num==2)
                        printf("\n\n%d is a Prime Number\n\n", num);

                    for(i=2;i<=num-1;i++)
                    {

                        (num%i==0)?({printf("\n\n%d is Not a Prime Number.\n\n", num);break;}):({printf("\n\n%d is a Prime Number\n\n", num);break;});

                    }

                    printf("\nRepeat? y/n- ");
                    fflush(stdin);
            scanf("%c", &rep);
            (rep=='y')?({goto prime;}):({goto main;});
                }

        }
    case 5:
        {
            mul:
            {
                a=1;
            printf("Value of repetitions- ");
            scanf("%d", &num);
            printf("Enter Digits to multiply:\n");

            for(i=1;i<=num;i++)
            {
                scanf("%d", &b);
                printf("* ");
                a=a*b;
                continue;
            }
            printf("\nThe Multiplication of Digits= %d\n\n", a);
            printf("Repeat? y/n- ");
            fflush(stdin);
            scanf("%c", &rep);
            (rep=='y')?({goto mul;}):({goto main;});
            }
        }
//Case 6 is only for aesthetic reasons.
        case 6:
        printf("\n\nPress Enter\n\n");
}
}

【问题讨论】:

  • 这段代码根本无法编译。 (rep=='y')?({goto fact;}):({goto main;});是不正确的C,应该是if (rep == 'y') goto fact; else goto main;
  • 我在 code:blocks 中多次使用它。至少喜欢10-20次。它顺利地遵守了。编译时报什么错误?
  • 我想说,第一步是将执行单个工作(fact、sum、oe、prime、mul 等)的代码分解为单独的函数。然后,您可以更轻松地循环——在这些函数中,或者通过重复调用它们。无论哪种方式,do{work();}while(!done()); 的一些变化将允许您根据用户的需要多次重复。
  • 抱歉,这段代码有太多错误,根本无法挽救。不仅是 goto 问题,还要摆脱 continue、fflush(stdin)、void main、MS DOS 库、全局变量等。请采用一致的缩进样式,包含 2 或 4 个空格。总体而言,您需要一个新的学习来源 C。您当前的学习来源(老师/书籍)不可信,他们正在教您不良和不正确的习惯。
  • @Jabberwocky:你的编译器是一致的;该代码使用 GCC 扩展 — statement expressions,其中语法为 ({ … })

标签: c switch-statement goto


【解决方案1】:

构造可以是(仅大纲):

    do {
        printf("\nMAIN MENU\n\n1. Factorial\n2. Sum\n3. Odd/Even\n4. Prime Number\n5. Multiplication\n6. Exit\n");
        scanf("%d", &n);
        switch (n) {
             case 1:
                  do {
                      //do your stuff
                      printf("repeat?")
                      scanf(" %c", &rep);
                  } while (rep=='y');
                  break;
        }

    } while (n<6);

【讨论】:

  • 谢谢!将返回结果。
  • 这会改变程序的行为 - 在原来的 if "repeat?"确认后,只重复选中的操作,不重复主菜单。
  • @Armali,我明白了……改了。
  • @Armali 该程序在将其更改为 do while 后就像一个魅力,保持程序的行为相同。
【解决方案2】:

考虑到您不想以除 goto 函数之外的任何方式更改程序这一事实,您可以将整个代码置于 do while 循环、while 循环或 for 循环中,如下所示:

do {
....
} while(rep != 'y')

【讨论】:

    【解决方案3】:

    找到了我的问题的解决方案。用 do-while 循环替换了多个 Go-to。 感谢@Paul Ogilvie 和@Harshit Joshi 的建议。 欢迎任何关于代码改进的建议!

    #include<conio.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    int a,b,i,num,n;
    char rep;
    void main (void)
    
    {
        do
        {
            printf("\nMAIN MENU\n\n1. Factorial\n2. Sum\n3. Odd/Even\n4. Prime Number\n5. Multiplication\n6. Exit\n\n");
            scanf("%d", &n);
    
            switch(n)
            {
                case 1:
     //Replaced Goto from here with do while loop
                    do
                    {
                        printf("\nNumber- ");
                        scanf("%d", &num);
                        a=1;
                        for(i=1;i<=num;i++)
                            {
                                a=a*i;
                                continue;
                            }
                            printf("\nFactorial of %d= %d\n\n", num, a);
                            printf("Repeat? y/n- ");
                            fflush(stdin);
                            scanf("%c", &rep);
                    }
                    while(rep=='y');
                    break;
    
                case 2:
                    do
                    {
                        a=0;
                        printf("\nValue of repetitions- ");
                        scanf("%d", &num);
                        printf("Enter Digits to sum:\n");
    
                        for(i=1;i<=num;i++)
                        {
                            scanf("%d", &b);
                            if(i<num)
                                printf("+ ");
                            a=a+b;
                        }
                        printf("\nThe Sum of Digits= %d\n\n", a);
                        printf("Repeat? y/n- ");
                        fflush(stdin);
                        scanf("%c", &rep);
                    }
                    while(rep=='y');
                    break;
    
                case 3:
                    do
                    {
                        printf("\nEnter a Number- ");
                        scanf("%d", &a);
                        (a%2==0)?(printf("\n%d is an Even Number\n",a)):(printf("\n%d is an Odd number\n", a));
                        printf("\nRepeat? y/n- ");
                        fflush(stdin);
                        scanf("%c", &rep);
                    }
                    while(rep=='y');
                    break;
    
                case 4:
                    do
                    {
                        printf("\nEnter a Number- ");
                        scanf("%d",&num);
                        if(num==2)
                            printf("\n\n%d is a Prime Number\n\n", num);
    
                        for(i=2;i<=num-1;i++)
                        {
                            (num%i==0)?({printf("\n\n%d is Not a Prime Number.\n\n", num);break;}):({printf("\n\n%d is a Prime Number\n\n", num);break;});
                        }
                        printf("\nRepeat? y/n- ");
                        fflush(stdin);
                        scanf("%c", &rep);
                    }
                    while(rep=='y');
                    break;
    
                case 5:
                    do
                    {
                        a=1;
                        printf("\nValue of repetitions- ");
                        scanf("%d", &num);
                        printf("Enter Digits to multiply:\n");
    
                        for(i=1;i<=num;i++)
                        {
                            scanf("%d", &b);
                            printf("* ");
                            a=a*b;
                            continue;
                        }
                        printf("\nThe Multiplication of Digits= %d\n\n", a);
                        printf("Repeat? y/n- ");
                        fflush(stdin);
                        scanf("%c", &rep);
                    }
                    while(rep=='y');
                    break;
            }
        }
        while(rep=='n');
    //Used the above given condition to continue the loop in Main Menu as well. Works properly.
    }
    

    【讨论】:

    • 考虑检查对scanf() 的调用返回的值。如果输入不符合预期,例如当需要一个数字时输入一个字母,不会对变量进行赋值。在某些情况下,这可能会导致未定义的行为,并且肯定会导致意外行为。 scanf() 返回成功分配的次数,因此在int ret_val = scanf("%d", &amp;n); 之后,如果输入正确,ret_val 变量将保持 1,否则为 0(或 EOF 在极少数情况下)。另外请注意,使用全局变量是一个非常不好的习惯,而且很少需要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多