C语言 C - 具有多个案例编号的开关
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20208628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C - Switch with multiple case numbers
提问by user2649696
So my professor asked us to create a switch statement. We are allowed to use only the "SWITCH"statement to do the program. He wants us to input a number and then display it if it is on the number range and what briefcase number will be taken as shown below. Now... I know that for this type of program it is easier to use the IF statement. Doing Case 1: Case 2: Case 3...Case 30 will work but will take too much time due to the number range.
所以我的教授要求我们创建一个 switch 语句。我们只允许使用“SWITCH”语句来执行程序。他希望我们输入一个数字,然后显示它是否在数字范围内,以及将采取什么公文包编号,如下所示。现在...我知道对于这种类型的程序,使用IF 语句更容易。执行案例 1:案例 2:案例 3...案例 30 将起作用,但由于数字范围,将花费太多时间。
#include <stdio.h>
main()
{
int x;
char ch1;
printf("Enter a number: ");
scanf("%d",&x);
switch(x)
{
case 1://for the first case #1-30
case 30:
printf("The number you entered is >= 1 and <= 30");
printf("\nTake Briefcase Number 1");
break;
case 31://for the second case #31-59
case 59:
printf("The number you entered is >= 31 and <= 59");
printf("\nTake Briefcase Number 2");
break;
case 60://for the third case #60-89
case 89:
printf("The number you entered is >= 60 and <= 89");
printf("\nTake Briefcase Number 3");
break;
case 90://for the fourth case #90-100
case 100:
printf("The number you entered is >= 90 and <= 100");
printf("\nTake Briefcase Number 4");
break;
default:
printf("Not in the number range");
break;
}
getch();
}
My professor told us that there is a shorter way on how to do this but won't tell us how. The only way I can think of shortening it is by using IF but we are not allowed to. Any Ideas on how I can make this work out?
我的教授告诉我们,有一种更短的方法可以做到这一点,但不会告诉我们如何做。我能想到的缩短它的唯一方法是使用 IF 但我们不允许这样做。关于如何解决这个问题的任何想法?
回答by tay10r
With GCC and CLang, you can use case ranges, like this:
使用 GCC 和 CLang,您可以使用 case 范围,如下所示:
switch (x){
case 1 ... 30:
printf ("The number you entered is >= 1 and <= 30\n");
break;
}
The only cross-compiler solution is to use case statements like this:
唯一的交叉编译器解决方案是使用这样的 case 语句:
switch (x){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
printf ("The number you entered is >= 1 and <= 6\n");
break;
}
Edit:Using something to the effect of switch (x / 10)is another good way of doing this. It may be simpler to use GCC case ranges when the ranges aren't differences of 10, but on the other hand your professor might not take a GCC extension as an answer.
编辑:使用某种效果switch (x / 10)是另一种这样做的好方法。当范围不是 的差异时,使用 GCC 案例范围可能更简单10,但另一方面,您的教授可能不会将 GCC 扩展作为答案。
回答by woolstar
If the ranges are consistent, then you can throw away some of the data:
如果范围一致,那么您可以丢弃一些数据:
switch (x / 10 )
{
case 0:
case 1:
case 2: // x is 0 - 29
break ;
// etc ...
}
Otherwise you'll have to do a little bit of hackery around the edges.
否则你将不得不在边缘做一些hackery。
回答by uhs
Try this ...
#include <stdio.h>
main()
{
int x;
char ch1;
printf("Enter a number: ");
scanf("%d",&x);
int y=ceil(x/30.0);
switch(y)
{
case 1:
printf("The number you entered is >= 1 and <= 30");
printf("\nTake Briefcase Number 1");
break;
case 2:
printf("The number you entered is >= 31 and <= 60");
printf("\nTake Briefcase Number 2");
break;
case 3:
printf("The number you entered is >= 61 and <= 90");
printf("\nTake Briefcase Number 3");
break;
case 4:
printf("The number you entered is >= 91 and <= 100");
printf("\nTake Briefcase Number 4");
break;
default:
printf("Not in the number range");
break;
}
getch();
}

