Problem
Given an integer between 0 and 999,999, print an English phrase that describes the integer (eg, “One Thousand, Two Hundred and Thirty Four”).
Solution
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
string one_digit[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
string two_digit1[10] = {"ten", "eleven", "twelve", "thirteen", "fourth", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string two_digit[10] = {"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
string base0[10] = {"ten", "hundred", "thousand"};
int get_thousand_digit(int n)
{
return n / 1000;
}
int get_hundred_digit(int n)
{
return (n - get_thousand_digit(n) * 1000)/100;
}
int get_ten_digit(int n)
{
return (n - get_thousand_digit(n) * 1000 - get_hundred_digit(n) * 100) / 10;
}
int get_single_digit(int n)
{
return n % 10;
}
string convert_num_under_100(int n)
{
string ret = "";
if(n < 10){
return one_digit[n];
}
else if(n < 20){
return two_digit1[n - 10];
}
else
{
int t = get_ten_digit(n);
int s = get_single_digit(n);
ret += two_digit[t - 2];
if(s){
ret += " ";
ret += one_digit[s];
}
return ret;
}
}
string transition(int thousand, int hundred, int ten, int single, int pos)
{
string ret;
if(pos == 0){
if(thousand == 0){
return "";
}
else{
if(hundred && (ten || single)){
return " ";
}
else if(hundred &&(!(ten || single))){
return " and ";
}
else if(!hundred &&(ten || single)){
return " and ";
}
else
return "";
}
}
else if(pos == 1){
if(thousand == 0 && hundred == 0){
return "";
}
else{
if(!(ten || single)){
return " ";
}
else{
return " and ";
}
}
}
}
string translate_num(int n)
{
string ret;
if(n < 0 || n > 99999){
cout << "the number is out of the range [0..99999]" << endl;
ret = "Error";
return ret;
}
if(n == 0){
ret = one_digit[0];
return ret;
}
int thousand = get_thousand_digit(n);
int hundred = get_hundred_digit(n);
int ten = get_ten_digit(n);
int single = get_single_digit(n);
ret = "";
if(thousand > 0){
ret += convert_num_under_100(thousand);
ret += " ";
ret += base0[2];
ret += transition(thousand, hundred, ten, single, 0);
}
if(hundred){
ret += one_digit[hundred];
ret += " ";
ret += base0[1];
ret += transition(thousand, hundred, ten, single, 1);
}
if(ten || single){
ret += convert_num_under_100(ten * 10 + single);
}
return ret;
}
int main(int argc, char* argv[])
{
//srand((unsigned)time(NULL));
for(int i = 0; i < 10; i ++){
int n = rand() % 99999 + 1;
cout << n << " in english is " << translate_num(n) << endl;
}
return 0;
}
Output
42 in english is fourty two
18468 in english is eighteen thousand four hundred and sixty eight
6335 in english is six thousand three hundred and thirty five
26501 in english is twenty six thousand five hundred and one
19170 in english is nineteen thousand one hundred and seventy
15725 in english is fifteen thousand seven hundred and twenty five
11479 in english is eleven thousand four hundred and seventy nine
29359 in english is twenty nine thousand three hundred and fifty nine
26963 in english is twenty six thousand nine hundred and sixty three
24465 in english is twenty four thousand four hundred and sixty five