Advertisement
stchorbadzhiev

Data Types - Problem 10. Data Overflow

Jun 7th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 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 _10.Data_Overflow
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var a = ulong.Parse(Console.ReadLine());
  14.             var b = ulong.Parse(Console.ReadLine());
  15.             decimal overflow = 0.0m;
  16.             ulong maxA = ulong.MaxValue;
  17.             ulong maxB = ulong.MaxValue;
  18.             string typeA = "ulong";
  19.             string typeB = "ulong";
  20.             if (byte.MinValue <= a && a <= byte.MaxValue)
  21.             {
  22.                 a = (byte)a;
  23.                 maxA = byte.MaxValue;
  24.                 typeA = "byte";
  25.             }
  26.             else if (ushort.MinValue <= a && a <= ushort.MaxValue)
  27.             {
  28.                 a = (ushort)a;
  29.                 maxA = ushort.MaxValue;
  30.                 typeA = "ushort";
  31.             }
  32.             else if (uint.MinValue <= a && a <= uint.MaxValue)
  33.             {
  34.                 a = (uint)a;
  35.                 maxA = uint.MaxValue;
  36.                 typeA = "uint";
  37.             }
  38.             if (byte.MinValue <= b && b <= byte.MaxValue)
  39.             {
  40.                 b = (byte)b;
  41.                 maxB = byte.MaxValue;
  42.                 typeB = "byte";
  43.             }
  44.             else if (ushort.MinValue <= b && b <= ushort.MaxValue)
  45.             {
  46.                 b = (ushort)b;
  47.                 maxB = ushort.MaxValue;
  48.                 typeB = "ushort";
  49.             }
  50.             else if (uint.MinValue <= b && b <= uint.MaxValue)
  51.             {
  52.                 b = (uint)b;
  53.                 maxB = uint.MaxValue;
  54.                 typeB = "uint";
  55.             }
  56.  
  57.             if (a > b)
  58.             {
  59.                 Console.WriteLine($"bigger type: {typeA}");
  60.                 Console.WriteLine($"smaller type: {typeB}");
  61.                 overflow = (decimal)a / maxB;
  62.                 Console.WriteLine($"{a} can overflow {typeB} {Math.Round(overflow)} times");
  63.             }
  64.             else
  65.             {
  66.                 Console.WriteLine($"bigger type: {typeB}");
  67.                 Console.WriteLine($"smaller type: {typeA}");
  68.                 overflow = (decimal)b / maxA;
  69.                 Console.WriteLine($"{b} can overflow {typeA} {Math.Round(overflow)} times");
  70.             }
  71.            
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement