2016-03-02 75 views
-1

我已經寫了一個程序,要求用戶輸入一個兩位數的號碼,並打印出英文單詞的號碼。 例子:程序打印一個號碼的英文單詞

Enter a two digit number: 45 
You entered fourty-five 

基本上,我所做的是我已經把switch案例數爲10〜19,然後用於十位和另一個用於一個人的另一臺交換機的情況。

的問題是,輸入號碼,出於某種原因之後,在沒有打印You entered the number:

這裏經過任何顯示的代碼:

#include <stdio.h> 

int main(void) { 
    int Num, Tens, Ones; 

    printf("Enter a Two Digit Number: "); 
    scanf("%d", &Num); 

    printf("You entered the number: "); 

    if (10 <= Num && Num >= 19) { 
     switch (Num) { 
      case 10: printf("Ten\n");  break; 
      case 11: printf("Eleven\n");  break; 
      case 12: printf("Twelve\n");  break; 
      case 13: printf("Thirteen\n"); break; 
      case 14: printf("Fourteen\n"); break; 
      case 15: printf("Fifteen\n"); break; 
      case 16: printf("Sixteen\n"); break; 
      case 17: printf("Seventeen\n"); break; 
      case 18: printf("Eighteen\n"); break; 
      case 19: printf("Nineteen\n"); break; 
     } 
    } 
    if (20 <= Num && Num >= 99) { 
     Tens = Num/10; 
     switch (Tens) { 
      case 2: printf("Twenty"); break; 
      case 3: printf("Thirty"); break; 
      case 4: printf("Fourty"); break; 
      case 5: printf("Fifty"); break; 
      case 6: printf("Sixty"); break; 
      case 7: printf("Seventy"); break; 
      case 8: printf("Eighty"); break; 
      case 9: printf("Ninety"); break; 
     } 
    } 
    Ones = Num % 10; 
    if (Ones == 0) 
     printf("\n"); 
    else 
    if (1 <= Ones && Ones >= 9) { 
     printf("-"); 
     switch (Ones) { 
      case 1: printf("One");  break; 
      case 2: printf("Two");  break; 
      case 3: printf("Three"); break; 
      case 4: printf("Four");  break; 
      case 5: printf("Five");  break; 
      case 6: printf("Six");  break; 
      case 7: printf("Seven"); break; 
      case 8: printf("Eight"); break; 
      case 9: printf("Nine");  break; 
     } 
     printf("\n"); 
    } 
    return 0; 
} 
+5

所有情況都不對......累積獎金! – LPs

+0

'(10 <= Num && Num> = 19)'你認爲這可能永遠是真的嗎?你可以通過一些基本的printf調試發現自己。 –

+0

你不處理'0'。 – chqrlie

回答

3

你的條件是錯誤的,將永遠是假的在正常執行中。

嘗試這些修改:

  • 10 <= Num && Num >= 19 - >10 <= Num && Num <= 19
  • 20 <= Num && Num >= 99 - >20 <= Num && Num <= 99
  • 1 <= Ones && Ones >= 9 - >1 <= Ones && Ones <= 9
+0

...和'10 <= Num' -->'Num> = 10' – LPs

+0

@LP它只會釋放或增加可讀性。 – MikeCAT

+1

肯定....;) – LPs

0

你所有的條件是不正確的。作爲事實上,你可以簡化代碼,併除去大部分的測試:

#include <stdio.h> 

int main(void) { 
    int Num, Tens, Ones; 

    printf("Enter a Two Digit Number: "); 
    scanf("%d", &Num); 

    printf("You entered the number: "); 

    switch (Num) { 
     case 0: printf("Zero\n");  break; 
     case 10: printf("Ten\n");  break; 
     case 11: printf("Eleven\n");  break; 
     case 12: printf("Twelve\n");  break; 
     case 13: printf("Thirteen\n"); break; 
     case 14: printf("Fourteen\n"); break; 
     case 15: printf("Fifteen\n"); break; 
     case 16: printf("Sixteen\n"); break; 
     case 17: printf("Seventeen\n"); break; 
     case 18: printf("Eighteen\n"); break; 
     case 19: printf("Nineteen\n"); break; 
    } 

    Tens = Num/10; 
    switch (Tens) { 
     case 2: printf("Twenty"); break; 
     case 3: printf("Thirty"); break; 
     case 4: printf("Fourty"); break; 
     case 5: printf("Fifty"); break; 
     case 6: printf("Sixty"); break; 
     case 7: printf("Seventy"); break; 
     case 8: printf("Eighty"); break; 
     case 9: printf("Ninety"); break; 
    } 

    Ones = Num % 10; 

    if (Ones > 0) { 
     if (Tenths > 2) 
      printf("-"); 
     switch (Ones) { 
      case 1: printf("One");  break; 
      case 2: printf("Two");  break; 
      case 3: printf("Three"); break; 
      case 4: printf("Four");  break; 
      case 5: printf("Five");  break; 
      case 6: printf("Six");  break; 
      case 7: printf("Seven"); break; 
      case 8: printf("Eight"); break; 
      case 9: printf("Nine");  break; 
     } 
    } 
    printf("\n"); 
    return 0; 
} 
2

除了與運營商的問題,可以考慮用查找表替換switch,出於性能的緣故:

#include <stdio.h> 

void englishize (int n) 
{ 
    static const char* const TEXTUAL_0_9 [] = 
    { 
    "zero", 
    "one", 
    "two", 
    "three", 
    "four", 
    "five", 
    "six", 
    "seven", 
    "eight", 
    "nine", 
    }; 

    static const char* const TEXTUAL_10_19 [] = 
    { 
    "ten", 
    "eleven", 
    "twelve", 
    "thirteen", 
    "fourteen", 
    "fifteen", 
    "sixteen", 
    "seventeen", 
    "eighteen", 
    "nineteen", 
    }; 

    static const char* const TENS [] = 
    { 
    "zero", 
    "ten", 
    "twenty", 
    "thirty", 
    "forty", 
    "fifty", 
    "sixty", 
    "seventy", 
    "eighty", 
    "ninety" 
    }; 

    if((n % 10) == 0) // divisible by ten 
    { 
    printf("%s\n", TENS[n/10]); 
    return ; 
    } 
    else if(n >= 20) // all numbers from 20 and above behave logically 
    { 
    printf("%s-%s\n", TENS[n/10], TEXTUAL_0_9[n%10]); 
    } 
    else if(n >= 10) // special case for numbers between 10-19 
    { 
    printf("%s\n", TEXTUAL_10_19[n-10]); 
    } 
    else // n < 10 
    { 
    printf("%s\n", TEXTUAL_0_9[n]); 
    } 
} 

int main() 
{ 
    for(int i=0; i<100; i++) 
    { 
    englishize(i); 
    } 

    return 0; 
} 
+0

你可以簡單地用'0'到'19'的單個數組。 – chqrlie

+0

@chqrlie我認爲,但與單獨的數組,因爲0到9之一用於兩個不同的目的,所以它看起來更清潔,讓他們分開。有可能更有效的方法來實現這個順便說一句,我只是把一些東西拼湊在一起,以替代潛在的「開關」。 – Lundin