用于打印数字

时间:2016-03-02 14:24:18

标签: c visual-studio-2015

我写了一个程序,要求用户输入一个两位数的数字,然后打印出该数字的英文单词。 例如:

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

基本上,我所做的是我为10到19号码设置了switch个案例,然后为Ten's使用了另一个开关案例,而为One's用了另一个案例。

问题是在输入数字后,由于某种原因它在打印后没有显示任何内容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;
}

3 个答案:

答案 0 :(得分:3)

您的条件错误,在正常执行中始终为假。

尝试这些修改:

  • 10 <= Num && Num >= 19 - &gt; 10 <= Num && Num <= 19
  • 20 <= Num && Num >= 99 - &gt; 20 <= Num && Num <= 99
  • 1 <= Ones && Ones >= 9 - &gt; 1 <= Ones && Ones <= 9

答案 1 :(得分: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; 
}

答案 2 :(得分: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;
}