Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- void show_binary (unsigned int u);
- unsigned char uncodding (char str[1000], char key);
- unsigned char codding (char str[1000], char key);
- int main ()
- {
- setlocale (LC_ALL, "Russian");
- char temp [100] = {0};
- cout << "Введите ключ кодирования ";
- cin >> temp;
- int key = 0;
- key = strlen (temp);
- cout << "Количество сдвигов " << key << endl;
- cout << "Введите строку ";
- char str [1000] = {0};
- cin >> str;
- //не закодированый массив
- for (int i = 0; i < strlen (str) ; i++)
- show_binary (str[i]);
- cout << "Закодированая строка ";
- for (int i = 0; i < strlen (str) ; i++)
- {
- str [i] = codding (&str [i], key);
- cout << str [i];
- }
- cout << '\n';
- //закодированый массив
- for (int i = 0; i < strlen (str) ; i++)
- show_binary (str[i]);
- cout << "Раскодированая строка ";
- for (int i = 0; i < strlen (str) ; i++)
- {
- str [i] = uncodding (&str [i], key);
- cout << str [i];
- }
- cout << '\n';
- //раскодированый массив
- for (int i = 0; i < strlen (str) ; i++)
- show_binary (str[i]);
- return 0;
- }
- unsigned char codding (char str[1000], char key)
- {
- int i;
- unsigned int temp;
- temp = str [0];
- for (i = 0; i < strlen (str); i++)
- {
- for (int j = 0; j < key; j++)
- {
- if (temp & 128)
- {
- temp = temp << 1;
- temp = temp | 1;
- }
- else
- temp = temp << 1;
- }
- return temp;
- }
- }
- unsigned char uncodding (char str[1000], char key)
- {
- int i;
- unsigned char temp;
- temp = (unsigned char) str [0];
- for (i = 0; i < strlen (str); i++)
- {
- for (int j = 0; j < key; j++)
- {
- if (temp & 1)
- {
- temp = (unsigned char)temp >> 1;
- temp = (unsigned char)temp | 128;
- }
- else
- temp = (unsigned char)temp >> 1;
- }
- return (unsigned char)temp;
- }
- }
- void show_binary (unsigned int u)
- {
- int t;
- for (t=256; t>0; t = t/2)
- if (u & t) cout << "1";
- else cout << "0";
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement