ntw.cpp - word to num


SUBMITTED BY: Guest

DATE: Dec. 5, 2013, 2:42 p.m.

FORMAT: C++

SIZE: 5.7 kB

HITS: 663

  1. #include "ntw.h"
  2. unsigned long ntw::getNum()
  3. {
  4. return varNum;
  5. }
  6. string ntw::getWord()
  7. {
  8. return Nword;
  9. }
  10. void ntw::u20( unsigned long number, bool one )
  11. {
  12. switch( number )
  13. {
  14. case 1:
  15. if( one )
  16. {
  17. Nword += "eins";
  18. }
  19. else
  20. {
  21. Nword += "ein";
  22. }
  23. break;
  24. case 2: Nword += "zwei"; break;
  25. case 3: Nword += "drei"; break;
  26. case 4: Nword += "vier"; break;
  27. case 5: Nword += "fünf"; break;
  28. case 6: Nword += "sechs"; break;
  29. case 7: Nword += "sieben"; break;
  30. case 8: Nword += "acht"; break;
  31. case 9: Nword += "neun"; break;
  32. case 10: Nword += "zehn"; break;
  33. case 11: Nword += "elf"; break;
  34. case 12: Nword += "zwölf"; break;
  35. case 13: Nword += "dreizehn"; break;
  36. case 14: Nword += "vierzehn"; break;
  37. case 15: Nword += "fünfzehn"; break;
  38. case 16: Nword += "sechszehn"; break;
  39. case 17: Nword += "siebzehn"; break;
  40. case 18: Nword += "achtzehn"; break;
  41. case 19: Nword += "neunzehn"; break;
  42. default: Nword += "-Fehler-"; break;
  43. }
  44. }
  45. //*******************************************************
  46. void ntw::tens( unsigned long number )
  47. {
  48. switch( number / 10 )
  49. {
  50. case 2: Nword += "zwanzig"; break;
  51. case 3: Nword += "dreissig"; break;
  52. case 4: Nword += "vierzig"; break;
  53. case 5: Nword += "fünfzig"; break;
  54. case 6: Nword += "sechzig"; break;
  55. case 7: Nword += "siebzig"; break;
  56. case 8: Nword += "achtzig"; break;
  57. case 9: Nword += "neunzig"; break;
  58. default: Nword += "-Fehler-"; break;
  59. }
  60. }
  61. //*******************************************************
  62. void ntw::u100( unsigned long number, bool one )
  63. {
  64. if( number < 20 )
  65. {
  66. u20( number, one );
  67. }
  68. else if( number % 10 )
  69. {
  70. u20( number % 10, false );
  71. Nword += "und";
  72. tens( number );
  73. }
  74. else
  75. {
  76. tens( number );
  77. }
  78. }
  79. //*******************************************************
  80. void ntw::u1000( unsigned long number, bool one )
  81. {
  82. if( number < 100 )
  83. {
  84. u100( number, one );
  85. }
  86. else if( number % 100 )
  87. {
  88. u20( number / 100, false );
  89. Nword += "hundert";
  90. u100( number % 100, one );
  91. }
  92. else
  93. {
  94. u20( number / 100, false );
  95. Nword += "hundert";
  96. }
  97. }
  98. //*******************************************************
  99. void ntw::thousand( unsigned long number )
  100. {
  101. if( number >= 1000 )
  102. {
  103. u1000( number / 1000, false );
  104. Nword += "tausend";
  105. }
  106. if( number % 1000 )
  107. {
  108. u1000( number % 1000, true );
  109. }
  110. }
  111. //*******************************************************
  112. void ntw::million( unsigned long number )
  113. {
  114. if( number >= 1000000 )
  115. {
  116. int mil = number / 1000000;
  117. if( mil == 1 )
  118. {
  119. Nword = "einemillion";
  120. }
  121. else
  122. {
  123. u1000( mil, false );
  124. Nword += "millionen";
  125. }
  126. number %= 1000000;
  127. }
  128. thousand( number );
  129. }
  130. //*******************************************************
  131. void ntw::milliard( unsigned long number )
  132. {
  133. if( number >= 1000000000 )
  134. {
  135. int mil = number / 1000000000;
  136. if( mil == 1 )
  137. {
  138. Nword += "einemilliarde";
  139. }
  140. else
  141. {
  142. u1000( mil, false );
  143. Nword += "milliarden";
  144. }
  145. number %= 1000000000;
  146. }
  147. million( number );
  148. }
  149. //*******************************************************
  150. string ntw::NumToWord( unsigned long number )
  151. {
  152. varNum = number;
  153. if( number == 0 )
  154. {
  155. Nword = "Null";
  156. }
  157. else
  158. {
  159. milliard( number );
  160. }
  161. Nword[0] = toupper( Nword[0] );
  162. return Nword;
  163. }
  164. //*******************************************************

comments powered by Disqus