拼出整数数字的程序:(在 switch 语句中)与 case 值不匹配,为什么?

问题描述 投票:0回答:4

摘自《C 语言编程》一书

编写一个程序,获取从终端输入的整数,并提取该整数的每个数字并用英语显示。因此,如果用户输入 932,程序应该显示

nine three two

如果用户仅输入

0
,请记住显示“零”。

已经几个小时了,仍然无法解决..有人知道如何解决吗?这是到目前为止的代码

#include <stdio.h>

int right_digit,number;

int main ()
{    
    scanf("%i",&number);

    right_digit = number % 10;
    
    switch (right_digit)
    {
        case '0':
            printf("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;
        default:
            break;
    }
    
    number = number / 10;
    
    return 0;
}
c char switch-statement
4个回答
3
投票

这里的第一个问题是,您(错误地)尝试使用整数的字符表示。在您的代码中,

right_digit
应该表示 integer 数字,而不是字符文字。

您不能使用

''
,只需写

 case 0:

 ... 

 case 1:

等等。

只是补充一点你的错误,它正在考虑字符文字

'0'
'1'
等的相应整数值。 对于 ASCII,它们相当于

 case 48:
 case 49:
 .
 .

这不是你想要的。

也就是说,

  • 您需要将模计算和 switch-case 放入循环中,并对输入整数的所有位数进行转换。

  • 您需要从头开始打印(MSB),目前,您从LSB开始打印。 (提示:开始打印模运算的结果

  • 根据要求,
  • printf("0");
    应为
    printf("Zero ");


0
投票
  /*USING SWITCH CASE ...ALSO YOU CAN USE '0' and negative numbers */
    #include <stdio.h>
    #include <stdlib.h>

    int main (void)
    {
        int rem,num,sum=0,rem1,num1,add;
        printf("enter the number:\n");
        scanf("%i",&num);
        if(num<0)
        {
            printf("minus ");
            num=-num;
        }
        if(num==0)
        {
            printf("zero");
        }
        while(num!=0)
        {
           rem=num%10;
           num=num/10;
           sum=sum*10 +rem;
        }
        /*printf("%i\n",sum);*/

        while(sum!=0)
        {
            rem1=sum%10;
            sum=sum/10;
            switch(rem1)
            {
            case 0:
                printf("zero ");
                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;
            default:
                printf("invalid no");
            }
        }
       return 0;
    }

0
投票

我希望这可以解决从左到右读取数字的问题,以及为什么开关循环不起作用。

#include <stdio.h>

//Program to type numbers into English

int main(void)
{
    int n,d;

printf("\n\nGo ahead and type numbers:\n");
scanf("%i",&n);

while(n != 0)
{   
    
    d = (n / 100) % 100;

    
        switch(d)
        {
            case 0:
                printf("Zero ");
                break;

            case 1:
                printf("One ");
                break;

            case 2:
                printf("Two ");
                break;

            case 3: 
                printf("Three ");
                break;

            default:
                printf("%i   ",d);
                break;
        }

    n -= d*100;
    
        d = (n / 10) % 10;

    
        switch(d)
        {
            case 0:
                printf("Zero ");
                break;

            case 1:
                printf("One ");
                break;

            case 2:
                printf("Two ");
                break;

            case 3: 
                printf("Three ");
                break;

            default:
                printf("%i   ",d);
                break;
        }

    n -= d*10;

    d = n % 10;

    
        switch(d)
        {
            case 0:
                printf("Zero ");
                break;

            case 1:
                printf("One ");
                break;

            case 2:
                printf("Two ");
                break;

            case 3: 
                printf("Three ");
                break;

            default:
                printf("%i   ",d);
                break;
        }

    n /= 10;
    }
}

这里

case 0:
case 1:
没有
''
,因为它们不是字符,而是数字。这里的输出将是:

User input:
321

Output:
三二一

现在,

d = (n / 100) % 10;

获取左边的数字。

d = (321 / 100) % 10;
d = 3 % 10;
d = 3;

现在,

 n -= d * 100;

变成:

n = 321 - (3 * 100);
n = 321 - 300;
n = 21;

新号码是21! 这里:

d = (n / 10) % 10;
d = (21 / 10) % 10;
d = 2 % 10;
d = 2;

那么,

n -= d * 10;

变成:

n = 21 - (2 * 10);
n = 21 - 20;
n = 1;

下一行:

d = n % 10;
d = 1 % 10;
d = 1;

最后,

n /= 10;

将 n 设置为 0,取消循环。

当然,您可以为所有数字添加更多案例!


-2
投票

希望这个程序能帮助您理解逻辑,我还发布了使用 switch case 解决相同问题的解决方案....

/* Write a program that takes an integer keyed in from
 * the terminal and extracts and displays each digit of the
 * integer in English. So, if the user types in 932, the
 * program should display    >>> nine three two <<<. 
 * (Remember to display “zero” if the user types in
 * just a 0.)
 */


/*USING IF-ELSE IF*/
    #include <stdio.h>
    #include <stdlib.h>

    int main (void)
    {
        int rem,num,sum=0,rem1;
        printf("enter the number:\n");
        scanf("%i",&num);
        if(num<0)
        {
            printf("minus ");
            num=-num;
        }
        if(num==0)
        {
            printf("zero");
        }
        while(num!=0)
        {
           rem=num%10;
           num=num/10;
           sum=sum*10 +rem;
        }
        /*printf("%i\n",sum);*/

        while(sum!=0)
        {
            rem1=sum%10;
            sum=sum/10;
            if(rem1==0)
           {
             printf("zero ");
           }
            else if(rem1==1)
            {
                printf("one ");
            }
             else if(rem1==2)
            {
                 printf("two ");
            }
             else if(rem1==3)
            {
                printf("three ");
            }
             else if(rem1==4)
            {
                printf("four ");
            }
             else if(rem1==5)
            {
                printf("five ");
            }
             else if(rem1==6)
            {
                printf("six ");
            }
             else if(rem1==7)
            {
                printf("seven ");
            }
             else if(rem1==8)
            {
                printf("eight ");
            }
             else if(rem1==9)
            {
                printf("nine ");
            }
            else
            {
                printf("invalid no");
            }
        }
        return 0;
    }
© www.soinside.com 2019 - 2024. All rights reserved.