Topic:
Given an integer, the English description of the integer is printed. (Example: "One thousand, Hundred thirty four")
Ideas:
Convert every three bits once, inserting million,thousand in place. That
Convert (323 984) =convert (+) + "million," +convert (323) + "thousand" +convert (984)
Code:
#include <iostream>using namespacestd;stringdigits[]={" One"," Both","three"," Four","Five","Six","Seven","Eight","Nine"};stringteens[]={"Eleven","Twelve","Thirtteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};stringtens[]={"Ten","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};stringbigs[]={"","thousand,","Million,"};stringNUMTOENGLISH100 (intnum);stringNumtoenglish (intnum) { if(num==0) return "Zero"; Else if(num<0){ return "Negative"+ Numtoenglish (-1*num); } intCount=0; stringStr=""; while(num>0){ if(num% +!=0) {str=NUMTOENGLISH100 (num% +) +bigs[count]+" "+str; } num/= +; Count++; } returnstr;}stringNUMTOENGLISH100 (intnum) { stringStr=""; if(num>= -) {str+=digits[num/ --1]+"Hundred"; Num%= -; } if(num>= One&& num<= +){ returnstr+teens[num- One]+" "; } Else if(num==Ten|| num>= -) {str+=tens[num/Ten-1]+" "; Num%=Ten; } if(num>=1&& num<=9) Str+=digits[num-1]+" "; returnstr;}intMain () {intnum; while(cin>>num) {cout<< numtoenglish (num) <<Endl; } return 0;}
(algorithm) integer to English description