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
- void func1(string str_10) {
- string str_2_10;
- string str_2_4_2_1;
- // Перевод из 10-sys в 2-10-sys
- for (auto symb : str_10) {
- switch (symb) {
- case '0': str_2_10 += "0000"; break;
- case '1': str_2_10 += "0001"; break;
- case '2': str_2_10 += "0010"; break;
- case '3': str_2_10 += "0011"; break;
- case '4': str_2_10 += "0100"; break;
- case '5': str_2_10 += "0101"; break;
- case '6': str_2_10 += "0110"; break;
- case '7': str_2_10 += "0111"; break;
- case '8': str_2_10 += "1000"; break;
- case '9': str_2_10 += "1001"; break;
- default: exit(1);
- }
- }
- cout << "Result (2-10 sys): " << str_2_10 << endl;
- // Перевод из 2-10-sys в 2-4-2-1
- for (unsigned int i = 0; str_2_10.size() / 4 > i; i++) {
- string tempStr(str_2_10, i * 4, 4);
- if (tempStr == "0000") { str_2_4_2_1 += "0000"; } else
- if (tempStr == "0001") { str_2_4_2_1 += "0001"; } else
- if (tempStr == "0010") { str_2_4_2_1 += "0010"; } else
- if (tempStr == "0100") { str_2_4_2_1 += "0100"; } else
- if (tempStr == "0101") { str_2_4_2_1 += "1011"; } else
- if (tempStr == "0110") { str_2_4_2_1 += "1100"; } else
- if (tempStr == "0111") { str_2_4_2_1 += "1101"; } else
- if (tempStr == "1000") { str_2_4_2_1 += "1110"; } else
- if (tempStr == "1001") { str_2_4_2_1 += "1111"; }
- else { exit(1); }
- }
- cout << "Result (2421 Code): " << str_2_4_2_1 << endl;
- }
- // Перевод из 2-4-2-1 в 2-10-систему
- void func2(string str_2_4_2_1) {
- string str_2_10;
- // Перевод из 2-4-2-1 в 2-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") { str_2_10 += "0000"; } else
- if (tempStr == "0001") { str_2_10 += "0001"; } else
- if (tempStr == "0010") { str_2_10 += "0010"; } else
- if (tempStr == "0011") { str_2_10 += "0011"; } else
- if (tempStr == "0100") { str_2_10 += "0100"; } else
- if (tempStr == "1011") { str_2_10 += "0101"; } else
- if (tempStr == "1100") { str_2_10 += "0110"; } else
- if (tempStr == "1101") { str_2_10 += "0111"; } else
- if (tempStr == "1110") { str_2_10 += "1000"; } else
- if (tempStr == "1111") { str_2_10 += "1001"; }
- else { exit(1); }
- }
- cout << "Result (2-10 sys): " << str_2_10 << endl;
- }
- int main() {
- string temp_str;
- cout << "Enter number (10-sys): "; cin >> temp_str;
- func1(temp_str);
- cout << "Enter number (2421 Code): "; cin >> temp_str;
- func2(temp_str);
- // cout << func2(func1("255")) << endl;
- // 100000000
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement