问题:输入-999999~999999之间的数字,输出其英文文本。
代码如下:
/*
从-999999~999999的数字转换成英文文本。
作者:x-lion
*/
#include <iostream>
using namespace std;
void numtotext(int num); //数字转换成文本
void dowith3d(int num); //处理三数组
int main()
{
int num;
cout<<"Please enter an integer(-999999~999999):"<<endl;
cin>>num;
if(num == 0) //输入数字为0
{
cout<<"zero"<<endl;
}
if(num<0) //输入数字为负数,输出minus,然后转换为对应正数问题
{
cout<<"minus ";
num = -num;
}
int num_h = num/1000; //取前三位
int num_l = num%1000; //取后三位
if(num_h!=0)
{
dowith3d(num_h); //处理前三位
cout<<" thousand ";
}
if(num_l!=0)
{
dowith3d(num_l); //处理后三位
}
return 0;
}
void dowith3d(int num)
{
int num_huns = num/100;
int num_tens = num%100;
if(num_huns>0)
{
numtotext(num_huns);
cout<<" hundred ";
}
if(num_tens!=0)
{
if(num_tens<=20)
{
numtotext(num_tens);
}
else
{
numtotext(num_tens/10*10);
cout<<" ";
numtotext(num_tens%10);
}
}
}
void numtotext(int num)
{
switch(num)
{
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;
case 10:
cout<<"ten";
break;
case 11:
cout<<"eleven";
break;
case 12:
cout<<"twelve";
break;
case 13:
cout<<"thirteen";
break;
case 14:
cout<<"fourteen";
break;
case 15:
cout<<"fifteen";
break;
case 16:
cout<<"sixteen";
break;
case 17:
cout<<"seventeen";
break;
case 18:
cout<<"eighteen";
break;
case 19:
cout<<"nineteen";
break;
case 20:
cout<<"twenty";
break;
case 30:
cout<<"thirty";
break;
case 40:
cout<<"forty";
break;
case 50:
cout<<"fifty";
break;
case 60:
cout<<"sixty";
break;
case 70:
cout<<"seventy";
break;
case 80:
cout<<"eighty";
break;
case 90:
cout<<"ninety";
break;
}
}