2013-11-26 69 views
11

所以我的教授要求我们创建一个switch语句。我们被允许使用只有“SWITCH”声明来执行该程序。他希望我们输入一个数字,然后在数字范围内显示,然后如下所示输入公文包号码。现在...我知道这种类型的程序更容易使用IF语句。做案例1:案例2:案例3 ...案例30将工作,但由于数量范围,将花费太多时间。C - 多个案例号的开关

#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(); 
     } 

我的教授告诉我们,如何做到这一点有一个简短的方法,但不会告诉我们如何。我能想到缩短它的唯一方法是使用IF,但我们不允许。任何想法,我如何可以使这项工作?

+0

只是为了检查,你确定第一个案件从1到30?而不是1到29? –

+3

您是否在考虑'案例1:案例30:'会匹配范围内的任何数字?它不会。 – John3136

+0

您的第一对'case'标签只能找到2个值:1和30;他们忽略2..29。对于下一个“范围”也是如此,等等。只使用开关,你就会感受到一个痛苦的世界。 –

回答

21

随着GCC和铛,您可以使用情况的范围,就像这样:

switch (x){ 

case 1 ... 30: 
    printf ("The number you entered is >= 1 and <= 30\n"); 
    break; 
} 

唯一的交叉编译器解决方案是使用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; 
} 

编辑:使用一些东西到switch (x/10)的效果是这样做的另一个好方法。当范围不是10的差异时,使用GCC范围可能会更简单,但另一方面,您的教授可能不会将GCC范围作为答案。

7

如果范围是一致的,那么你可以扔掉一些数据:

switch (x/10) 
{ 
    case 0: 
    case 1: 
    case 2: // x is 0 - 29 
    break ; 

    // etc ... 
} 

否则,你就必须做周围的边缘两轮牛车的一点点。

+0

+1:或多或少 - '(x-1)/ 10'会为1..30提供'[012]';重复。麻烦将是-8..0(也给0)。 –

3
Try this ... 

#include <stdio.h> 
#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(); 
     } 
+1

您目前对'ceil()'的使用稍微不正确。你用'30'来尝试用'x'的整数除法,因为整数除法被取消,这已经是整数了。也许你是说'ceil(x/30.0)'? –

+0

通过编辑我的答案进行更改.. – uhs

+2

不幸的是,我自己做不到这一点,因为编辑少于6个字符。如果你自己可以做到这一点,我很乐意删除这些评论。 –