0

嗨,我早些时候发了一个帖子,虽然我认为它很模棱两可,所以我会再试一次,换个说法。

我正在尝试编写一个程序,该程序接受两个英文数字的输入并将它们转换为整数(使用数组),运行计算然后将它们转换回英文单词。

例如:

用户输入:sixty_six + 6 输出:seventy_two

用户输入:40_4 - 4 输出:40

ETC

然而,我一直无法在我的代码中找出问题,花了最后一个小时左右试图找到问题,尽管我无法做到。所以希望这里有人可以帮助我。

我的代码在cout那里,所以它在循环中出错了,它似乎按预期运行,直到第二个 cout 行。意味着它输出the token is fortyandtens is euqal to然后它失败并终止:

nt Wordnum::read_number(string n) {
    int result_ten = 0;
    int result_unit = 0;
    int j = 0;

    //  Loops through input and turns it all to lower case.

    while (n[j]) {
        char c;

        c = n[j];
        c = (tolower(c));
        n[j] = c;
        j++;
    }

    string delimiter = "_";
    size_t pos = 0;
    string token;

    // Checks for the _ delimiter and takes the first token then checks it against the array to "convert" from string to int.
    while ((pos = n.find(delimiter)) != string::npos) {
        token = n.substr(0, pos);

        cout << "Token is: '" << token << "'" << std::endl;

        for (int i = 0; i < 20; i++) {
            if (token == tens[i]) {
                result_ten = i * 10;
                n.erase(0, pos + delimiter.length());
                cout << "tens is equals to: " << result_ten << endl;
            } else if (token == units[i]) {
                result_unit = i;
                n.erase(0, pos);
            }
        }

        result_ten = (result_ten + result_unit);

        cout << "ten is equals to: " << result_ten << endl;
    }

    return result_ten;
}

正在使用的数组:

string const Wordnum::units[] = {"zero", "one", "two", "three", "four", "five",
    "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
    "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

string const Wordnum::tens[] = {"zero", " ", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

提前致谢。如果有什么不清楚的地方,我会尝试更好地解释它。

4

3 回答 3

3

您尝试索引tens[i],而我从 [0,20) 开始。但是,十个中没有 20 个索引。

于 2013-09-10T05:39:20.183 回答
2

您的错误在您的for-loop中:

for (int i = 0; i < 20; i++) {
    //              ^^
    if (token == tens[i]) {
    //           ^^^^^^^
    // ...
}

i019包括在内。而你试图索引tens[i]. 但:

string const Wordnum::tens[] = {"zero", " ", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

这条线向我们展示了tens只有10元素。这里有著名的越界错误。

您可以通过以下方式解决此问题:

for (int i = 0; i < 20; i++) {
    if ( i < 10 && token == tens[i]) {
    //   ^^^^^^^^^^
    // ...
}

我不确定你的算法会给出预期的结果,但至少你的程序不会崩溃。

于 2013-09-10T06:22:42.647 回答
1

int Wordnum::read_number(string n) {

int result_ten = 0;
int result_unit = 0;
int j = 0, f3 =0;
//  Loops through input and turns it all to lower case.
while (n[j]) {
    char c;

    c = n[j];
    c = (tolower(c));
    n[j] = c;
    j++;
}
string delimiter = "_";
size_t pos = 0, pos1 = 0;
string token;
// Checks for the _ delimiter and takes the first token then checks it against the    array to "convert" from string to int.

if((pos = n.find(delimiter)) != string::npos) {
    token = n.substr(0, pos);
}else
{
    token = n.substr(0, n.length());
    f3 = 1;
}

for (int i = 0; i < 20; i++) {
    if (token == tens[i]) {
       result_ten = i * 10;
       i=0;
       if(f3 == 1)
          break;
       token = n.substr(pos+1,(n.length()-pos-1) );
     } else if (token == units[i]) {
            result_unit = i;
            break;
     }
 }## End of loop for converting the string to int ##
 result_ten = (result_ten + result_unit);
 cout << "value is : " << result_ten << endl;
 return result_ten;

}

看看这个,希望对你有帮助

于 2013-09-10T07:14:35.377 回答