替换开关盒中的 goto
Replacement for goto in switch case
我写了这段代码来执行多项数学运算,用主菜单来select运算,重复直到你想要再回到主菜单。我已经使用开关盒将其循环并转到。我想替换 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");
}
}
构造可以是(仅大纲):
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')
找到了我的问题的解决方案。用 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.
}
我写了这段代码来执行多项数学运算,用主菜单来select运算,重复直到你想要再回到主菜单。我已经使用开关盒将其循环并转到。我想替换 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");
}
}
构造可以是(仅大纲):
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')
找到了我的问题的解决方案。用 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.
}