使用switch语句将两位数字转换为单词

时间:2013-05-22 13:05:59

标签: c switch-statement

问题是写一个程序要求用户输入2位数字,然后打印出英文单词。假设你输入'41',printf函数打印出'四十一'。我几乎完成了程序,但坚持将11-19位数转换为相应的单词。我用一个switch语句打印第一个数字和第二个switch语句来打印第二个数字。看看代码:

#include <stdio.h>

int main(void)
{
    int first_digit, second_digit;

    printf("Enter two digits: ");
    scanf("%1d%1d",&first_digit,&second_digit);

    switch(first_digit % 10) {
    case 1: printf("ten"); 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(second_digit % 10) {
    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;
    }
    return 0;
}

5 个答案:

答案 0 :(得分:2)

#include <stdio.h>

int main(void)
{
    int num;

    printf("Enter a two digit number: ");
    scanf("%d",&num);
    if(num>10 && num<20){
    badnums(num);
    }
    else{
    goodnums(num);
    }
    return 0;
}

void goodnums(int num){
switch(num % 10) {
        case 0: break;
        case 1: printf("ten"); 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(num / 10) {
        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;
    }
}

void badnums(int num){
//handle the numbers here 11-19
}

答案 1 :(得分:2)

快速和肮脏

#include <stdio.h>

int main(void)
{
    int first_digit, second_digit;

    printf("Enter two digits: ");
    scanf("%1d%1d",&first_digit,&second_digit);

    if (first_digit == 1) {
        switch(second_digit % 10) {
            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(" ninteen"); break;
        }
        return 0;
    }
    switch(first_digit % 10) {
        case 1: printf("ten"); 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(second_digit % 10) {
        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;
    }
    return 0;
}

答案 2 :(得分:0)

试试这个:

[0] [ishpeck@yoshimitsu digit]$ cat main.c 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void teens(int number) 
{
    switch(number) 
    {
        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;
        default: printf("ten");
    }
}

void donums(int number) 
{
    switch(number/10) 
    {
        case 1: return teens(number);
        case 2: printf("twenty"); break;
        case 3: printf("thirt"); break;
        case 4: printf("fourt"); break;
        case 5: printf("fift"); break;
        case 6: printf("sixt"); break;
        case 7: printf("sevent"); break;
        case 8: printf("eighty"); break;
        case 9: printf("ninety"); break;
    }
    switch(number%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;
    }
}

int main(void)
{
    int number;
    printf("Enter two digit number.\n");
    scanf("%d", &number);
    donums(number);
    printf("\n");
    return 0;
}
[0] [ishpeck@yoshimitsu digit]$ gcc --version
gcc (GCC) 4.7.1
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[0] [ishpeck@yoshimitsu digit]$ gcc -o digits main.c
[0] [ishpeck@yoshimitsu digit]$ for x in {10..99}; do echo $x | ./digits; done
Enter two digit number.
ten
Enter two digit number.
eleven
Enter two digit number.
twelve
Enter two digit number.
thirteen
Enter two digit number.
fourteen
Enter two digit number.
fifteen
Enter two digit number.
sixteen
Enter two digit number.
seventeen
Enter two digit number.
eighteen
Enter two digit number.
nineteen
Enter two digit number.
twenty
Enter two digit number.
twenty one
Enter two digit number.
twenty two
Enter two digit number.
twenty three
Enter two digit number.
twenty four
Enter two digit number.
twenty five
Enter two digit number.
twenty six
Enter two digit number.
twenty seven
Enter two digit number.
twenty eight
Enter two digit number.
twenty nine
Enter two digit number.
thirt
Enter two digit number.
thirt one
Enter two digit number.
thirt two
Enter two digit number.
thirt three
Enter two digit number.
thirt four
Enter two digit number.
thirt five
Enter two digit number.
thirt six
Enter two digit number.
thirt seven
Enter two digit number.
thirt eight
Enter two digit number.
thirt nine
Enter two digit number.
fourt
Enter two digit number.
fourt one
Enter two digit number.
fourt two
Enter two digit number.
fourt three
Enter two digit number.
fourt four
Enter two digit number.
fourt five
Enter two digit number.
fourt six
Enter two digit number.
fourt seven
Enter two digit number.
fourt eight
Enter two digit number.
fourt nine
Enter two digit number.
fift
Enter two digit number.
fift one
Enter two digit number.
fift two
Enter two digit number.
fift three
Enter two digit number.
fift four
Enter two digit number.
fift five
Enter two digit number.
fift six
Enter two digit number.
fift seven
Enter two digit number.
fift eight
Enter two digit number.
fift nine
Enter two digit number.
sixt
Enter two digit number.
sixt one
Enter two digit number.
sixt two
Enter two digit number.
sixt three
Enter two digit number.
sixt four
Enter two digit number.
sixt five
Enter two digit number.
sixt six
Enter two digit number.
sixt seven
Enter two digit number.
sixt eight
Enter two digit number.
sixt nine
Enter two digit number.
sevent
Enter two digit number.
sevent one
Enter two digit number.
sevent two
Enter two digit number.
sevent three
Enter two digit number.
sevent four
Enter two digit number.
sevent five
Enter two digit number.
sevent six
Enter two digit number.
sevent seven
Enter two digit number.
sevent eight
Enter two digit number.
sevent nine
Enter two digit number.
eighty
Enter two digit number.
eighty one
Enter two digit number.
eighty two
Enter two digit number.
eighty three
Enter two digit number.
eighty four
Enter two digit number.
eighty five
Enter two digit number.
eighty six
Enter two digit number.
eighty seven
Enter two digit number.
eighty eight
Enter two digit number.
eighty nine
Enter two digit number.
ninety
Enter two digit number.
ninety one
Enter two digit number.
ninety two
Enter two digit number.
ninety three
Enter two digit number.
ninety four
Enter two digit number.
ninety five
Enter two digit number.
ninety six
Enter two digit number.
ninety seven
Enter two digit number.
ninety eight
Enter two digit number.
ninety nine

答案 3 :(得分:0)

int main(void)
{
    int first_digit, second_digit;

    printf("Enter two digits: ");
    scanf("%1d%1d",&first_digit,&second_digit);


    switch(first_digit % 10) {
        case 1: {
                 switch(second_digit % 10) {
            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(" ninteen"); break;
        }
        return 0;
        }
        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(second_digit % 10) {
        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;
    }
    return 0;
}

答案 4 :(得分:0)

@ user2409559,@ Dinever; 首先,你的代码中不需要%,因为你把i / p作为第一和第二位数。 其次你的代码不完整。输入数字b / w 10到19并查看o / p。

我在下面发布我的代码.......看一看。

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

int main()
{
int num,n,r;

printf("Enter a two-digit number: ");
scanf("%d",&num);

n = num/10;
r = num%10;

switch(n)
{ 
    case 1: switch(r)
            {
                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;                                    
    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;
}
if(n != 1)
{
     switch(r)
     {
         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: ;                         

     }
}
getch();

}