【问题标题】:Exercise in C with if and switch statements: Write code to convert 2 digit numbers into words使用 if 和 switch 语句在 C 中练习:编写代码将 2 位数字转换为单词
【发布时间】:2020-09-27 19:46:37
【问题描述】:

我想弄清楚这个练习。作业要求将两位数转换为单词,输出应该是这样的:

Enter a two-digit number:45
You entered the number forty-five.

我仍然是编程的初学者。我在这本 C 编程书籍的这一章,在关于 switch 和 if 语句的练习部分。练习建议使用两个switch 语句,一个用于十位,另一个用于单位,但11 和19 内的数字需要特殊处理。

问题是我试图弄清楚我应该为11 和19 之间的数字做什么,我想使用if 语句但是第二个switch 函数将包含在输出,它会变成类似 You've entered the number eleven one.

这是我目前一直在写的程序(不完整):

    int digits;

    printf("Enter a two-digit number:");

    scanf("%d", &digits);

    printf("You entered the number ");

    switch (digits / 10) {
    case 20:
        printf("twenty-");break;
    case 30:
        printf("thirty-");break;
    case 40:
        printf("forty-");break;
    case 50:
        printf("fifty-");break;
    case 60:
        printf("sixty-");break;
    case 70:
        printf("seventy-");break;
    case 80:
        printf("eighty-");break;
    case 90:
        printf("ninety-");break;
    }

    switch (digits % 10) {
    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;
    }
    return 0;

【问题讨论】:

  • 不确定我是否会担心将 2 位数字转换为整数 - 对 switch(string[0]) { case '0': ..... 来说更容易和更有效。对于常见情况,您需要一个通用的 "do_uints() 函数(例如case '2': printf("twenty"); do_units(string[1]); break; 以便对 00 到 19 的数字使用其他东西很容易(您可能有一个嵌套开关,例如 case '1': switch(string[1]) { case '0': printf("Ten"); break; case '1': printf("Eleven"); ...。

标签: c if-statement switch-statement


【解决方案1】:

在 switch 之前使用这些 if then else 语句。

if (digits == 11) {
    printf("eleven");
}
else if (digits == 12) {
    printf("twelve");
}
else if  (... ) {// similar statements for 13 14 15 16 17 18 19


}
// now figure out where do the switch statements have to be inserted
else {
    
     // here?
}
// or here?

上述解决方案假设您尚未学习数组。如果您已经学习或知道使用数组 - 有一些方法可以改进此代码。

【讨论】:

  • 我还没有学过数组,所以我的选择非常有限。
【解决方案2】:

这对学习者来说是一个很好的问题,不想放弃太多

// Pseudo code
int print_two_digits(int tens, int ones) {
  if (tens 2 or more) 
    print tens_place(tens) // use `tens` to index an array of strings.
    if (ones == 0) return;
    print -
  else 
    ones += tens*10;

  print ones_text(ones) // ones is 0-19 at this point. Index an string array with `ones`
}

如何索引字符串数组并打印?

// index: valid for 0,1,2
void print rgb(int index) {
  const char *primary[] = { "red", "green", "blue" };
  puts(primary[index]);
}

琐事:文本 10-99 中的英文数字大多是大端,最重要的在前,如“forty-two”,除了 [11-19] 的小数在前在“四个青少年”中。

【讨论】:

    【解决方案3】:

    我建议你用 3 个开关而不是 2 个开关编写一个函数:

    • 第一个switch,用于处理0等特殊情况以及10到19之间的数字并直接返回。
    • 第二个 switch 处理从 20 开始的十位并打印没有尾随破折号的单词,
    • if 语句,用于测试是否需要破折号,
    • 第三个switch 用于处理单位。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多