2013-11-26 75 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個字符。如果你自己可以做到這一點,我很樂意刪除這些評論。 –