使用switch语句转换成两位数字单词 [英] Converting two digit number to words using switch statement

查看:175
本文介绍了使用switch语句转换成两位数字单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说写一个程序,要求用户输入2位数字,然后打印的英文单词吧。假设你输入'41'printf函数打印出41。我几乎完成了计划,但停留在11-19转换成数字及其对应的单词。我使用了一个switch语句来打印的第一位和第二switch语句来打印第二位。看一看在code:

 的#include<&stdio.h中GT;INT主要(无效)
{
    INT first_digit,second_digit;    的printf(请输入两个数字:);
    scanf函数(%升跌%1D,&安培; first_digit,&安培; second_digit);    开关(first_digit%10){
    案例1:输出(十);打破;
    案例2:输出(廿);打破;
    案例3:输出(三十);打破;
    案例4:printf的(四十);打破;
    案例5:printf的(五十);打破;
    案例6:输出(六十条);打破;
    案例7:printf的(七十);打破;
    案例8:printf的(八);打破;
    案例9:输出(90);打破;
    }
    开关(second_digit%10){
    情况下0:打破;
    案例1:输出(1);打破;
    案例2:printf的(二);打破;
    案例3:输出(三);打破;
    案例4:printf的(四大);打破;
    案例5:printf的(五);打破;
    案例6:printf的(六个一);打破;
    案例7:输出(七);打破;
    案例8:输出(八);打破;
    案例9:输出(九);打破;
    }
    返回0;
}


解决方案

快速和肮脏

 的#include<&stdio.h中GT;INT主要(无效)
{
    INT first_digit,second_digit;    的printf(请输入两个数字:);
    scanf函数(%升跌%1D,&安培; first_digit,&安培; second_digit);    如果(first_digit == 1){
        开关(second_digit%10){
            情况下0:输出(十);打破;
            案例1:输出(十一);打破;
            案例2:输出(十二条);打破;
            案例3:输出(十三);打破;
            案例4:printf的(十四);打破;
            案例5:输出(十五);打破;
            案例6:输出(十六条);打破;
            案例7:输出(十七);打破;
            案例8:printf的(十八);打破;
            案例9:输出(ninteen);打破;
        }
        返回0;
    }
    开关(first_digit%10){
        案例1:输出(十);打破;
        案例2:输出(廿);打破;
        案例3:输出(三十);打破;
        案例4:printf的(四十);打破;
        案例5:printf的(五十);打破;
        案例6:输出(六十条);打破;
        案例7:printf的(七十);打破;
        案例8:printf的(八);打破;
        案例9:输出(90);打破;
    }
    开关(second_digit%10){
        情况下0:打破;
        案例1:输出(1);打破;
        案例2:printf的(二);打破;
        案例3:输出(三);打破;
        案例4:printf的(四大);打破;
        案例5:printf的(五);打破;
        案例6:printf的(六个一);打破;
        案例7:输出(七);打破;
        案例8:输出(八);打破;
        案例9:输出(九);打破;
    }
    返回0;
}

最好的问候:)

The question says to write a program asking the user to enter 2 digit number, then prints the English word for it. Suppose you enter '41' the printf function prints out 'forty one'. I have almost completed the program but stuck on converting 11-19 digits into their corresponding words. I used one switch statement to print the first digit and second switch statement to print the second digit. Have a look at the code:

#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;
}

解决方案

Quick and Dirty

#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;
}

Best Regards:)

这篇关于使用switch语句转换成两位数字单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆