0

问题是要编写一个程序,要求用户输入 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;
}
4

5 回答 5

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;
}
于 2013-05-22T13:19:15.550 回答
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
}
于 2013-05-22T13:17:21.747 回答
1

@user2409559,@Dinever;首先,您的代码中不需要 %,因为您将输入作为第一位和第二位数字。其次,您的代码不完整。输入 10 到 19 之间的数字并查看输出。

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

#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();
}        
于 2013-06-08T21:06:55.510 回答
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;
}
于 2013-05-22T13:25:55.113 回答
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
于 2013-05-22T13:18:22.313 回答