tranerius

dec to bin / bin to dec converter

Mar 3rd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.53 KB | None | 0 0
  1. class Number_systems
  2. {
  3.     public enum Num_system
  4.     {
  5.         bin_to_dec,
  6.         dec_to_bin
  7.     }
  8.  
  9.     static void Main()
  10.     {
  11.         uint num_of_operation;
  12.         string value = "";
  13.         while (true)
  14.         {
  15.             do
  16.             {
  17.                 System.Console.Write("Список доступных операций\n1 - Перевести из двоичной в десятичную\n2 - Перевести из десятичной в двоичную" +
  18.                     "\n0 - Завершить работу\nКакую операцию необходимо выполнить? ");
  19.                 num_of_operation = System.Convert.ToUInt32(Value_check());
  20.                 if (num_of_operation == 1)
  21.                 {
  22.                     value = "двоичное";
  23.                 }
  24.                 else if (num_of_operation == 2)
  25.                 {
  26.                     value = "десятичное";
  27.                 }
  28.                 System.Console.Clear();
  29.                 if (num_of_operation != 1 && num_of_operation != 2 && num_of_operation != 0)
  30.                 {
  31.                     System.Console.WriteLine("Ошибка: введен неверный ответ. Попробуйте ещё раз");
  32.                     System.Console.Write("Для продолжения нажмите любую клавишу . . . ");
  33.                     System.Console.ReadKey();
  34.                     System.Console.Clear();
  35.                 }
  36.             } while (num_of_operation > 2);
  37.             if (num_of_operation == 0)
  38.             {
  39.                 break;
  40.             }
  41.             else
  42.             {
  43.                 System.Console.Write("Введите {0} значение: ", value);
  44.                 value = Value_check().ToString();
  45.                 System.Console.Clear();
  46.                 Start_operation(--num_of_operation, value);
  47.             }
  48.             System.Console.Write("Для продолжения нажмите любую клавишу . . . ");
  49.             System.Console.ReadKey();
  50.             System.Console.Clear();
  51.         }
  52.     }
  53.  
  54.     public static void Start_operation(uint num_of_operation, string value)
  55.     {
  56.         if (num_of_operation == 0)
  57.         {
  58.             System.Console.WriteLine("Результат перевода {0} из двоичной в десятичную форму равен {1}", value, Bin_to_dec(value));
  59.         }
  60.         else if (num_of_operation == 1)
  61.         {
  62.             System.Console.WriteLine("Результат перевода {0} из десятичной в двоичную форму равен {1}", value, Dec_to_bin(value));
  63.         }
  64.     }
  65.  
  66.     public static string Bin_to_dec(string bin)
  67.     {
  68.         int value = 0;
  69.         bool have_minus = false;
  70.         for (int i = 1; i <= bin.Length; i++)
  71.         {
  72.             if (i == 1 && (bin[i - 1] == '-' || bin[i - 1] == '+'))
  73.             {
  74.                 if (bin[i - 1] == '-')
  75.                 {
  76.                     have_minus = true;
  77.                 }
  78.                 continue;
  79.             }
  80.             if (bin[i - 1] != '0' && bin[i - 1] != '1')
  81.             {
  82.                 return "ошибке. Двоичное число может содержать только цифры 0 и 1";
  83.             }
  84.             if (bin[i - 1] == '1')
  85.             {
  86.                 if (!have_minus)
  87.                 {
  88.                     value += System.Convert.ToInt32(System.Math.Pow(2, bin.Length - i));
  89.                 }
  90.                 else
  91.                 {
  92.                     value -= System.Convert.ToInt32(System.Math.Pow(2, bin.Length - i));
  93.                 }
  94.             }
  95.         }
  96.  
  97.         return value.ToString();
  98.     }
  99.  
  100.     public static string Dec_to_bin(string dec)
  101.     {
  102.         string value = "";
  103.         bool have_minus = false;
  104.         if (System.Convert.ToInt32(dec) < 0)
  105.         {
  106.             have_minus = true;
  107.         }
  108.         while (System.Convert.ToInt32(dec) != 0)
  109.         {
  110.             if (System.Convert.ToInt32(dec) % 2 != 0)
  111.             {
  112.                 value += '1';
  113.             }
  114.             else
  115.             {
  116.                 value += '0';
  117.             }
  118.             dec = (System.Convert.ToInt32(dec) / 2).ToString();
  119.         }
  120.         return Flip(value, have_minus);
  121.     }
  122.  
  123.     public static double Value_check()
  124.     {
  125.         bool was_point = false;
  126.         string check = System.Console.ReadLine();
  127.         for (int i = 0; i < check.Length; i++)
  128.         {
  129.             if (check[i] == '.' || check[i] == ',' && !was_point)
  130.             {
  131.                 check = check.Remove(i, 1);
  132.                 check = check.Insert(i, ",");
  133.                 was_point = true;
  134.             }
  135.             else if (check[i] == '.' || check[i] == ',' && was_point)
  136.             {
  137.                 check = check.Remove(i, 1);
  138.                 i--;
  139.             }
  140.             if ((check[i] < 48 && check[i] != 43 && check[i] != 44 && check[i] != 45 && check[i] != 0) || check[i] > 57)
  141.             {
  142.                 check = check.Remove(i, 1);
  143.                 i--;
  144.             }
  145.         }
  146.         if (check.Length == 0)
  147.         {
  148.             return 0;
  149.         }
  150.         return System.Convert.ToDouble(check);
  151.     }
  152.  
  153.     public static string Flip(string str, bool have_minus)
  154.     {
  155.         string new_str = "";
  156.         for (int i = str.Length; i != 0; i--)
  157.         {
  158.             new_str += str[i - 1];
  159.         }
  160.         if (have_minus)
  161.         {
  162.             new_str = new_str.Insert(0, "-");
  163.         }
  164.         return new_str;
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment