Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Number_systems
- {
- public enum Num_system
- {
- bin_to_dec,
- dec_to_bin
- }
- static void Main()
- {
- uint num_of_operation;
- string value = "";
- while (true)
- {
- do
- {
- System.Console.Write("Список доступных операций\n1 - Перевести из двоичной в десятичную\n2 - Перевести из десятичной в двоичную" +
- "\n0 - Завершить работу\nКакую операцию необходимо выполнить? ");
- num_of_operation = System.Convert.ToUInt32(Value_check());
- if (num_of_operation == 1)
- {
- value = "двоичное";
- }
- else if (num_of_operation == 2)
- {
- value = "десятичное";
- }
- System.Console.Clear();
- if (num_of_operation != 1 && num_of_operation != 2 && num_of_operation != 0)
- {
- System.Console.WriteLine("Ошибка: введен неверный ответ. Попробуйте ещё раз");
- System.Console.Write("Для продолжения нажмите любую клавишу . . . ");
- System.Console.ReadKey();
- System.Console.Clear();
- }
- } while (num_of_operation > 2);
- if (num_of_operation == 0)
- {
- break;
- }
- else
- {
- System.Console.Write("Введите {0} значение: ", value);
- value = Value_check().ToString();
- System.Console.Clear();
- Start_operation(--num_of_operation, value);
- }
- System.Console.Write("Для продолжения нажмите любую клавишу . . . ");
- System.Console.ReadKey();
- System.Console.Clear();
- }
- }
- public static void Start_operation(uint num_of_operation, string value)
- {
- if (num_of_operation == 0)
- {
- System.Console.WriteLine("Результат перевода {0} из двоичной в десятичную форму равен {1}", value, Bin_to_dec(value));
- }
- else if (num_of_operation == 1)
- {
- System.Console.WriteLine("Результат перевода {0} из десятичной в двоичную форму равен {1}", value, Dec_to_bin(value));
- }
- }
- public static string Bin_to_dec(string bin)
- {
- int value = 0;
- bool have_minus = false;
- for (int i = 1; i <= bin.Length; i++)
- {
- if (i == 1 && (bin[i - 1] == '-' || bin[i - 1] == '+'))
- {
- if (bin[i - 1] == '-')
- {
- have_minus = true;
- }
- continue;
- }
- if (bin[i - 1] != '0' && bin[i - 1] != '1')
- {
- return "ошибке. Двоичное число может содержать только цифры 0 и 1";
- }
- if (bin[i - 1] == '1')
- {
- if (!have_minus)
- {
- value += System.Convert.ToInt32(System.Math.Pow(2, bin.Length - i));
- }
- else
- {
- value -= System.Convert.ToInt32(System.Math.Pow(2, bin.Length - i));
- }
- }
- }
- return value.ToString();
- }
- public static string Dec_to_bin(string dec)
- {
- string value = "";
- bool have_minus = false;
- if (System.Convert.ToInt32(dec) < 0)
- {
- have_minus = true;
- }
- while (System.Convert.ToInt32(dec) != 0)
- {
- if (System.Convert.ToInt32(dec) % 2 != 0)
- {
- value += '1';
- }
- else
- {
- value += '0';
- }
- dec = (System.Convert.ToInt32(dec) / 2).ToString();
- }
- return Flip(value, have_minus);
- }
- public static double Value_check()
- {
- bool was_point = false;
- string check = System.Console.ReadLine();
- for (int i = 0; i < check.Length; i++)
- {
- if (check[i] == '.' || check[i] == ',' && !was_point)
- {
- check = check.Remove(i, 1);
- check = check.Insert(i, ",");
- was_point = true;
- }
- else if (check[i] == '.' || check[i] == ',' && was_point)
- {
- check = check.Remove(i, 1);
- i--;
- }
- if ((check[i] < 48 && check[i] != 43 && check[i] != 44 && check[i] != 45 && check[i] != 0) || check[i] > 57)
- {
- check = check.Remove(i, 1);
- i--;
- }
- }
- if (check.Length == 0)
- {
- return 0;
- }
- return System.Convert.ToDouble(check);
- }
- public static string Flip(string str, bool have_minus)
- {
- string new_str = "";
- for (int i = str.Length; i != 0; i--)
- {
- new_str += str[i - 1];
- }
- if (have_minus)
- {
- new_str = new_str.Insert(0, "-");
- }
- return new_str;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment