C ++将数字转换为单词

时间:2016-10-26 02:28:47

标签: c++ string int

我在书中遇到了这个程序,将一个数字转换为单词。初始程序转换数字1-1000,但随后要求您修改程序以接受最多1,000,000的数字。我得到它使用数字高达20,999,但不能让它工作过去。我整天都在修补它并在网上查看了很多程序示例,但我看到的每个人都在使用我作为初学者的技术。任何建议将不胜感激。这是我到目前为止所拥有的。

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

string digitName(int digit);
string teenName(int number);
string tensName(int number);
string intName(int number);


int main()
{
    int input;

    do
    {

    cout << "Please enter a positive integer: ";

    cin >> input;

    cout << "\n" << intName(input) << endl;

    cout << "\n\n" << endl;

    }while (input > 0);

    return 0;
}

string digitName(int digit)
{
    if (digit == 1) return "one";
    if (digit == 2) return "two";
    if (digit == 3) return "three";
    if (digit == 4) return "four";
    if (digit == 5) return "five";
    if (digit == 6) return "six";
    if (digit == 7) return "seven";
    if (digit == 8) return "eight";
    if (digit == 9) return "nine";

    return "";
}

string teenName(int number)
{
    if (number == 10) return "ten";
    if (number == 11) return "eleven";
    if (number == 12) return "twelve";
    if (number == 13) return "thirteen";
    if (number == 14) return "fourteen";
    if (number == 15) return "fifteen";
    if (number == 16) return "sixteen";
    if (number == 17) return "seventeen";
    if (number == 18) return "eighteen";
    if (number == 19) return "nineteen";

    return "";
}

string tensName(int number)
{
    if (number >= 90) return "ninety";
    if (number >= 80) return "eighty";
    if (number >= 70) return "seventy";
    if (number >= 60) return "sixty";
    if (number >= 50) return "fifty";
    if (number >= 40) return "fourty";
    if (number >= 30) return "thirty";
    if (number >= 20) return "twenty";

    return "";
}

string intName(int number)
{
    int part = number;
    string name;

    if (part >= 20000)
    {
        if (part % 10000 == 0)
        {
            name = tensName(part / 1000) + " thousand ";
            part = part % 1000;

        }else
        {
            name = tensName(part / 1000) + " ";
            part = part % 10000;
        }
    }

    if (part >= 10000)
    {
        name = teenName(part / 1000) + " thousand ";
        part = part % 1000;
    }

    if (part >= 1000)
    {
        name = digitName(part / 1000) + " thousand ";
        part = part % 1000;
    }

    if (part >= 100)
    {
        name = name + digitName(part / 100) + " hundred";
        part = part % 100;
    }

    if (part >= 20)
    {
        name = name + " " + tensName(part);
        part = part % 10;

    }else if (part >= 10)
    {
        name = name + " " + teenName(part);
        part = 0;
    }

    if (part > 0)
    {
        name = name + " " + digitName(part);
    }

    return name;
}

1 个答案:

答案 0 :(得分:5)

两件事:如果只是索引到一个数组,你可以节省很多钱。通过在数百个案例之后重复实现它,您可以更加一般地解决问题。数千,数百万,数十亿,数万亿和数万亿都遵循完全相同的模式,因此可以递归地完成。

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

    string digitName(int digit);
    string teenName(int number);
    string tensName(int number);
    string intName(int number);

    vector<string> ones {"","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    vector<string> teens {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen","sixteen", "seventeen", "eighteen", "nineteen"};
    vector<string> tens {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

    string nameForNumber (long number) {
        if (number < 10) {
            return ones[number];
        } else if (number < 20) {
            return teens [number - 10];
        } else if (number < 100) {
            return tens[number / 10] + ((number % 10 != 0) ? " " + nameForNumber(number % 10) : "");
        } else if (number < 1000) {
            return nameForNumber(number / 100) + " hundred" + ((number % 100 != 0) ? " " + nameForNumber(number % 100) : "");
        } else if (number < 1000000) {
            return nameForNumber(number / 1000) + " thousand" + ((number % 1000 != 0) ? " " + nameForNumber(number % 1000) : "");
        } else if (number < 1000000000) {
            return nameForNumber(number / 1000000) + " million" + ((number % 1000000 != 0) ? " " + nameForNumber(number % 1000000) : "");
        } else if (number < 1000000000000) {
            return nameForNumber(number / 1000000000) + " billion" + ((number % 1000000000 != 0) ? " " + nameForNumber(number % 1000000000) : "");
        }
        return "error";
    }

    int main()
    {
        long input;
        do
        {
            cout << "Please enter a positive integer: ";    
            cin >> input;
            cout << "\n" << nameForNumber(input) << endl;
            cout << "\n\n" << endl;
        }while (input > 0);
        return 0;
    }