Boris-Stavrev92

Untitled

Jun 7th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4.  
  5. namespace _18.Different_Integers_Size
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11.  
  12. long num;
  13. var line = Console.ReadLine();
  14. bool cantFitInLong = long.TryParse(line, out num);
  15.  
  16. /* перфектният момент за да научим TryParse()
  17. *
  18. TryParse метода работи по следния начин ... пробва дали нещо може да се
  19. въведе в дадения тип , който е зададен (в нашия случай long)
  20. ако може ... да пренесе стойността си в num ... и var (може и с bool да се напише)
  21. cantFitInLong ще получи булева стойност (true или false)
  22.  
  23. */
  24. if (!cantFitInLong) //
  25. {
  26. Console.WriteLine($"{line} can't fit in any type ");
  27. return;
  28. }
  29.  
  30.  
  31.  
  32. Console.WriteLine($"{num} can fit in:");
  33.  
  34. if (num >= sbyte.MinValue && num <= sbyte.MaxValue)
  35. {
  36. Console.WriteLine("* sbyte");
  37. }
  38. if (num >= byte.MinValue && num <= byte.MaxValue)
  39. {
  40. Console.WriteLine("* byte");
  41. }
  42. if (num >= short.MinValue && num <= short.MaxValue)
  43. {
  44. Console.WriteLine("* short");
  45. }
  46. if (num >= ushort.MinValue && num <= ushort.MaxValue)
  47. {
  48. Console.WriteLine("* ushort");
  49. }
  50. if (num >= int.MinValue && num <= int.MaxValue)
  51. {
  52. Console.WriteLine("* int");
  53. }
  54. if (num >= uint.MinValue && num <= uint.MaxValue)
  55. {
  56. Console.WriteLine("* uint");
  57. }
  58. if (num >= long.MinValue && num <= long.MaxValue)
  59. {
  60. Console.WriteLine("* long");
  61. }
  62.  
  63.  
  64.  
  65.  
  66. /*
  67. // задачата тука е грешна защото когато се проверява за кое число може да се набута в BigInteger ... почти всичко което въведем
  68. // ще може да се набута => трябва да проверяваме само до последния зададен най - голям тип long
  69.  
  70. BigInteger num;
  71. var line = Console.ReadLine();
  72. bool cantFitInBigInteger = BigInteger.TryParse(line , out num);
  73. /*
  74. TryParse метода работи по следния начин ... пробва дали нещо може да се
  75. въведе в дадения тип , който е зададен (в нашия случай BigInteger)
  76. ако може ... ще пренесе стойността си на num ... и var (може и bool)
  77. cantFitInBigInteger ще получи булева стойност (true или false)
  78.  
  79. */
  80. /* if (!cantFitInBigInteger) //
  81. {
  82. Console.WriteLine($"{line} can't fit in any type ");
  83. return;
  84. }
  85.  
  86.  
  87.  
  88. Console.WriteLine($"{num} can fit in:");
  89.  
  90. if (num >= sbyte.MinValue && num <= sbyte.MaxValue)
  91. {
  92. Console.WriteLine("* sbyte");
  93. }
  94. if (num >= byte.MinValue && num <= byte.MaxValue)
  95. {
  96. Console.WriteLine("* byte");
  97. }
  98. if (num >= short.MinValue && num <= short.MaxValue)
  99. {
  100. Console.WriteLine("* short");
  101. }
  102. if (num >= ushort.MinValue && num <= ushort.MaxValue)
  103. {
  104. Console.WriteLine("* ushort");
  105. }
  106. if (num >= int.MinValue && num <= int.MaxValue)
  107. {
  108. Console.WriteLine("* int");
  109. }
  110. if (num >= uint.MinValue && num <= uint.MaxValue)
  111. {
  112. Console.WriteLine("* uint");
  113. }
  114. if (num >= long.MinValue && num <= long.MaxValue)
  115. {
  116. Console.WriteLine("* long");
  117. }
  118.  
  119.  
  120. // */
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. /*
  132. var num = BigInteger.Parse(Console.ReadLine());
  133.  
  134. try
  135. {
  136.  
  137.  
  138. Console.WriteLine($"{num} can fit in:");
  139.  
  140. if (num >= sbyte.MinValue && num <= sbyte.MaxValue)
  141. {
  142. Console.WriteLine("* sbyte");
  143. }
  144. if (num >= byte.MinValue && num <= byte.MaxValue)
  145. {
  146. Console.WriteLine("* byte");
  147. }
  148. if (num >= short.MinValue && num <= short.MaxValue)
  149. {
  150. Console.WriteLine("* short");
  151. }
  152. if (num >= ushort.MinValue && num <= ushort.MaxValue)
  153. {
  154. Console.WriteLine("* ushort");
  155. }
  156. if (num >= int.MinValue && num <= int.MaxValue)
  157. {
  158. Console.WriteLine("* int");
  159. }
  160. if (num >= uint.MinValue && num <= uint.MaxValue)
  161. {
  162. Console.WriteLine("* uint");
  163. }
  164. if (num >= long.MinValue && num <= long.MaxValue)
  165. {
  166. Console.WriteLine("* long");
  167. }
  168. /* if (num >= ulong.MinValue && num <= ulong.MaxValue)
  169. {
  170. Console.WriteLine("* ulong");
  171. }
  172. }
  173. */
  174.  
  175. //*
  176.  
  177. /* catch (OverflowException)
  178. {
  179. Console.WriteLine(num + "can't fit in any type");
  180.  
  181. }
  182.  
  183.  
  184. // */
  185.  
  186. }
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment