3

请善待我是一个完全的新手。

我有一个从 num1 到 num90 的变量(字符串)列表。我需要通过将 int 中的数字添加到单词 num 来从函数中调用它们。

任务是将数字从“跳转到 C++”中转换为单词......我可能不会以“正确”的方式进行,但这部分已经阻止了我一段时间!

我正在尝试这样:

    #include <iostream>
    #include <string>

    using namespace std;

    // Hard code numbers to 20 and then in tens to 90
    string num1 = "one";
    string num2 = "two";
    string num3 = "three";
    string num4 = "four";
    string num5 = "five";
    string num6 = "six";
    string num7 = "seven";
    string num8 = "eight";
    string num9 = "nine";

等等...高达 90

    int main ()
    {

    // Break the number down into three digit blocks
    int num = 0;

    cout << "Please enter the number: ";
    cin >> num;


  while (num > 999)
    {
int digit = (num % 1000);
num = ((num - digit)/1000);
//cout << digit << '\n';
//cout << num << '\n';

// For each block of numbers work out hundreds,


if (digit > 100)
{
    int i = digit;
    int j = (i % 100);
    cout << num.append(j) << " hundred";
}

我需要将存储在“j”中的数字标记到单词 num 上,以便调用字符串 num*。

这可能吗?

4

3 回答 3

3

当然,使用地图:

#include <map>
#include <string>
#include <iostream>

std::map<int, std::string> words { { 1, "one" }, { 2, "two" }, { 3, "three" } };

int main()
{
    std::cout << words[1] << std::endl;
}

您可能必须处理一些特殊情况(最多二十个?),并且您需要一个“一百”等词。如果您想使其国际化,您将不得不更加努力地思考。

于 2012-12-02T11:31:47.747 回答
2

我需要将存储在“j”中的数字标记到单词 num 上,以便调用字符串 num*。

这种方法的问题是你在代码中使用的任何变量名都没有被编译:当程序运行时,它会操纵你的变量的值,但它不知道也不关心你决定使用名称“num3”而不是“numero3”或“foobar”。

如果您想在数字(3)和字符串(“三”)之间建立链接,那么您可以使用 Vector(正如@Mark建议的那样,尽管在 20 之后您会遇到问题)或者更好的是 Map(如@ Kerrek建议):这些将起作用,因为在这两种情况下,字符串都是由变量的(例如digit in的值lookup[digit]或文字值1 in words[1])而不是变量的名称引用的。


编辑:

出于兴趣,这里有一个使用'if'和'switch'的版本......

#include <iostream>
#include <string>
using namespace std;

string units2word(int units){
    switch (units){
        case 0: return "zero";
        case 1: return "one";
        case 2: return "two";
        case 3: return "three";
        // etc ...
        case 18: return "eighteen";
        case 19: return "nineteen";
    }
}

string tens2word(int tens){
    switch(tens){
        case 2: return "twenty";
        case 3: return "thirty";
        // etc ...
        case 9: return "ninety";
    }
}

string num2words(int num) {
    if (num > 99 && num%100 == 0) return units2word(num/100) + " hundred";
    if (num > 99) return units2word(num/100) + " hundred and " + num2words(num%100);
    if (num < 20) return units2word(num);
    if (num%10 == 0) return tens2word(num/10);
    return tens2word(num/10) +"-"+ units2word(num%10);
}

int main(int argc, char *argv[]) {
    int num = -1;
    while( num < 0 || num > 999){
        cout << "Please enter a number between 0 and 999: ";
        cin >> num;
    }
    cout << "You typed: " << num2words(num) << endl;
}
于 2012-12-02T11:48:42.897 回答
1

你应该看看使用 std::vector。这为您提供了一个带有索引的变量

std::vector<std::string> lookup;
lookup.push_back( "zero" );   // starts at lookup[0]

lookup.push_back( "one" );
lookup.push_back( "two" );
// etc

// then

std::cout << lookup[digit] << std::endl;
std::cout << lookup[num] << std::endl;
于 2012-12-02T11:30:36.763 回答