C/C++教程

C++ 将整数转为英文

本文主要是介绍C++ 将整数转为英文,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

这几天帮香港那边一同学看代码,题目大概要求是输入整数,输出英文。

类似如下:

然后我是在同学写的基础上帮改的,感觉有点收藏意义,于是打算在此记录一下。

反正我感觉有点冗余了,我自己写可能会写的不太一样。

或许有错误没发现,发现了可以帮忙指正一下。

#include <iostream>
using namespace std;

int main()
{
    cout << "Please enter an integer number: ";
    int integer;
    cin >> integer;

    if (integer == 0)
    {
        cout << "Zero";
    }

    else
    {
        int num_of_integer;
        int places;
        num_of_integer = 0;
        places = integer;
        do
        {
            num_of_integer += 1;
            places /= 10;
        } while (places > 0);

        int d = 12 - num_of_integer;
        int digit[12];

        for (int i = 0; i < 12; i++)
        {
            if (i < d)
            {
                digit[i] = 0;
            }

            else if (i >=d && i < 12)
            {
                int p = 0, power = 1;
                while (p < 12 - i - 1)
                {
                    power = 10 * power;
                    p += 1;
                }
                digit[i] = integer / power;
                integer = integer - digit[i] * power;
            }
        }

        for (int m = 0; m < 4; m++)
        {
            int m1, m2, m3;
            m1 = 3 * m;
            m2 = 3 * m + 1;
            m3 = 3 * m + 2;
            ///3-digit segments

            //如果三位是000,则跳过这三个
            if (digit[m1]==0 && digit[m2]==0 && digit[m3]==0)
            {
                continue;
            }

            //如果第一位不是0,则表示hundred
            switch (digit[m1])
            {
            case 1:
                cout << "One Hundred ";
                break;
            case 2:
                cout << "Two Hundred ";
                break;
            case 3:
                cout << "Three Hundred ";
                break;
            case 4:
                cout << "Four Hundred ";
                break;
            case 5:
                cout << "Five Hundred ";
                break;
            case 6:
                cout << "Six Hundred ";
                break;
            case 7:
                cout << "Seven Hundred ";
                break;
            case 8:
                cout << "Eight Hundred ";
                break;
            case 9:
                cout << "Nine Hundred ";
                break;
            }

            //如果第一位不是0后面两位不为00,则在hundred后面加and
            if (digit[m1] != 0 && (digit[m2]!=0 || digit[m3]!=0))
            {
                cout << "and ";
            }

            //如果第一位是0后面两位不为00,则在加and,但要考虑10023这种类似情况,如果前面位全部是0则不能加and
            if (digit[m1] == 0 && (digit[m2] != 0 || digit[m3] != 0))
            {
                int x = 0;
                for (int i = 0; i < 3 * m; i++) {
                    x += digit[i];
                }
                if (x != 0) cout << "and ";
            }

            if (digit[m2] == 1)
            {
                switch (digit[m3])
                {
                case 0:                             
                    cout << "Ten ";
                    break;
                case 1:
                    cout << "Eleven ";
                    break;
                case 2:
                    cout << "Twelve ";
                    break;
                case 3:
                    cout << "Thirteen ";
                    break;
                case 4:
                    cout << "Fourteen ";
                    break;
                case 5:
                    cout << "Fifteen ";
                    break;
                case 6:
                    cout << "Sixteen ";
                    break;
                case 7:
                    cout << "Seventeen ";
                    break;
                case 8:
                    cout << "Eighteen ";
                    break;
                case 9:
                    cout << "Nineteen ";
                    break;
                }
            }

            else
            {
                switch (digit[m2])
                {
                case 2:
                    cout << "Twenty ";
                    break;
                case 3:
                    cout << "Thirty ";
                    break;
                case 4:
                    cout << "Forty ";
                    break;
                case 5:
                    cout << "Fifty ";
                    break;
                case 6:
                    cout << "Sixty ";
                    break;
                case 7:
                    cout << "Seventy ";
                    break;
                case 8:
                    cout << "Eighty ";
                    break;
                case 9:
                    cout << "Ninety ";
                    break;
                }

                switch (digit[m3])
                {
                case 1:
                    cout << "One ";
                    break;
                case 2:
                    cout << "Two ";
                    break;
                case 3:
                    cout << "Three ";
                    break;
                case 4:
                    cout << "Four ";
                    break;
                case 5:
                    cout << "Five ";
                    break;
                case 6:
                    cout << "Six ";
                    break;
                case 7:
                    cout << "Seven ";
                    break;
                case 8:
                    cout << "Eight ";
                    break;
                case 9:
                    cout << "Nine ";
                    break;
                }
            }

            switch (m)
            {
            case 0:
                if (digit[m1] == 0 && digit[m2] == 0 && digit[m3] == 0)
                {
                    cout << "";
                }
                else
                {
                    cout << "Billion ";
                }
                break;
            case 1:
                if (digit[m1] == 0 && digit[m2] == 0 && digit[m3] == 0)
                {
                    cout << "";
                }
                else
                {
                    cout << "Million ";
                }
                break;
            case 2:
                if (digit[m1] == 0 && digit[m2] == 0 && digit[m3] == 0)
                {
                    cout << "";
                }
                else
                {
                    cout << "Thousand ";
                }
                break;
            case 3:
                break;
            }

        }

    }
    return 0;
}

结果如下:

仅供参考,请勿直接抄袭!!!

这篇关于C++ 将整数转为英文的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!