Advertisement
viraco4a

DifferentIntegerSizes

May 17th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _18_DiffIntSize
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string input = Console.ReadLine();
  14. string[] Types;
  15. Dictionary<string, bool> Dict;
  16. CreateDict(out Types, out Dict);
  17. int counter = check(input, Dict);
  18. if (counter > 0)
  19. {
  20. Console.WriteLine($"{input} can fit in:");
  21. foreach (var item in Dict)
  22. {
  23. KeyValuePair<string, bool> kvp = item;
  24. if (kvp.Value == true)
  25. {
  26. Console.WriteLine($"* {kvp.Key}");
  27. }
  28. }
  29. }
  30. else
  31. {
  32. Console.WriteLine($"{input} can't fit in any type");
  33. }
  34. }
  35.  
  36. private static void CreateDict(out string[] Types, out Dictionary<string, bool> Dict)
  37. {
  38. Types = new string[]
  39. {
  40. "sbyte", "byte", "short", "ushort", "int", "uint", "long"
  41. };
  42. Dict = new Dictionary<string, bool>();
  43. foreach (var item in Types)
  44. {
  45. Dict.Add(item, false);
  46. }
  47. }
  48.  
  49. private static int check(string input, Dictionary<string, bool> dict)
  50. {
  51. int counter = 0;
  52.  
  53. if (TryLong(input))
  54. {
  55. counter++;
  56. dict["long"] = true;
  57. }
  58. if (TryUInt(input))
  59. {
  60. counter++;
  61. dict["uint"] = true;
  62. }
  63. if (TryInt(input))
  64. {
  65. counter++;
  66. dict["int"] = true;
  67. }
  68. if (TryUshort(input))
  69. {
  70. counter++;
  71. dict["ushort"] = true;
  72. }
  73. if (TryShort(input))
  74. {
  75. counter++;
  76. dict["short"] = true;
  77. }
  78. if (TryByte(input))
  79. {
  80. counter++;
  81. dict["byte"] = true;
  82. }
  83. if (TrySbyte(input))
  84. {
  85. counter++;
  86. dict["sbyte"] = true;
  87. }
  88.  
  89. return counter;
  90. }
  91.  
  92. private static bool TryLong(string input)
  93. {
  94. bool longType = false;
  95. try
  96. {
  97. long N = long.Parse(input);
  98. longType = true;
  99.  
  100. }
  101. catch (Exception)
  102. {
  103. longType = false;
  104. }
  105.  
  106. return longType;
  107. }
  108.  
  109. private static bool TryUInt(string input)
  110. {
  111. bool uIntType = false;
  112. try
  113. {
  114. uint N = uint.Parse(input);
  115. uIntType = true;
  116.  
  117. }
  118. catch (Exception)
  119. {
  120. uIntType = false;
  121. }
  122.  
  123. return uIntType;
  124. }
  125.  
  126. private static bool TryInt(string input)
  127. {
  128. bool IntType = false;
  129. try
  130. {
  131. int N = int.Parse(input);
  132. IntType = true;
  133.  
  134. }
  135. catch (Exception)
  136. {
  137. IntType = false;
  138. }
  139.  
  140. return IntType;
  141. }
  142.  
  143. private static bool TryUshort(string input)
  144. {
  145. bool ushortType = false;
  146. try
  147. {
  148. ushort N = ushort.Parse(input);
  149. ushortType = true;
  150.  
  151. }
  152. catch (Exception)
  153. {
  154. ushortType = false;
  155. }
  156.  
  157. return ushortType;
  158. }
  159.  
  160. private static bool TryShort(string input)
  161. {
  162. bool shortType = false;
  163. try
  164. {
  165. short N = short.Parse(input);
  166. shortType = true;
  167.  
  168. }
  169. catch (Exception)
  170. {
  171. shortType = false;
  172. }
  173.  
  174. return shortType;
  175. }
  176.  
  177. private static bool TryByte(string input)
  178. {
  179. bool byteType = false;
  180. try
  181. {
  182. byte N = byte.Parse(input);
  183. byteType = true;
  184.  
  185. }
  186. catch (Exception)
  187. {
  188. byteType = false;
  189. }
  190.  
  191. return byteType;
  192. }
  193.  
  194. private static bool TrySbyte(string input)
  195. {
  196. bool sbyteType = false;
  197. try
  198. {
  199. sbyte N = sbyte.Parse(input);
  200. sbyteType = true;
  201.  
  202. }
  203. catch (Exception)
  204. {
  205. sbyteType = false;
  206. }
  207.  
  208. return sbyteType;
  209. }
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement