1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <ostream>
#include <string>
const std::string NUMSPL1[] = {
"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"
};
const std::string NUMSPL2[] = {
"twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"
};
std::string numSpLt100(const std::string& n) //you can easily change integer (ex: 123) to string ("123")
{
std::string result = "";
if (n.length() == 1) { //length of string n == digit count
result = NUMSPL1[n[0] - '0']; //don't have to use 10 if else for n in range [0-9]
} else if (n.length() == 2) { //2-digit numbers
if (n[0] == '1') {
result = NUMSPL1[n[1] - '0' + 10]; //don't have to use another 10 if else for n in range [10-19]
} else { //n in range [20-99]
result = NUMSPL2[n[0] - '2'];
if (n[1] != '0') result += " " + NUMSPL1[n[1] - '0'];
}
}
//for other lengths 0,3,4,5,... do nothing
return result;
}
int main()
{
std::cout << numSpLt100("0") << std::endl;
std::cout << numSpLt100("7") << std::endl;
std::cout << numSpLt100("16") << std::endl;
std::cout << numSpLt100("24") << std::endl;
std::cout << numSpLt100("50") << std::endl;
std::cout << numSpLt100("99") << std::endl;
}
|