繁体   English   中英

c中有没有可能把这个if语句变成switch语句?

[英]Is it possible to turn this one if statement into a switch statement in c?

我的任务是创建一个程序,该程序将要求输入 integer,并使用 if-else 语句将输入的 integer 转换为相应的英语单词格式。 现在,我必须改用 switch 来修改它。 这是我到目前为止的进展。

#include<stdio.h>
#include<conio.h>

main()
{
  int num,thousands,hundreds,tens,ones;
  printf("Enter number (1-9999): ");
  scanf("%d",&num);
  if (num < 1 || num > 9999)
     printf("Invalid number.");
  else
  {
  thousands = num / 1000;
  hundreds = num % 1000 / 100;
  tens = num % 1000 % 100 / 10;
  ones = num % 1000 % 100 % 10;
  
    switch(thousands) {
    case 1: printf("one thousand"); break;
    case 2: printf("two thousand"); break;
    case 3: printf("three thousand"); break;
    case 4: printf("four thousand"); break;
    case 5: printf("five thousand"); break;
    case 6: printf("six thousand"); break;
    case 7: printf("seven thousand"); break;
    case 8: printf("eight thousand"); break;
    case 9: printf("nine thousand"); break;
    }

    switch(hundreds) {
    case 0: break;
    case 1: printf(" one hundred"); break;
    case 2: printf(" two hundred"); break;
    case 3: printf(" three hundred"); break;
    case 4: printf(" four hundred"); break;
    case 5: printf(" five hundred"); break;
    case 6: printf(" six hundred"); break;
    case 7: printf(" seven hundred"); break;
    case 8: printf(" eight hundred"); break;
    case 9: printf(" nine hundred"); break;
    }

    switch(tens) {
    {
    case 1: 
    {
        switch(ones) {
            case 0: printf(" ten");break;
            case 1: printf(" eleven"); break;
            case 2: printf(" twelve"); break;
            case 3: printf(" thirteen"); break;
            case 4: printf(" fourteen"); break;
            case 5: printf(" fifteen"); break;
            case 6: printf(" sixteen"); break;
            case 7: printf(" seventeen"); break;
            case 8: printf(" eighteen"); break;
            case 9: printf(" nineteen"); break;
        }
        break;
    }
    break;
    }
        
    case 2: printf(" twenty"); break;
    case 3: printf(" thirty"); break;
    case 4: printf(" forty"); 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;

    }

    if (tens != 1)
    {   
        switch(ones) {
            case 0: break;
            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;
        }

    }

}
getch();
}

它工作正常,但是否可以将一个 if 语句 (if (tens?= 1)) 改为 switch 语句,非常感谢任何反馈,谢谢!

很简单,真的:

/* Finished thousands and hundreds */

switch(tens) {
    case 1: ones = ones + 10; break;
    case 2: printf(" twenty"); break;
    case 3: printf(" thirty"); break;
    case 4: printf(" forty"); 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;
}

switch(ones) {
    case  0: break;
    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;
    case 10: printf(" ten");break;
    case 11: printf(" eleven"); break;
    case 12: printf(" twelve"); break;
    case 13: printf(" thirteen"); break;
    case 14: printf(" fourteen"); break;
    case 15: printf(" fifteen"); break;
    case 16: printf(" sixteen"); break;
    case 17: printf(" seventeen"); break;
    case 18: printf(" eighteen"); break;
    case 19: printf(" nineteen"); break;
}

有时候,不要想太多,也不要试图操纵每一条微小的逻辑途径,这是值得的。 停下来想一想——认真地想一想这个任务。


编辑
也许,看着这段代码,您发现了将“打印个数”移动到它自己的 function 中的想法。然后,用千值调用它并打印单词“千”,然后用百值调用它并打印单词“百”...学习使用功能能够重复使用代码。 非常宝贵的一课!!

PS:如果您遵循此建议,您可以将可接受的输入范围扩大到打印,甚至是“16942”(16942),甚至超过 99,999。 (而且,即使在上面,如果你注意到当用分隔符书写时,数字作为单词的“三元组”是如何出现在逗号之间的……)

是的。 你可以这样做。 将最终的if作为switch实现可能如下所示:

switch(tens)
    {   
        case 1:
            break;
        default:
            switch(ones) {
                case 0: break;
                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;
            }
        break;
    }

switch default 就像 if 的 else 语句; 如果没有其他条件为真,其中的代码就会运行。

你可以试试

    switch(tens)
    {
        case 2 ... 9:
        {
            switch(ones) {
                case 0: break;
                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;
            }
            break;
        }

        case 1:
        default:
            break;

    }

其中2... 9表示如果tens位在2 到 9范围内,包括 9 和 2,因为tens位始终是 0 到 9 之间的数字

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM