fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. const std::string NUMSPL1[] = {
  5. "zero", "one", "two", "three", "four", "five", "six",
  6. "seven", "eight", "nine", "ten", "eleven", "twelve",
  7. "thirteen", "fourteen", "fifteen", "sixteen",
  8. "seventeen", "eighteen", "nineteen"
  9. };
  10. const std::string NUMSPL2[] = {
  11. "twenty", "thirty", "forty", "fifty", "sixty",
  12. "seventy", "eighty", "ninety"
  13. };
  14. const std::string NUMSPL3[] = {
  15. "hundred", "thousand", "million", "billion", "trillion",
  16. "quadrillion", "quintillion", "sextillion", "septillion",
  17. "octillion", "nonillion", "decillion", "undecillion",
  18. "duodecillion", "tredecillion", "quattuordecillion", "quindecillion",
  19. "sexdecillion", "septendecillion", "octodecillion", "novemdecillion",
  20. "vigintillion"
  21. };
  22.  
  23. std::string numSp(const std::string& num);
  24.  
  25. int main()
  26. {
  27. std::cout << "0" << ": " << numSp("0") << ".\n\n"
  28. << "13" << ": " << numSp("13") << ".\n\n"
  29. << "218" << ": " << numSp("218") << ".\n\n"
  30. << "1204" << ": " << numSp("1204") << ".\n\n"
  31. << "10009" << ": " << numSp("10009") << ".\n\n"
  32. << "023.456.951.743.236.125.000.012.000" << ":\n"
  33. << numSp("023456951743236125000012000") << ".\n\n"
  34. << "777.777.777.777.777.777.777.777.777.777.777.777.777.777.777."
  35. "777.777.777.777.777.777.777" ":\n"
  36. << numSp("777777777777777777777777777777777777777777777777777777"
  37. "777777777777") << ".\n\n";
  38. }
  39.  
  40. std::string removeLeadingZeros(const std::string& n)
  41. {
  42. std::string result = "0";
  43. if (n.length() == 1) {
  44. result = n;
  45. } else if (n.length() > 1) {
  46. for (size_t i = 0; i < n.length(); ++i)
  47. if (n[i] != '0') {
  48. result = n.substr(i);
  49. break;
  50. }
  51. }
  52. return result;
  53. }
  54.  
  55. std::string numSpLt100(const std::string& num)
  56. {
  57. std::string n = removeLeadingZeros(num);
  58. std::string result = "";
  59. if (n.length() == 1) {
  60. result = NUMSPL1[n[0] - '0'];
  61. } else if (n.length() == 2) {
  62. if (n[0] == '1') {
  63. result = NUMSPL1[n[1] - '0' + 10];
  64. } else {
  65. result = NUMSPL2[n[0] - '2'];
  66. if (n[1] != '0') result += " " + NUMSPL1[n[1] - '0'];
  67. }
  68. }
  69. return result;
  70. }
  71.  
  72. std::string numSpLt1000(const std::string& num)
  73. {
  74. std::string n = removeLeadingZeros(num);
  75. std::string result = "";
  76. if (n.length() == 1 || n.length() == 2) {
  77. result = numSpLt100(n);
  78. } else if (n.length() == 3) {
  79. std::string lt100 = numSpLt100(n.substr(1));
  80. result = NUMSPL1[n[0] - '0'] + " " + NUMSPL3[0];
  81. if (lt100 != NUMSPL1[0]) result += " " + lt100;
  82. }
  83. return result;
  84. }
  85.  
  86. std::string numSp(const std::string& num)
  87. {
  88. std::string n = removeLeadingZeros(num);
  89. std::string res = "";
  90. if (n.length() <= 3) {
  91. res = numSpLt1000(n);
  92. } else if (n.length() <= 66) {
  93. int t = 1;
  94. res = numSpLt1000(n.substr(n.length() - 3));
  95. if (res == NUMSPL1[0]) res = "";
  96. while (n.length() - 3*t > 3)
  97. {
  98. std::string tmp = numSpLt1000( n.substr(n.length()-3*++t, 3) );
  99. if (tmp != NUMSPL1[0])
  100. res = tmp + " " + NUMSPL3[t-1] + (res!="" ? ", " : "") + res;
  101. }
  102. res = numSpLt1000( n.substr(0, n.length() - 3*t) ) + " " +
  103. NUMSPL3[t] + (res!="" ? ", " : "") + res;
  104. }
  105. return res;
  106. }
Success #stdin #stdout 0s 2996KB
stdin
Standard input is empty
stdout
0: zero.

13: thirteen.

218: two hundred eighteen.

1204: one thousand, two hundred four.

10009: ten thousand, nine.

023.456.951.743.236.125.000.012.000:
twenty three septillion, four hundred fifty six sextillion, nine hundred fifty one quintillion, seven hundred forty three quadrillion, two hundred thirty six trillion, one hundred twenty five billion, twelve thousand.

777.777.777.777.777.777.777.777.777.777.777.777.777.777.777.777.777.777.777.777.777.777:
seven hundred seventy seven vigintillion, seven hundred seventy seven novemdecillion, seven hundred seventy seven octodecillion, seven hundred seventy seven septendecillion, seven hundred seventy seven sexdecillion, seven hundred seventy seven quindecillion, seven hundred seventy seven quattuordecillion, seven hundred seventy seven tredecillion, seven hundred seventy seven duodecillion, seven hundred seventy seven undecillion, seven hundred seventy seven decillion, seven hundred seventy seven nonillion, seven hundred seventy seven octillion, seven hundred seventy seven septillion, seven hundred seventy seven sextillion, seven hundred seventy seven quintillion, seven hundred seventy seven quadrillion, seven hundred seventy seven trillion, seven hundred seventy seven billion, seven hundred seventy seven million, seven hundred seventy seven thousand, seven hundred seventy seven.