Advertisement
StanislavIvanov

10. Data Overflow - Data Types and Variables

Jun 17th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 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.             ulong num1 = ulong.Parse(Console.ReadLine());
  14.             ulong num2 = ulong.Parse(Console.ReadLine());
  15.  
  16.             string typeNum1 = string.Empty;
  17.             string typeNum2 = string.Empty;
  18.  
  19.             double overflowCount = 0.0;
  20.  
  21.             bool num1Byte = (byte.MinValue <= num1 && byte.MaxValue >= num1);
  22.             bool num1Ushort = (ushort.MinValue <= num1 && ushort.MaxValue >= num1);
  23.             bool num1Uint = (uint.MinValue <= num1 && uint.MaxValue >= num1);
  24.             bool num1Ulong = (ulong.MinValue <= num1 && ulong.MaxValue >= num1);
  25.  
  26.             bool num2Byte = (byte.MinValue <= num2 && byte.MaxValue >= num2);
  27.             bool num2Ushort = (ushort.MinValue <= num2 && ushort.MaxValue >= num2);
  28.             bool num2Uint = (uint.MinValue <= num2 && uint.MaxValue >= num2);
  29.             bool num2Ulong = (ulong.MinValue <= num2 && ulong.MaxValue >= num2);
  30.  
  31.             if (num1Byte)
  32.             {
  33.                 typeNum1 = "byte";
  34.             }
  35.             else if (num1Ushort)
  36.             {
  37.                 typeNum1 = "ushort";
  38.             }
  39.             else if (num1Uint)
  40.             {
  41.                 typeNum1 = "uint";
  42.             }
  43.             else
  44.             {
  45.                 typeNum1 = "ulong";
  46.             }
  47.  
  48.  
  49.             if (num2Byte)
  50.             {
  51.                 typeNum2 = "byte";
  52.             }
  53.             else if (num2Ushort)
  54.             {
  55.                 typeNum2 = "ushort";
  56.             }
  57.             else if (num2Uint)
  58.             {
  59.                 typeNum2 = "uint";
  60.             }
  61.             else
  62.             {
  63.                 typeNum2 = "ulong";
  64.             }
  65.  
  66.             if (num1 > num2)
  67.             {
  68.                 Console.WriteLine($"bigger type: {typeNum1}");
  69.                 Console.WriteLine($"smaller type: {typeNum2}");
  70.  
  71.                 if (typeNum1=="ulong")
  72.                 {
  73.                     if (typeNum2=="uint")
  74.                     {
  75.                         overflowCount = (num1 / (double)uint.MaxValue);
  76.                     }
  77.                     else if (typeNum2=="ushort")
  78.                     {
  79.                         overflowCount = (num1 / (double)ushort.MaxValue);
  80.                     }
  81.                     else if (typeNum2=="byte")
  82.                     {
  83.                         overflowCount = (num1 / (double)byte.MaxValue);
  84.                     }
  85.                 }
  86.                 else if (typeNum1=="uint")
  87.                 {
  88.                     if (typeNum2 == "ushort")
  89.                     {
  90.                         overflowCount = (num1 / (double)ushort.MaxValue);
  91.                     }
  92.                     else if (typeNum2 == "byte")
  93.                     {
  94.                         overflowCount = (num1 / (double)byte.MaxValue);
  95.                     }
  96.                 }
  97.                 else if (typeNum1=="ushort")
  98.                 {
  99.                     if (typeNum2=="byte")
  100.                     {
  101.                         overflowCount = (num1 / (double)byte.MaxValue);
  102.                     }
  103.                 }
  104.  
  105.                 Console.WriteLine($"{num1} can overflow {typeNum2} {Math.Round(overflowCount)} times");
  106.             }
  107.             else
  108.             {
  109.                 Console.WriteLine($"bigger type: {typeNum2}");
  110.                 Console.WriteLine($"smaller type: {typeNum1}");
  111.  
  112.                 if (typeNum2 == "ulong")
  113.                 {
  114.                     if (typeNum1 == "uint")
  115.                     {
  116.                         overflowCount = (num2 / (double)uint.MaxValue);
  117.                     }
  118.                     else if (typeNum1 == "ushort")
  119.                     {
  120.                         overflowCount = (num2 / (double)ushort.MaxValue);
  121.                     }
  122.                     else if (typeNum1 == "byte")
  123.                     {
  124.                         overflowCount = (num2 / (double)byte.MaxValue);
  125.                     }
  126.                 }
  127.                 else if (typeNum2 == "uint")
  128.                 {
  129.                     if (typeNum1 == "ushort")
  130.                     {
  131.                         overflowCount = (num2 / (double)ushort.MaxValue);
  132.                     }
  133.                     else if (typeNum1 == "byte")
  134.                     {
  135.                         overflowCount = (num2 / (double)byte.MaxValue);
  136.                     }
  137.                 }
  138.                 else if (typeNum2 == "ushort")
  139.                 {
  140.                     if (typeNum1 == "byte")
  141.                     {
  142.                         overflowCount = (num2 / (double)byte.MaxValue);
  143.                     }
  144.                 }
  145.                 Console.WriteLine($"{num2} can overflow {typeNum1} {Math.Round(overflowCount)} times");
  146.             }
  147.  
  148.    
  149.  
  150.  
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement