Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Numerics;
- namespace _18.Different_Integers_Size
- {
- class Program
- {
- static void Main(string[] args)
- {
- long num;
- var line = Console.ReadLine();
- bool cantFitInLong = long.TryParse(line, out num);
- /* перфектният момент за да научим TryParse()
- *
- TryParse метода работи по следния начин ... пробва дали нещо може да се
- въведе в дадения тип , който е зададен (в нашия случай long)
- ако може ... да пренесе стойността си в num ... и var (може и с bool да се напише)
- cantFitInLong ще получи булева стойност (true или false)
- */
- if (!cantFitInLong) //
- {
- Console.WriteLine($"{line} can't fit in any type ");
- return;
- }
- Console.WriteLine($"{num} can fit in:");
- if (num >= sbyte.MinValue && num <= sbyte.MaxValue)
- {
- Console.WriteLine("* sbyte");
- }
- if (num >= byte.MinValue && num <= byte.MaxValue)
- {
- Console.WriteLine("* byte");
- }
- if (num >= short.MinValue && num <= short.MaxValue)
- {
- Console.WriteLine("* short");
- }
- if (num >= ushort.MinValue && num <= ushort.MaxValue)
- {
- Console.WriteLine("* ushort");
- }
- if (num >= int.MinValue && num <= int.MaxValue)
- {
- Console.WriteLine("* int");
- }
- if (num >= uint.MinValue && num <= uint.MaxValue)
- {
- Console.WriteLine("* uint");
- }
- if (num >= long.MinValue && num <= long.MaxValue)
- {
- Console.WriteLine("* long");
- }
- /*
- // задачата тука е грешна защото когато се проверява за кое число може да се набута в BigInteger ... почти всичко което въведем
- // ще може да се набута => трябва да проверяваме само до последния зададен най - голям тип long
- BigInteger num;
- var line = Console.ReadLine();
- bool cantFitInBigInteger = BigInteger.TryParse(line , out num);
- /*
- TryParse метода работи по следния начин ... пробва дали нещо може да се
- въведе в дадения тип , който е зададен (в нашия случай BigInteger)
- ако може ... ще пренесе стойността си на num ... и var (може и bool)
- cantFitInBigInteger ще получи булева стойност (true или false)
- */
- /* if (!cantFitInBigInteger) //
- {
- Console.WriteLine($"{line} can't fit in any type ");
- return;
- }
- Console.WriteLine($"{num} can fit in:");
- if (num >= sbyte.MinValue && num <= sbyte.MaxValue)
- {
- Console.WriteLine("* sbyte");
- }
- if (num >= byte.MinValue && num <= byte.MaxValue)
- {
- Console.WriteLine("* byte");
- }
- if (num >= short.MinValue && num <= short.MaxValue)
- {
- Console.WriteLine("* short");
- }
- if (num >= ushort.MinValue && num <= ushort.MaxValue)
- {
- Console.WriteLine("* ushort");
- }
- if (num >= int.MinValue && num <= int.MaxValue)
- {
- Console.WriteLine("* int");
- }
- if (num >= uint.MinValue && num <= uint.MaxValue)
- {
- Console.WriteLine("* uint");
- }
- if (num >= long.MinValue && num <= long.MaxValue)
- {
- Console.WriteLine("* long");
- }
- // */
- /*
- var num = BigInteger.Parse(Console.ReadLine());
- try
- {
- Console.WriteLine($"{num} can fit in:");
- if (num >= sbyte.MinValue && num <= sbyte.MaxValue)
- {
- Console.WriteLine("* sbyte");
- }
- if (num >= byte.MinValue && num <= byte.MaxValue)
- {
- Console.WriteLine("* byte");
- }
- if (num >= short.MinValue && num <= short.MaxValue)
- {
- Console.WriteLine("* short");
- }
- if (num >= ushort.MinValue && num <= ushort.MaxValue)
- {
- Console.WriteLine("* ushort");
- }
- if (num >= int.MinValue && num <= int.MaxValue)
- {
- Console.WriteLine("* int");
- }
- if (num >= uint.MinValue && num <= uint.MaxValue)
- {
- Console.WriteLine("* uint");
- }
- if (num >= long.MinValue && num <= long.MaxValue)
- {
- Console.WriteLine("* long");
- }
- /* if (num >= ulong.MinValue && num <= ulong.MaxValue)
- {
- Console.WriteLine("* ulong");
- }
- }
- */
- //*
- /* catch (OverflowException)
- {
- Console.WriteLine(num + "can't fit in any type");
- }
- // */
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment