Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:
如22:twenty two,123:one hundred and twenty three。
说明:
数字为正整数,长度不超过九位,不考虑小数,转化结果为英文小写;
输出格式为twenty two;
非法数据请返回“error”;
关键字提示:and,billion,million,thousand,hundred。
方法原型:public static String parse(long num)
输入一个long型整数
输出相应的英文写法
示例1
2356
two thousand three hundred and fifty six
//第四十一题 学英语
#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;
}
#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<