英語の勉強/ファーウェイ試験(C/C++)


タイトルの説明
Jessiは英語を初めて勉強して、一連の数字を速く読むために、プログラムを編纂して数字を英語に変換します:
例えば22:twenty two,123:one hundred and twenty three.
 
説明:
数字は正の整数で、長さは9桁を超えず、小数を考慮せず、変換結果は英語の小文字になる.
出力フォーマットはtwenty twoです.
不正なデータは「error」に戻ってください.
キーワードヒント:and,billion,million,thousand,hundred.
メソッドプロトタイプ:public static String parse(long num)
説明を入力:
long型整数を入力
出力の説明:
対応する英語の書き方を出力
例1
入力
2356

しゅつりょく
two thousand three hundred and fifty six

コード1:長い間デバッグしていました
//          
#include
#include
using namespace std;
int main()
{
	string sP[9]{ "billion","","million","","","thousand","hundred","and" };
	string sNum[28]{"zero", "one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen",
	"fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety" };
	string inStr;
	while (cin >> inStr)
	{
		size_t iLength = inStr.length();
		if (iLength > 9)
		{
			cout << "error" << endl;
			continue;
		}
		for (int i = iLength-1; i >= 0; i--)
		{
			int temp = iLength - i - 1;
			if (iLength > 2 && i == 1)
			{
				if ((inStr[temp] - '0') && (inStr[temp-1] - '0'))
                {
                    cout << sP[9 - i - 1] << " " 
                        << sNum[(inStr[temp] - '0') > 1 ? (inStr[temp] - '0' + 18) : (inStr[temp] - '0' + 9+inStr[temp+1] - '0')] << " ";
                    
                }
				else if(inStr[temp] - '0')
					cout << sNum[(inStr[temp] - '0') > 1 ? (inStr[temp] - '0' + 18) : (inStr[temp] - '0' + 9)] << " ";
				else
					cout << sP[9 - i - 1] << " ";
			}			
			else if (iLength > 2 && i > 1)
			{
				if (i != 4 && i != 5)
				{
					if((inStr[temp] - '0'))
						cout << sNum[inStr[temp] - '0'] << " " << sP[9 - i - 1] << " ";
					else if(i==3 && (inStr[temp - 1] - '0'))
						cout << sP[9 - i - 1] << " ";
				}
					
				else if(i==4 && (inStr[temp] - '0'))
				    cout << sNum[inStr[temp] - '0' + 18] << " ";
				else if (i == 5)
				{
					if(inStr[temp] - '0')
						cout << sNum[inStr[temp] - '0'] << " " << sP[6] << " " << sP[7] << " ";
					else
						cout << sP[6] << " " << sP[7] << " ";
				}		
			}
			else if (iLength > 2 && i < 1)
			{
				if ((inStr[temp] - '0') &&((inStr[temp - 1] - '0')!=1))
					cout << sNum[inStr[temp] - '0'];
			}
				
		}
		cout << endl;
	}
	return 0;
}

コード2:本人が書いたものではなく、他の人がどのように書いたかを見てみましょう.
#include
#include
#include
 
using namespace std;
 
string arr1[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
string arr2[] = { "ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nighteen" };
string arr3[] = { "","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety" };
string arr4[] = { "and","hundred"};
string arr5[] = {"","thousand","million","billion"};
 
string convert(int num)
    {
    string s;
    if(num>0&&num<=9)
        {
        s+=arr1[num];
    }
    else if(num>=10&&num<=19)
        {
        s+=arr2[num%10];
    }
    else if(num>=20&&num<=99)
        {
        if(num%10==0)
            {
            s+=arr3[num/10];
        }
        else
            {
            s+=arr3[num/10]+' '+arr1[num%10];
        }
    }
    else if(num>=100&&num<=999)
        {
        if(num%100==0)
            {
            s+=arr1[num/100]+' '+arr4[1];
        }
        else if(num%100<=9)
            {
            s+=arr1[num/100]+' '+arr4[1]+' '+arr4[0]+' '+arr1[num%100];
        }
        else
            {
            s+=arr1[num/100]+' '+arr4[1]+' '+arr4[0]+' '+convert(num%100);
        }
    }
    return s;
}
 
int main()
{
    long num;
    while (cin >> num)
        {
        string res;
        vector v;
        while(num)
            {
            v.push_back(num%1000);
            num=num/1000;
        }
        for(int i=v.size()-1;i>=0;i--)
            {
            res+=convert(v[i]);
            if(i!=0)
                {
                res+=' ';
            }
            res+=arr5[i];
            if(i!=0)
                {
                res+=' ';
            }
        }
        cout<