2016-06-12 86 views
-1

我得到解決的問題如下: - 寫一個名爲「is_digit」的函數接受一個字符作爲輸入,並返回一個整數值如果參數輸入是0到9之間的ASCII數字,則爲1。否則,該函數必須返回值0.C發出函數返回1當用戶輸入爲0-9時ASCII

- 在主函數中,調用is_digit,並根據用戶輸入傳入參數。然後主函數必須輸出調用is_digit的結果。

這是我迄今爲止

#include <stdio.h> 

     char is_digit(int x) 
     { 
      if (x > '48' && x < '57') //The ascII range between 0-9 (i think...) 
      { 
       return 1; 
      } 
      else      
//Whatever the user input is it always runs the else 
      { 
       return 0;    
//Is this what the question means by "function must return a value of 0" 
      } 
     } 

     int main() 
     { 
      char x; 
      printf("input: "); 
      scanf("%c", &x); 
      is_digit(x); 
     } 

爲什麼我的程序沒有返回1,如果我的用戶輸入是0-9?

更新版本與考慮到的意見:

#include <stdio.h> 

char is_digit(char x) 
{ 
    if (x >= 48 && x <= 57) 
    { 
     printf("1"); 
     return 1; 
    } 
    else 
    { 
     printf("0"); 
     return 0; 
    } 
} 

int main() 
{ 
    char x; 
    printf("input: "); 
    scanf("%c", &x); 
    is_digit(x); 
} 
+0

測試'if(x>'48'&& x <'57')'不正確。刪除單引號,並使用'> ='而不是'>'(也可用'<='而不是'<') – Aziz

+0

你正在比較一個int與一個字符(各種:兩個元素的字符;不確定這是多麼有效)。請使用和*讀取*您的編譯器警告。 – Evert

+0

哎呀,修正了我的if語句,非常感謝!但我的功能仍然沒有返回1 ..... –

回答

0

你的錯誤是

if (x > '48' && x < '57') //The ascII range between 0-9 (i think...) 

應該

if (x > 47 && x < 58) //The ascII range between 0-9 (i think...) 

改正的代碼如下所示

#include <stdio.h> 

      int is_digit(char x) 
      { 
       if (x > 47 && x < 58) //The ascII range between 0-9 (i think...) 
       { 
        return 1; 
       } 
       else      
    //Whatever the user input is it always runs the else 
       { 
        return 0;    
    //Is this what the question means by "function must return a value of 0" 
       } 
      } 

      int main() 
      { 
       char x; 
       printf("input: "); 
       scanf("%c", &x); 
       printf("result is: %d",is_digit(x)); 
       return 0; 

      } 

編程中的返回語句是計算機做什麼的證明。在c和C++中,在main中返回0表示程序正確結束,不會崩潰,但不一定代碼是錯誤或正確的。

+0

感嘆。只需寫出明顯的'if(x> ='0'&& x <='9')',那麼您不必知道或關心ASCII值是什麼,並且不會冒險讓他們錯誤。 –

+0

該任務是打印is_digit的結果。這個程序不這樣做。返回'is_digit(x)'是假的。 –

0

你的返回類型爲char, 試試這個:

int is_digit(int x) 
+0

編寫一個名爲is_digit的函數,它接受一個「字符作爲輸入」,並返回一個整數值1. 作爲輸入要求的字符是什麼有點讓我在這個問題上跳動。如果它是整數輸入,它將是一塊蛋糕。 –

+0

@ F.Horn爲什麼? 'scanf(「%c」,&x)'將一個char讀入'x' - 一塊蛋糕。測試它可以用int is_digit(int x){return x> ='0'&& x <='9'; '' - 另一塊蛋糕。 –

+0

「寫一個名爲」is_digit「的函數接受一個字符作爲輸入」是問題要求的一部分 –

0

總之:因爲你不告訴它來。最後一行應該是:

return is_digit(x);

沒有return,從is_digit()結果只是簡單地扔掉......

+0

非常感謝!我多愚蠢。這解決了這個問題。 它返回和int,但我的函數總是運行else語句?關於爲什麼if語句沒有運行的任何線索? –

+0

詢問計算機對x有什麼價值。而不是'printf(「0」);'use'printf(「%d」,x);'它會打印出x的值而不是簡單的'0' - 然後你可以找出x是什麼。它應該在這個範圍內,但也許你首先鍵入一個空格?或者是?或者是其他東西。如果輸出是32或更少,那麼它是一個空格或鍵。這就是'scanf(「%c」)的問題' - 它掃描下一個字符,不管它是什麼 –

1

試試這個: 另外要注意使用「無符號的字符X」你主要這意味着正值0-255

#include <stdio.h> 

int is_digit(int x) 
{ 
    if (x >= 48 && x <= 57) 
     { 
      return 1; 
     } 
    return 0; 
} 

最好是改變你的方法返回一個int而不是char。

取下單引號,因爲你是比較ASCII字符(INT)

的數值也用「< =」和「> =」的跡象,因爲這個問題說的包容性。 '<'和'>'是排他性的。

你不需要別的塊,因爲如果它是一個數字,答案會立即返回。

「//這是什麼問題意味着」功能必須返回值爲0「?

是的,就是這個意思。

+0

int is_digit(int x) { return x> ='0'&& x <='9'; } –

1

你可以做一個直接的比較:

int is_digit(char x) 
{ 
    int result; 
    if (x >= '0' && x <= '9') 
    { 
     result = 1; 
    } 
    else 
    { 
     result = 0; 
    } 
    return result; 
} 
+1

是的,另一個回答針對整數值的測試是無能的。但是......簡單地說'return x> ='0'&& x <='9;'做的是正確的事情,可能是導師所期待的。 –