实现一个将数字转换为文本的源代码(c++)

Implement a source code that turns numbers into text (C++)

本文关键字:文本 源代码 c++ 转换 一个 实现 数字      更新时间:2023-10-16

我刚开始学习c++一段时间。我正在读《跳到c++》。这个问题属于第七章,问题1:

实现一个将数字转换为文本的源代码。

这是我到目前为止所做的:

#include <iostream>
#include <string>
using namespace std;
int LessThen20 (int i);
int main () {
    int i = 1;
    cout << "please input a number: n";
    cin >> i;
    if (i < 20) {
        cout << LessThen20(i);
    }
    if ( i >= 20 && i < 30) {
        cout <<"Twenty " ??
    }
}
int LessThen20 (int i) {
    if (i == 0) {
        cout << "zero" <<endl;
    }
    if ( i == 1) {
        cout << "one"; <<endl;
    }
    if (i == 2) {
        cout << "two"; <<endl;
    }
    if ( i == 3) {
        cout << "three"; <<endl;
    }
    if (i == 4) {
        cout << "four"; <<endl;
    }
    if ( i == 5) {
        cout << "five"; <<endl;
    }
    if (i == 6) {
        cout << "six"; <<endl;
    }
    if ( i == 7) {
        cout << "seven"; <<endl;
    }
    if (i == 8) {
        cout << "eight" <<endl;
    }
    if ( i == 9) {
        cout << "nine"; <<endl;
    }
    if (i == 10) {
        cout << "ten"; <<endl;
    }
    if ( i == 11) {
        cout << "eleven"; <<endl;
    }
    if (i == 12) {
        cout << "twelve"; <<endl;
    }
    if ( i == 13) {
        cout << "thirteen"; <<endl;
    }
    if (i == 14) {
        cout << "fourteen"; <<endl;
    }
    if ( i == 15) {
        cout << "fifteen"; <<endl;
    }
    if (i == 16) {
        cout << "sixteen"; <<endl;
    }
    if ( i == 17) {
        cout << "seventeen"; <<endl;
    }
    if (i == 18) {
        cout << "eighteen"; <<endl;
    }
    if ( i == 19) {
        cout << "nineteen"; <<endl;
    }
}

只要输入的数字小于20,我的程序就可以工作。但是我不知道怎么把数字25变成"25"。

任何帮助将不胜感激!

小于20的都是特例。然而,你应该有一个包含"zero", "one", "two",等的数据结构,而不是一个巨大的if链,并索引到数据结构中。(例如向量、映射或数组。如果你不知道如何使用这些,我建议你学习,因为数据结构在所有编程语言中都非常有用,你会想要学习它们。

超过20,我们必须开始更一般地编码。我们必须将创建单词版本分成几个部分:

1)获取单位列。你可以用数字% 10只得到单位,因为% 10除以10后得到余数。你可以使用单位号来索引前面的0、1、2等数据结构,并得到要打印的内容。

2)得到十位。类似的想法-(数字/10)% 10。现在索引十位列变成一个数据结构,比如","十","二十","三十",…

3)…

如果第二个数字大于2,则只需按照与"twenty"相同的方式进行连接,但要与thirty, forty等进行连接。

同样的方法可以应用于百位、千位等。

最好将"前缀"存储在某种可以直接索引的数组中(例如,myTensArray[3]给出"thirty")。

有许多解决方案,但因为我认为你想自己实现这里的一些想法(仅针对整数类型):首先,我将确定存储字符串所需的字符数组的长度。位数可由

决定。
int digits = ((int)log10(number))+1; //+1 if number is negative

接下来你想把你的数字分成几位数

for(int i = digits - 1; number; i--){  //until number is 0
//charFromDidgit(...) returns the char for a nuber between 0 and 9 (e.g. '3' for 3) 
yourCharArray[i] = charFromDigit(number%10);
number /= 10;
}

不要忘记在数组末尾加上结束的0

如果您想要一种仅对基本概念执行此操作的方法,您可以简单地使用循环来检查数字是否超过某个值,然后根据该数字的大小从该数字中减去该值。在每次循环运行之后,使用一个变量来保存从数字中减去的次数。

例如:

//Your number is greater than 1 billion
while ( x > 1000000000 ) { 
billions += 1 
x -= 1000000000 
}
//Your number is greater than 1 million
while ( x > 1000000 ) { 
millions += 1 
x -= 1000000
}
//Your number is greater than 1 thousand
while ( x > 1000 ) { 
thousands += 1 
x -= 1000
}

以此类推。这样你就把它变成了一个小得多的问题,因为你现在只需要处理三位数(你可以使用上面完全相同的步骤,只是处理十位和百位)。你也可以使用模数运算符。虽然更难实现,但效率更高。