2014-01-07 30 views
9

我想寫,有很多比較略大於小於switch語句ç

 
Write a program in 「QUANT.C」 which 「quantifies」 numbers. Read an integer 「x」 and test it, producing the 
following output: 

x greater than or equal to 1000 print 「hugely positive」 
x from 999 to 100 (including 100) print 「very positive」 
x between 100 and 0 print 「positive」 
x exactly 0 print 「zero」 
x between 0 and -100 print 「negative」 
x from -100 to -999 (including -100) print 「very negative」 
x less than or equal to -1000 print 「hugely negative」 

Thus -10 would print 「negative」, -100 「very negative」 and 458 「very positive」. 

然後我試着用開關來解決它,但它沒有工作代碼,做我必須使用if語句來解決它,還是有一種方法可以使用switch來解決它?

#include <stdio.h> int main(void) { int a=0; printf("please enter a number : \n"); scanf("%i",&a); switch(a) { case (a>1000): printf("hugely positive"); break; case (a>=100 && a<999): printf("very positive"); break; case (a>=0 && a<100): printf("positive"); break; case 0: printf("zero"); break; case (a>-100 && a<0): printf("negative"); break; case (a<-100 && a>-999): printf("very negative"); break; case (a<=-1000): printf("hugely negative"); break; return 0; }
+3

'switch'只能處理具有常量積分值的精確比較。你將不得不使用'if'和'else'。 –

+0

定義'沒有工作' – deW1

+0

*七*編譯錯誤應該告訴你*某事*(和1警告:'控制到達非空函數結束)。 – usr2564301

回答

4

有沒有乾淨的方法來解決這個問題,因爲案例需要是整型。看看if-else if-else。

2

(a>1000)的計算結果爲1 [true]或0 [false]。

編譯,將得到錯誤

test_15.c:12: error: case label does not reduce to an integer constant 

這意味着,你必須使用一個integer constant價值爲case標籤。 If-else if-else循環應該適用於這種情況。

+0

a> 1000評估爲布爾值而不是整數1或0. –

+0

@EricFortin它是C. – Maroun

+0

@Maroun Maroun你說得對。 –

0

爲什麼你喜歡使用開關?

我在問,因爲這聽起來很像'家庭作業問題'。編譯器應該處理if/else構造,就像交換機一樣高效(即使你不處理範圍)。

開關不能處理的範圍,你所顯示的,但你可以(使用的if/else),然後使用switch語句來輸出答案找到一種方法,通過第一分類的輸入,包括開關。

4

如果你正在使用gcc,你有「運氣」,因爲它支持的正是你想要使用語言擴展:

#include <limits.h> 
... 

switch(a) 
{ 
case 1000 ... INT_MAX: // note: cannot omit the space between 1000 and ... 
    printf("hugely positive"); 
    break; 
case 100 ... 999: 
    printf("very positive"); 
    break; 
... 
} 

這是不規範的,雖然,其它編譯器不會理解你的代碼。經常提到您應該只使用標準功能編寫程序(「可移植性」)。

所以考慮使用 「精簡」 if-elseif-else結構:

if (a >= 1000) 
{ 
    printf("hugely positive"); 
} 
else if (a >= 100) 
{ 
    printf("very positive"); 
} 
else if ... 
... 
else // might put a helpful comment here, like "a <= -1000" 
{ 
    printf("hugely negative"); 
} 
+0

其他編譯器爲什麼不理解在switch語句中使用省略號(導致「非標準」)? –

6

開關少的if-else少的方法。警告:不要試圖將您的任務轉換爲「您的」解決方案,因爲您可能會被問到它的工作原理。

#include <stdio.h> 

int main(void) 
{ 
    int a=0, i; 
    struct { 
     int value; 
     const char *description; 
    } list[] = { 
     { -999, "hugely negative" }, 
     { -99, "very negative" }, 
     { 0, "negative" }, 
     { 1, "zero" }, 
     { 100, "positive" }, 
     { 1000, "very positive" }, 
     { 1001, "hugely positive" } 
    }; 

    printf("please enter a number : \n"); 
    scanf("%i",&a); 

    for (i=0; i<6 && a>=list[i].value; i++) ; 
    printf ("%s\n", list[i].description); 

    return 0; 
} 
+0

如果您一步一步解釋它,我將不勝感激 – Salahuddin

+1

'for'循環是最重要的 - 使用調試器,或通過筆和紙張進行處理。那麼,以及在你的描述列表中一致*處理相等運算符。 – usr2564301

1

這可能來得有些太晚了,但:

switch(option(a)){ 
    case (0): ... 
    case (1): ... 
    case (2): ... 
    case (n): ... 

其中,option()函數是簡單地用的if else功能。 它可以讓你保持開關的清晰外觀和邏輯部分在別處。