Advertisement
HyperSensualNarwhal

Number systems converter

Dec 22nd, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. int _dectobin(), _dectohex(), _bintodec(), _hextodec();
  9.  
  10. void main()
  11. {
  12.     setlocale(LC_ALL, "Russian");
  13.    
  14.     char key = 0;
  15.  
  16.     while (key != 27)
  17.     {
  18.    
  19.         cout << "1. Decimal to binary\n" << "2. Decimal to hexadecimal\n" << "3. Binary to decimal\n" << "4. Hexadecimal to decimal" << endl << endl;
  20.  
  21.     switch (key)
  22.     {
  23.         case 49:
  24.             _dectobin();
  25.             break;
  26.         case 50:
  27.             _dectohex();
  28.             break;
  29.         case 51:
  30.             _bintodec();
  31.             break;
  32.         case 52:
  33.             _hextodec();
  34.             break;
  35.         default:
  36.             system("cls");
  37.             cout << "1. Decimal to binary\n" << "2. Decimal to hexadecimal\n" << "3. Binary to decimal\n" << "4. Hexadecimal to decimal" << endl << endl;
  38.     }
  39.    
  40.         key = getch();
  41.         system("cls");
  42.     }
  43.  
  44. }
  45.  
  46. int _dectobin()
  47. {
  48.     const int size = 33;
  49.     int binary[size], decimal = (cout << "Введите десятичное (decimal) число -> ", cin >> decimal, cout << endl, decimal);
  50.     int i = size - 1;
  51.  
  52.     while (i != -1)
  53.     {
  54.         binary[i] = decimal % 2;
  55.         decimal = (decimal - binary[i]) / 2;
  56.  
  57.         i--;
  58.     }
  59.  
  60.     cout << "Binary: ";
  61.  
  62.     for (int i = 1; i < size; ((i % 8 == 0) ? cout << binary[i] << "  " : (i % 4 == 0) ? cout << binary[i] << " " : cout << binary[i]), i++);
  63.  
  64.     cout << endl << endl;
  65.  
  66.     return 0;
  67. }
  68.  
  69. int _dectohex()
  70. {
  71.     const int size = 9;
  72.     int decimal = (setlocale(LC_ALL, "Russian"), cout << "Введите десятичное (decimal) число -> ", cin >> decimal, cout << endl, decimal);
  73.     char hexadecimal[size];
  74.  
  75.     int i = size - 1;
  76.  
  77.     while (i != -1)
  78.     {
  79.         hexadecimal[i] = decimal % 16;
  80.         decimal = (decimal - hexadecimal[i]) / 16;
  81.  
  82.         i--;
  83.     }
  84.  
  85.     cout << "Hexadecimal: ";
  86.  
  87.     for (int i = 1; i < size; ((hexadecimal[i] == 10) ? cout << 'A' : (hexadecimal[i] == 11) ? cout << 'B' : (hexadecimal[i] == 12) ? cout << 'C' : (hexadecimal[i] == 13) ? cout << 'D' : (hexadecimal[i] == 14) ?
  88.                                                         cout << 'E' : (hexadecimal[i] == 15) ? cout << 'F' : (i % 4 == 0) ? cout << (int)hexadecimal[i] << " " : cout << (int)hexadecimal[i]), i++);
  89.  
  90.     cout << endl << endl;
  91.  
  92.     return 0;
  93. }
  94.  
  95. int _bintodec()
  96. {
  97.     const int size = 32; // на самом деле тут можно обойтись значением 19, потому как в long long int можно ввести не более 19 разрядов
  98.  
  99.     long long int bin = (cout << "Введите двоичное (binary) число -> ", cin >> bin, cout << endl, bin); // ввод также можно организовать посредством массива (неудобно) или строк (не проходили)
  100.     int power[size], mod = 0, dec = 0, pow = 1;
  101.  
  102.     for (int i = 0; i < size - 1; i++) // запись степеней 2 в массив power
  103.     {
  104.         power[i] = pow;
  105.         pow *= 2;
  106.     }
  107.  
  108.     int i = 0;
  109.  
  110.     while (bin != 0)
  111.     {
  112.         mod = bin % 10;
  113.         bin /= 10; // сокращение двоичного числа на разряд
  114.         dec = dec + (mod * power[i]); // умножение последнего разряда двоичного числа на 2 в степени от 1 и выше
  115.  
  116.         i++; // счетчик для степеней двойки
  117.     }
  118.  
  119.    
  120.     cout << "Decimal: " << dec << endl << endl;
  121.  
  122.     return 0;
  123. }
  124.  
  125. int _hextodec()
  126. {
  127.     const int size = 16;
  128.     long long int power[size], pow = 1;
  129.     long long int dec = 0;
  130.  
  131.     char hex[size] = {};
  132.     cout << "Введите шестнадцатиричное (hexadecimal) число -> "; cin >> hex; cout << endl;
  133.  
  134.     for (int i = 0; i < size; i++)
  135.     {
  136.         power[i] = pow;
  137.         pow *= 16;
  138.     }
  139.  
  140.     int i = 0;
  141.  
  142.     for (int i = 0; i < size; ((hex[i] == 'A') ? hex[i] = (int)10 : (hex[i] == 'B') ? hex[i] = (int)11 : (hex[i] == 'C') ? hex[i] = (int)12 : (hex[i] == 'D') ? hex[i] = (int)13 :
  143.         (hex[i] == 'E') ? hex[i] = (int)14 : (hex[i] == 'F') ? hex[i] = (int)15 : /* (hex[i] <= 0) ? hex[i] *= (int)0 : */ hex[i] = (char)hex[i] - 48), i++);
  144.  
  145.     for (int i = size - 1, j = 0; i >= 0; i--)
  146.     {
  147.         if (hex[i] >= 0) dec = dec + ((int)hex[i] * power[j++]);
  148.     }
  149.  
  150.     cout << "Decimal: " << dec << endl << endl;
  151.  
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement