Advertisement
daniil_mironoff

Untitled

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