C 更换开关箱中的goto
我写了这段代码来执行多个数学运算,用主菜单来选择运算,并重复它,直到您需要并返回到主菜单。我已使用开关盒将其循环并转到。我想替换go-to,并希望获得有关如何在不使用go-to的情况下使其以相同方式运行的建议。 多谢各位C 更换开关箱中的goto,c,switch-statement,goto,C,Switch Statement,Goto,我写了这段代码来执行多个数学运算,用主菜单来选择运算,并重复它,直到您需要并返回到主菜单。我已使用开关盒将其循环并转到。我想替换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 mathema
//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");
}
}
构造只能是大纲:
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);
考虑到您不想以goto函数以外的任何方式更改程序,您可以将整个代码置于do while循环、while循环或for循环下,如下所示:
do {
....
} while(rep != 'y')
找到了解决我问题的办法。用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.
}
这段代码根本不编译。rep=='y'?{goto-fact;}:{goto-main;};是不正确的C,如果rep='y'转到事实,则应该是;否则,就转到main;,我在代码块中多次使用它。至少10-20次。它顺利地完成了。编译时它会报告什么错误?我想说的是,第一步是将执行单个作业的代码fact、sum、oe、prime、mul等分解为单独的函数。然后,您可以更轻松地在这些函数内循环,或者通过重复调用它们来循环。不管是哪种方式,do{work;}while上都有一些变化!完成;将允许您根据用户的需要随时重复。对不起,这段代码中有太多错误,根本无法修复。不仅是goto问题,还应排除continue、FFLUSSTDIN、void main、MS DOS库、全局变量等,并采用2或4个空格的一致缩进样式。总的来说,你需要一个学习C语言的新来源。你现在的一位老师/一本书是不可信的,他们正在教你坏习惯和不正确的习惯。@Jabberwocky:你的编译器是一致的;代码使用了一个GCC扩展,语法为{…}。谢谢!将返回结果。这将更改程序的行为-如果重复,是否在原始中?肯定的是,只重复选择的操作,而不是主菜单。“我看到了……改变了它。”如果输入不符合预期,例如,当预期数字时输入字母,则不会对变量进行赋值。在某些情况下,这可能导致未定义的行为,当然也可能导致意外行为。scanf返回成功分配的数量,因此在int ret_val=scanf%d之后,&n;如果给出了正确的输入,ret_val变量将保持1,否则为0,或者在较少见的情况下为EOF。另外,请注意,使用全局变量是一个非常坏的习惯,很少有必要。