Advertisement
daniil_mironoff

Untitled

Nov 30th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // Перевод из 10-системы в 2-4-2-1
  7. string func1(string str_10) {
  8.     string str_2_4_2_1;
  9.  
  10.     for (auto symb : str_10) {
  11.         switch (symb) {
  12.             case '0': str_2_4_2_1 += "0000"; break;
  13.             case '1': str_2_4_2_1 += "0001"; break;
  14.             case '2': str_2_4_2_1 += "0010"; break;
  15.             case '3': str_2_4_2_1 += "0011"; break;
  16.             case '4': str_2_4_2_1 += "0100"; break;
  17.  
  18.             case '5': str_2_4_2_1 += "1011"; break;
  19.             case '6': str_2_4_2_1 += "1100"; break;
  20.             case '7': str_2_4_2_1 += "1101"; break;
  21.             case '8': str_2_4_2_1 += "1110"; break;
  22.             case '9': str_2_4_2_1 += "1111"; break;
  23.            
  24.             default: exit(1);
  25.         }
  26.        
  27.     }
  28.  
  29.  
  30.  
  31.     return str_2_4_2_1;
  32. }
  33.  
  34. // Перевод из 2-4-2-1 в 2-систему
  35. string func2(string str_2_4_2_1) {
  36.     unsigned int num_10 = 0;
  37.     string str_2;
  38.  
  39.     // Перевод из 2-4-2-1 в 10-sys
  40.     for (unsigned int i = 0; str_2_4_2_1.size() / 4 > i; i++) {
  41.         string tempStr(str_2_4_2_1, i * 4, 4);
  42.  
  43.         if (tempStr == "0000") { num_10 *= 10;              } else
  44.         if (tempStr == "0001") { num_10 *= 10; num_10 += 1; } else
  45.         if (tempStr == "0010") { num_10 *= 10; num_10 += 2; } else
  46.         if (tempStr == "0011") { num_10 *= 10; num_10 += 3; } else
  47.         if (tempStr == "0100") { num_10 *= 10; num_10 += 4; } else
  48.        
  49.         if (tempStr == "1011") { num_10 *= 10; num_10 += 5; } else
  50.         if (tempStr == "1100") { num_10 *= 10; num_10 += 6; } else
  51.         if (tempStr == "1101") { num_10 *= 10; num_10 += 7; } else
  52.         if (tempStr == "1110") { num_10 *= 10; num_10 += 8; } else
  53.         if (tempStr == "1111") { num_10 *= 10; num_10 += 9; }
  54.  
  55.         else { exit(1); }
  56.     }
  57.  
  58.     // Перевод из 10-sys в 2-sys
  59.     do {
  60.         if (num_10 % 2 == 1) { str_2.insert(str_2.begin(), '1'); }
  61.         else                 { str_2.insert(str_2.begin(), '0'); }
  62.  
  63.         num_10 /= 2;
  64.     } while (num_10 != 0);
  65.  
  66.  
  67.  
  68.     return str_2;
  69. }
  70.  
  71.  
  72.  
  73. int main() {
  74.     string temp_str;
  75.  
  76.     cout << "Enter number (10-sys): "; cin >> temp_str;
  77.     cout << "Result (2421 Code): " << func1(temp_str) << endl;
  78.  
  79.     cout << "Enter number (2421 Code): "; cin >> temp_str;
  80.     cout << "Result (2-sys): " << func2(temp_str) << endl;
  81.  
  82.     // cout << func2(func1("255")) << endl;
  83.     // 100000000
  84.  
  85.  
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement