fork download
  1.  
  2.  
  3.  
  4.  
  5.  
  6. using System;
  7. using System.Reflection;
  8. using System.Collections.Generic;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Threading;
  12. using System.Text;
  13. using System.Data;
  14. namespace ConsoleApplication1
  15. {
  16. class NumbersPositionsInWords
  17. {
  18. public string zeroPosition;//One, Two, Three
  19. public string tenthPosition;// Holds Twenty, Thirty, etc.
  20. public string elevenSeries;//Special words like 11 = Eleven, 12 = Tweleve, etc.
  21. }
  22.  
  23. class Program
  24. {
  25. static Dictionary<int, NumbersPositionsInWords> numbersToWordsDictionary = new Dictionary<int, NumbersPositionsInWords>();
  26. static string hundredString = "Hundred";
  27. static StringBuilder numberToWordText = new StringBuilder();
  28.  
  29. static Program()
  30. {
  31. numbersToWordsDictionary.Add(1,
  32. new NumbersPositionsInWords() { zeroPosition = "One", tenthPosition = "Ten", elevenSeries = "Eleven" });
  33.  
  34. numbersToWordsDictionary.Add(0,
  35. new NumbersPositionsInWords() { zeroPosition = "Zero", tenthPosition = null, elevenSeries = "Ten" });
  36.  
  37. numbersToWordsDictionary.Add(2,
  38. new NumbersPositionsInWords() { zeroPosition = "Two", tenthPosition = "Twenty", elevenSeries = "Tweleve" });
  39.  
  40. numbersToWordsDictionary.Add(3,
  41. new NumbersPositionsInWords() { zeroPosition = "Three", tenthPosition = "Thirty", elevenSeries = "Thirteen" });
  42.  
  43. numbersToWordsDictionary.Add(4,
  44. new NumbersPositionsInWords() { zeroPosition = "Four", tenthPosition = "Fourty", elevenSeries = "Fourteen" });
  45.  
  46. numbersToWordsDictionary.Add(5,
  47. new NumbersPositionsInWords() { zeroPosition = "Five", tenthPosition = "Fifty", elevenSeries = "Fifteen" });
  48.  
  49. numbersToWordsDictionary.Add(6,
  50. new NumbersPositionsInWords() { zeroPosition = "Six", tenthPosition = "Sixty", elevenSeries = "Sixteen" });
  51.  
  52. numbersToWordsDictionary.Add(7,
  53. new NumbersPositionsInWords() { zeroPosition = "Seven", tenthPosition = "Seventy", elevenSeries = "Seventeen" });
  54.  
  55. numbersToWordsDictionary.Add(8,
  56. new NumbersPositionsInWords() { zeroPosition = "Eight", tenthPosition = "Eighty", elevenSeries = "Eighteen" });
  57.  
  58. numbersToWordsDictionary.Add(9,
  59. new NumbersPositionsInWords() { zeroPosition = "Nine", tenthPosition = "Ninty", elevenSeries = "Ninteen" });
  60.  
  61. }
  62.  
  63. public static void Main(string[] args)
  64. {
  65. Console.WriteLine("Enter a 3digit number");
  66. string inputString = Console.ReadLine();
  67. ValidateInput(inputString);
  68.  
  69. int hundredthPostionDigit = GetDigitFromPosition(inputString, 0);
  70. StringBuilder resultString = new StringBuilder();
  71.  
  72. if (hundredthPostionDigit > 0)
  73. resultString.AppendFormat("{0}{1} {2}", numbersToWordsDictionary[hundredthPostionDigit].zeroPosition,
  74. string.Empty, hundredString);
  75.  
  76. int tenthPositionDigit = GetDigitFromPosition(inputString, 1);
  77. int unitsPositionDigit = GetDigitFromPosition(inputString, 2);
  78.  
  79. ParseTenthUnitPositionDigit(tenthPositionDigit, unitsPositionDigit);
  80.  
  81. if (resultString.Length > 0 && numberToWordText.Length > 0)
  82. resultString.AppendFormat(" {0} {1}", "And", numberToWordText);
  83. else resultString.Append(numberToWordText);
  84.  
  85. Console.WriteLine(resultString);
  86. Console.ReadLine();
  87. }
  88.  
  89. private static void ValidateInput(string str)
  90. {
  91. int dummy;
  92. if (str.Length > 3 || str.Length < 3 || !Int32.TryParse(str, out dummy))
  93. {
  94. Console.WriteLine("Enter numbers with only 3 digits. Press Enter key to close application.");
  95. Console.ReadLine();
  96. Environment.Exit(0);
  97. }
  98. }
  99.  
  100. private static void ParseTenthUnitPositionDigit(int tenthPositionDigit, int unitsPositionDigit)
  101. {
  102. if (tenthPositionDigit > 1)
  103. numberToWordText.AppendFormat("{0} {1}", numbersToWordsDictionary[tenthPositionDigit].tenthPosition,
  104. unitsPositionDigit != 0 ? numbersToWordsDictionary[unitsPositionDigit].zeroPosition : string.Empty);
  105.  
  106. if (tenthPositionDigit < 1)
  107. numberToWordText.AppendFormat("{0}", unitsPositionDigit > 0 ? numbersToWordsDictionary[unitsPositionDigit].zeroPosition : string.Empty);
  108.  
  109. if (tenthPositionDigit == 1)
  110. numberToWordText.AppendFormat("{0}", unitsPositionDigit == tenthPositionDigit ?
  111. numbersToWordsDictionary[tenthPositionDigit].elevenSeries : numbersToWordsDictionary[unitsPositionDigit].elevenSeries);
  112. }
  113.  
  114. private static int GetDigitFromPosition(string str, int position)
  115. {
  116. return Int32.Parse(str[position].ToString());
  117. }
  118. }
  119. }
  120.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty