Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- // Перевод из 10-системы в 2-4-2-1
- string func1(string str_10) {
- string str_2_4_2_1;
- for (auto symb : str_10) {
- switch (symb) {
- case '0': str_2_4_2_1 += "0000"; break;
- case '1': str_2_4_2_1 += "0001"; break;
- case '2': str_2_4_2_1 += "0010"; break;
- case '3': str_2_4_2_1 += "0011"; break;
- case '4': str_2_4_2_1 += "0100"; break;
- case '5': str_2_4_2_1 += "1011"; break;
- case '6': str_2_4_2_1 += "1100"; break;
- case '7': str_2_4_2_1 += "1101"; break;
- case '8': str_2_4_2_1 += "1110"; break;
- case '9': str_2_4_2_1 += "1111"; break;
- default: exit(1);
- }
- }
- return str_2_4_2_1;
- }
- // Перевод из 2-4-2-1 в 2-систему
- string func2(string str_2_4_2_1) {
- unsigned int num_10 = 0;
- string str_2;
- // Перевод из 2-4-2-1 в 10-sys
- for (unsigned int i = 0; str_2_4_2_1.size() / 4 > i; i++) {
- string tempStr(str_2_4_2_1, i * 4, 4);
- if (tempStr == "0000") { num_10 *= 10; } else
- if (tempStr == "0001") { num_10 *= 10; num_10 += 1; } else
- if (tempStr == "0010") { num_10 *= 10; num_10 += 2; } else
- if (tempStr == "0011") { num_10 *= 10; num_10 += 3; } else
- if (tempStr == "0100") { num_10 *= 10; num_10 += 4; } else
- if (tempStr == "1011") { num_10 *= 10; num_10 += 5; } else
- if (tempStr == "1100") { num_10 *= 10; num_10 += 6; } else
- if (tempStr == "1101") { num_10 *= 10; num_10 += 7; } else
- if (tempStr == "1110") { num_10 *= 10; num_10 += 8; } else
- if (tempStr == "1111") { num_10 *= 10; num_10 += 9; }
- else { exit(1); }
- }
- // Перевод из 10-sys в 2-sys
- do {
- if (num_10 % 2 == 1) { str_2.insert(str_2.begin(), '1'); }
- else { str_2.insert(str_2.begin(), '0'); }
- num_10 /= 2;
- } while (num_10 != 0);
- return str_2;
- }
- int main() {
- string temp_str;
- cout << "Enter number (10-sys): "; cin >> temp_str;
- cout << "Result (2421 Code): " << func1(temp_str) << endl;
- cout << "Enter number (2421 Code): "; cin >> temp_str;
- cout << "Result (2-sys): " << func2(temp_str) << endl;
- // cout << func2(func1("255")) << endl;
- // 100000000
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement