Last active
August 29, 2015 14:05
-
-
Save ivycheung1208/1a60aec24434bad7d6bf to your computer and use it in GitHub Desktop.
CC150 17.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 17.7 */ | |
// print an English phrase that describes the given integer | |
// http://en.wikipedia.org/wiki/English_numerals | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
string digits[] = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }; | |
string teens[] = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; | |
string tens[] = { "", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty" }; | |
string bigs[] = { "", "Thousand", "Million", "Billion", "Trillion", "quadrillion" }; | |
string numToString(int number) | |
{ | |
// negative number | |
if (number < 0) | |
return "Negative " + numToString(-number); | |
// small number (0-99) | |
if (number < 10) // [1, 9] | |
return digits[number]; | |
else if (number < 20) // [10, 19] | |
return teens[number % 10]; | |
else if (number < 100) { // [20, 99] | |
string str = tens[number / 10]; | |
if (number % 10 != 0) // the second digit is not zero | |
str += "-" + digits[number % 10]; | |
return str; | |
} | |
// median number (100-999) | |
if (number < 1000) // [100, 999] | |
return digits[number / 100] + " Hundred " + numToString(number % 100); | |
// large number (1000-?) | |
string str = ""; | |
int pow1k = 0; | |
while (number % 1000 != 0) { | |
str = numToString(number % 1000) + " " + bigs[pow1k++] + " " + str; | |
number /= 1000; | |
} | |
return str; | |
} | |
int main() | |
{ | |
int n; | |
cin >> n; | |
cout << numToString(n) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment