Advertisement
daniil_mironoff

Untitled

Dec 1st, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // Перевод из 10-системы в 2-4-2-1
  7. void 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 << "Result (2-10 sys): " << str_2_10 << endl;
  30.  
  31.     // Перевод из 2-10-sys в 2-4-2-1
  32.     for (unsigned int i = 0; str_2_10.size() / 4 > i; i++) {
  33.         string tempStr(str_2_10, i * 4, 4);
  34.        
  35.  
  36.         if (tempStr == "0000") { str_2_4_2_1 += "0000"; } else
  37.         if (tempStr == "0001") { str_2_4_2_1 += "0001"; } else
  38.         if (tempStr == "0010") { str_2_4_2_1 += "0010"; } else
  39.         if (tempStr == "0100") { str_2_4_2_1 += "0100"; } else
  40.         if (tempStr == "0101") { str_2_4_2_1 += "1011"; } else
  41.         if (tempStr == "0110") { str_2_4_2_1 += "1100"; } else
  42.         if (tempStr == "0111") { str_2_4_2_1 += "1101"; } else
  43.         if (tempStr == "1000") { str_2_4_2_1 += "1110"; } else
  44.         if (tempStr == "1001") { str_2_4_2_1 += "1111"; }
  45.  
  46.         else { exit(1); }
  47.     }
  48.  
  49.     cout << "Result (2421 Code): " << str_2_4_2_1 << endl;
  50. }
  51.  
  52. // Перевод из 2-4-2-1 в 2-10-систему
  53. void func2(string str_2_4_2_1) {
  54.     string str_2_10;
  55.  
  56.     // Перевод из 2-4-2-1 в 2-10-sys
  57.     for (unsigned int i = 0; str_2_4_2_1.size() / 4 > i; i++) {
  58.         string tempStr(str_2_4_2_1, i * 4, 4);
  59.  
  60.         if (tempStr == "0000") { str_2_10 += "0000"; } else
  61.         if (tempStr == "0001") { str_2_10 += "0001"; } else
  62.         if (tempStr == "0010") { str_2_10 += "0010"; } else
  63.         if (tempStr == "0011") { str_2_10 += "0011"; } else
  64.         if (tempStr == "0100") { str_2_10 += "0100"; } else
  65.         if (tempStr == "1011") { str_2_10 += "0101"; } else
  66.         if (tempStr == "1100") { str_2_10 += "0110"; } else
  67.         if (tempStr == "1101") { str_2_10 += "0111"; } else
  68.         if (tempStr == "1110") { str_2_10 += "1000"; } else
  69.         if (tempStr == "1111") { str_2_10 += "1001"; }
  70.  
  71.         else { exit(1); }
  72.     }
  73.  
  74.  
  75.  
  76.     cout << "Result (2-10 sys): " << str_2_10 << endl;
  77. }
  78.  
  79.  
  80.  
  81. int main() {
  82.     string temp_str;
  83.  
  84.     cout << "Enter number (10-sys): "; cin >> temp_str;
  85.     func1(temp_str);
  86.  
  87.     cout << "Enter number (2421 Code): "; cin >> temp_str;
  88.     func2(temp_str);
  89.  
  90.     // cout << func2(func1("255")) << endl;
  91.     // 100000000
  92.  
  93.  
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement