数字转换成英语的程序(c++实现)

数字转换成英文

如何把数字翻译成英文? 比方1439872321这个数字,看起来很长是不是?
如果你要用中文表达这个数字,你应该就按照4个数字一划,从右边开始,把它划成:14,3987,2321所以这个数字就是十四亿,三千九百万,两千三百二十一,但英文和中文表示的方法是不同的!

英语表示和中文表示的不同

我们中国人在计算数的时候,是按照四个零为万,八个零为亿的。老外则是三个零一计,在英文里,三个零是thousand,六个零是million,九个零是billion。这个方法也叫“点三杠四法”,用这个办法把1439872321这个数划为 1,439,872,321 。在英文里,3个零是thousand,6个零是Million,9个零是Billion。所以这个数字就是一个billion, 四百三十九个million,872个thousand,最后再加上321。用英文说出来的话就是one billion four hundred and thirty-nine million eight hundred and seventy-two thouand three hundred and twenty-one.

因此把数字转换成英语的程序我们可以把它拆分成两部分,一部分是输出数值,一部分是把数字进行拆分三位一份的数。三个零是thousand,六个零是million,九个零是billion。哈哈,好了我就讲解就到这了,废话不多说,上代码!

#include 
#include 
using namespace std;
class robot
{
	public:
		void out(int a);
		void tran_int(int n);
		~robot(){};
};
char *num1[]=
{
	"","one","two","three","four","five","six","seven","eight",
	"nine","ten","eleven","twelve","thirteen","fourteen",
	"fifteen","sixteen","seventeen","eighteen","nineteen"
};
char *num10[]=
{
	"","","twenty","thirty","forty","fifty","sisty","seventy",
	"eighty","ninety"
};
void robot::out(int a)
{
	int b=a%100;
	if(a/100!=0)
	{
		cout<<num1[a/100]<<" hundred ";
		if(b!=0)
		cout<<"and ";
	}
	if(b<20)
	{
		cout<<num1[b];
	}
	else
	{
		cout<<num10[b/10];
		if(b%10!=0)
		cout<<"-"<<num1[b%10];
	}
	
 }
 void robot::tran_int(int n)
 {
 	if(n>1999999999)
 		cout<<"无法处理大于1999999999位的数!"<<endl;
	else
	{
		int a=n/1000000000,b=(n%1000000000)/1000000,c=(n%1000000)/1000,d=n%1000;
		if(a!=0)
		{
			out(a);
			cout<<" billion ";
		}
		if(b!=0)
		{
			out(b);
			cout<<" million ";
		}
		if(c!=0)
		{
			out(c);
			cout<<" thousand ";
		}
		if(d!=0)
		{
			if(d<100&&(a!=0||b!=0||c!=0))
				cout<<"and ";
				out(d);
		}
		cout<<endl;
	 } 
 }
 int main()
 {
 	int n;
	cout<<"请输入n:";
 	cin>>n;
 	cout<<n<<endl;
 	robot a;
 	a.tran_int(n);
 	system("pause");
 	return 0;
 }

运行结果:
数字转换成英语的程序(c++实现)_第1张图片

本文部分内容来自简书:
作者:穿着prada挤地铁
链接:https://www.jianshu.com/p/e272e0188e34

你可能感兴趣的:(算法,c++)