我已编写此代码来执行多项数学运算,并使用主菜单选择了该运算,然后重复执行直到您想要并返回主菜单。我已经使用开关盒和去循环。我想替换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");
}
}
答案 0 :(得分:4)
构造可以是(仅概述):
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);
答案 1 :(得分:3)
考虑到除了goto函数外,您不想以任何其他方式更改程序,您可以将整个代码放在以下的do while循环,while循环或for循环下:
do {
....
} while(rep != 'y')
答案 2 :(得分:2)
解决了我的问题。用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.
}