Advertisement
SavaIv

Untitled

Jan 8th, 2020
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _14._Numbers_to_Words_3
  4. {
  5. class Program
  6. {
  7. // --- SOLUTION WITH 1 METOD + 3 SUB METODS + 1 SUB SUB METOD (11 to 19) --------------------
  8.  
  9. static void Main(string[] args)
  10. {
  11. int loops = int.Parse(Console.ReadLine());
  12.  
  13. for (int i = 1; i <= loops; i++)
  14. {
  15. int number = int.Parse(Console.ReadLine());
  16. string prnData = Letterize(number);
  17. Console.WriteLine(prnData);
  18. }
  19. }
  20.  
  21. static string Letterize(int number)
  22. {
  23. string borderBreaks = string.Empty;
  24. // before to start with the calculation is good to clean the
  25. // border cases (numbers witn more than 3 digits & numbers with less than 3 digits)
  26. if (number > 999)
  27. {
  28. borderBreaks = "too large";
  29. return borderBreaks;
  30. }
  31. else if (number < -999)
  32. {
  33. borderBreaks = "too small";
  34. return borderBreaks;
  35. }
  36.  
  37. string negativeORpositive = string.Empty; // as defoult is asume that the number is positive
  38. if (number < 0) negativeORpositive = "minus ";
  39. number = Math.Abs(number); // we will work with the absolute value of "n"
  40.  
  41. //
  42. int onesINT = number % 10;
  43. int tensINT = (number / 10) % 10;
  44. int handredsINT = (number / 100) % 10;
  45.  
  46. // as a generall - the code is working for every number between -999 to 999
  47. // but in the requiremnts is askin that if the number is consist of less that 3 digits
  48. // the answer to be ""
  49. // that is this filter for
  50. if (handredsINT == 0) return "";
  51.  
  52. // ще завъртим 3+1 метода (3 + метода за 11 до 19)
  53. string onesSTR = Ones(onesINT, tensINT, handredsINT);
  54. string tensSTR = Tens(onesINT, tensINT, handredsINT);
  55. string handredsSTR = Handreds(onesINT, tensINT, handredsINT);
  56.  
  57. // --- prn ---
  58. // the print is a little bit complicate - thats why here is the expression of the print:
  59. // +/-(1) handreds(2) and(3) tens(4) space(5) ones(6) --> following this, the print
  60. // will be aranged as 1+2+3+4+5+6 --> for every print member is necessary to be checked
  61. // a few conditions
  62.  
  63. // seems that the next asignemnts are not necessary becouse we olready have some of variables
  64. // and is not necessary to asigne them again to anather variable
  65. // BUT, to make more cleare the print arangement, this would be usefull:
  66.  
  67. string one = string.Empty; // here, we will kept the + or - value
  68. one = negativeORpositive;
  69.  
  70. string two = string.Empty; // here, we will kept value of the handreds
  71. if (handredsINT != 0) two = handredsSTR;
  72.  
  73. string three = string.Empty; // here, we will kept the word " and " - in case we will need it
  74. if (handredsINT != 0 && (tensINT != 0 || onesINT != 0)) three = " and ";
  75.  
  76. string four = string.Empty; // here, we will kept value of the tens
  77. if (tensINT != 0) four = tensSTR;
  78.  
  79. string five = string.Empty; // this is the spasion between tens and ones (if have)
  80. if (tensINT >= 2 && onesINT != 0) five = " ";
  81.  
  82. string six = string.Empty; // here, we will kept value of the ones
  83. if (tensINT != 1 && onesINT > 0) six = onesSTR;
  84.  
  85. //return value from the metod is:
  86. string result = one + two + three + four + five + six;
  87.  
  88. return result;
  89.  
  90. }
  91.  
  92. static string Handreds(int ons, int tns, int hndrds)
  93. // nonused parameters are included in the Handreds( parameters ) - this is in case...
  94. // sometimes... in the future ... may be... will be needed for a some sort check.
  95. {
  96. string handredsAsWord = string.Empty;
  97.  
  98. switch (hndrds)
  99. {
  100. case 0: handredsAsWord = ""; break;
  101. case 1: handredsAsWord = "one-hundred"; break;
  102. case 2: handredsAsWord = "two-hundred"; break;
  103. case 3: handredsAsWord = "three-hundred"; break;
  104. case 4: handredsAsWord = "four-hundred"; break;
  105. case 5: handredsAsWord = "five-hundred"; break;
  106. case 6: handredsAsWord = "six-hundred"; break;
  107. case 7: handredsAsWord = "seven-hundred"; break;
  108. case 8: handredsAsWord = "eight-hundred"; break;
  109. case 9: handredsAsWord = "nine-hundred"; break;
  110. //default: handredsAsWord = ""; break;
  111.  
  112. }
  113.  
  114. return handredsAsWord;
  115.  
  116. }
  117.  
  118. static string Tens(int ons, int tns, int hndrds)
  119. // nonused parameters are included in the Tens( parameters ) - this is in case...
  120. // sometimes... in the future ... may be... will be needed for a some sort check.
  121. {
  122. string tensAsWord = string.Empty;
  123.  
  124. switch (tns)
  125. {
  126. case 0: tensAsWord = ""; break;
  127. case 1: tensAsWord = case1119(ons); break; // this is the case between 11 and 19
  128. case 2: tensAsWord = "twenty"; break;
  129. case 3: tensAsWord = "thirty"; break;
  130. case 4: tensAsWord = "forty"; break;
  131. case 5: tensAsWord = "fifty"; break;
  132. case 6: tensAsWord = "sixty"; break;
  133. case 7: tensAsWord = "seventy"; break;
  134. case 8: tensAsWord = "eighty"; break;
  135. case 9: tensAsWord = "ninety"; break;
  136. //default: handredsAsWord = ""; break;
  137.  
  138. }
  139.  
  140. return tensAsWord;
  141. }
  142.  
  143. static string Ones(int ons, int tns, int hndrds)
  144. // nonused parameters are included in the Ones( parameters ) - this is in case...
  145. // sometimes... in the future ... may be... will be needed for a some sort check.
  146. {
  147. string onesAsWord = string.Empty;
  148.  
  149.  
  150. switch (ons)
  151. {
  152. case 0: onesAsWord = ""; break;
  153. case 1: onesAsWord = "one"; break;
  154. case 2: onesAsWord = "two"; break;
  155. case 3: onesAsWord = "three"; break;
  156. case 4: onesAsWord = "four"; break;
  157. case 5: onesAsWord = "five"; break;
  158. case 6: onesAsWord = "six"; break;
  159. case 7: onesAsWord = "seven"; break;
  160. case 8: onesAsWord = "eight"; break;
  161. case 9: onesAsWord = "nine"; break;
  162.  
  163. }
  164.  
  165. return onesAsWord;
  166. }
  167.  
  168.  
  169. static string case1119(int ons)
  170. {
  171. string teens = string.Empty;
  172.  
  173. switch (ons)
  174. {
  175. case 0: teens = "ten"; break;
  176. case 1: teens = "eleven"; break;
  177. case 2: teens = "twelve"; break;
  178. case 3: teens = "thirteen"; break;
  179. case 4: teens = "fourteen"; break;
  180. case 5: teens = "fifteen"; break;
  181. case 6: teens = "sixteen"; break;
  182. case 7: teens = "seventeen"; break;
  183. case 8: teens = "eighteen"; break;
  184. case 9: teens = "nineteen"; break;
  185.  
  186. }
  187.  
  188.  
  189. return teens;
  190.  
  191. }
  192.  
  193.  
  194.  
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement