本文介绍了写code给定的数字转换成单词(比如1234作为输入输出应该1234)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写C / C ++ / Java的code给定的数字转换成文字。

例如: -
输入:
1234

输出:
千234。

输入:
10

输出:

是否需要为数字0的完整开关的情况下为10。

每个数字名称后加上少年(例如:14:四青少年)。从14至19

和比范围为20加入TY和阿拉伯数字名到99的数字。

等。

我想一定有解决这一一些更好的方法。

C code是preferred。


解决方案

 #包括LT&;&iostream的GT;
使用命名空间std;
无效扩展(INT);
诠释的main()
{
    INT NUM;
    COUT<<输入号码:;
    CIN>> NUM;
    扩大(NUM);
}
无效扩展(int值)
{
    为const char * const的那些[20] = {零,一,二,三,四有,五,六个一,七,
    八,九,十,十一,十二条,十三,十四,十五,十六条,十七,
    十八,古诗十九首};
    为const char * const的数十[10] = {,十,二十,三十,四十,五十,六十条,七十
    八十,90};    如果(值小于0)
    {
        COUT<<减;
        扩大( - 值);
    }
    否则,如果(价值> = 1000)
    {
        扩大(值/ 1000);
        COUT<<千;
        如果(价值1000%)
        {
            如果(值%1000℃; 100)
            {
                COUT<< 与;
            }
            COUT<< ;
            扩大(价值1000%);
        }
    }
    否则,如果(价值> = 100)
    {
        扩大(价值/ 100);
        COUT<<百;
        如果(价值100%)
        {
            COUT<< 与;
            扩大(价值100%);
        }
    }
    否则,如果(价值> = 20)
    {
        COUT<<数十[值/ 10];
        如果(值10%)
        {
            COUT<< ;
            扩大(值10%);
        }
    }
    其他
    {
        COUT<<那些[值];
    }
    返回;
}

Write C/C++/Java code to convert given number into words.

eg:-Input:1234

Output:One thousand two hundred thirty-four.

Input:10

Output:Ten

Does it require a complete switch case for digits 0 to 10.

Adding "teen" after every number name (eg: 14: four "teen".) from 14 to 19.

And than adding "ty" and the digits name for a number in the range 20 to 99.

And so on.

I think there must be some far better approach for solving this.

C code is preferred.

解决方案
#include<iostream>
using namespace std;
void expand(int);
int main()
{
    int num;
    cout<<"Enter a number : ";
    cin>>num;
    expand(num);
}
void expand(int value)
{
    const char * const ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
    "eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
    "eighteen","nineteen"};
    const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
    "eighty","ninety"};

    if(value<0)
    {
        cout<<"minus ";
        expand(-value);
    }
    else if(value>=1000)
    {
        expand(value/1000);
        cout<<" thousand";
        if(value % 1000)
        {
            if(value % 1000 < 100)
            {
                cout << " and";
            }
            cout << " " ;
            expand(value % 1000);
        }
    }
    else if(value >= 100)
    {
        expand(value / 100);
        cout<<" hundred";
        if(value % 100)
        {
            cout << " and ";
            expand (value % 100);
        }
    }
    else if(value >= 20)
    {
        cout << tens[value / 10];
        if(value % 10)
        {
            cout << " ";
            expand(value % 10);
        }
    }
    else
    {
        cout<<ones[value];
    }
    return;
}

这篇关于写code给定的数字转换成单词(比如1234作为输入输出应该1234)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:21