English phrase of integer number

Given any integer, print an English phrase that describes the integer(e.g.,”One Thousand, Two Hundred Thirty Four”).

C++:
01 string hundredRep(int x)
02 {
03     string N[10]={“”,“One”,“Two”,“Three”,“Four”,“Five”,“Six”,
04       “Seven”,“Eight”,“Nine”};
05     string M[9]={” “, “Twenty”,“Thirty”,“Forty”, “Fifty”,
06       “Sixty”,“Seventy”,“Eighty”,“Ninty”};
07     string T[10]={“Ten”,“Eleven”,“Twelve”,“Thirteen”,“Fourteen”,“Fifteen”,
08       “Sixteen”,“Seventeen”,“Eighteen”,“Ninteen”};
09     int h=x/100;
10     int t=(xh*100)/10;
11     int f=x%10;
12     string s;
13     if(h>0)
14         s=s+N[h]+M[0]+“Hundred”;
15     if(t==0){
16         if(f==0)
17             return s;
18         else{
19             if(h>0)
20                 s=s+” and “+N[f];
21             else
22                 s=s+” “+N[f];
23             return s;
24         }
25     }
26     if(t==1){
27         s=s+M[0]+T[f];
28         return s;
29     }
30     else{
31         s=s+M[0]+M[t1];
32         if(f==0)
33             return s;
34         s=s+N[f];
35         return s;
36     }
37 }
38 
39 void printEnglishNumber(int x)
40 {
41     int b=x/1000000000;
42     x=xb*1000000000;
43     int m=x/1000000;
44     x=xm*1000000;
45     int s=x/1000;
46     x=xs*1000;
47     string p;
48     if(b>0)
49         p=p+hundredRep(b)+” billion, “;
50     if(m>0)
51         p=p+hundredRep(m)+” million, “;
52     if(s>0)
53         p=p+hundredRep(s)+” thousand, “;
54     if(x>0)
55         p=p+hundredRep(x);
56     cout<<p<<endl;
57 }
Advertisements
This entry was posted in Number trick, String. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s