Advertisement
Guest User

Untitled

a guest
May 26th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. void show_binary (unsigned int u);
  6. unsigned char uncodding (char str[1000], char key);
  7. unsigned char codding (char str[1000], char key);
  8. int main ()
  9. {
  10.     setlocale (LC_ALL, "Russian");
  11.     char temp [100] = {0};
  12. cout << "Введите ключ кодирования ";
  13. cin >> temp;
  14. int key = 0;
  15. key = strlen (temp);
  16. cout << "Количество сдвигов " << key << endl;
  17. cout << "Введите строку ";
  18. char str [1000] = {0};
  19. cin >> str;
  20. //не закодированый массив
  21. for (int i = 0; i < strlen (str) ; i++)
  22. show_binary (str[i]);
  23.  
  24. cout << "Закодированая строка ";
  25. for (int i = 0; i < strlen (str) ; i++)
  26.     {
  27. str [i] = codding (&str [i], key);
  28. cout << str [i];
  29.     }
  30. cout << '\n';
  31. //закодированый массив
  32. for (int i = 0; i < strlen (str) ; i++)
  33. show_binary (str[i]);
  34.  
  35.  
  36. cout << "Раскодированая строка ";
  37. for (int i = 0; i < strlen (str) ; i++)
  38.     {
  39. str [i] = uncodding (&str [i], key);
  40. cout << str [i];
  41.     }
  42. cout << '\n';
  43. //раскодированый массив
  44. for (int i = 0; i < strlen (str) ; i++)
  45. show_binary (str[i]);
  46.  
  47. return 0;
  48. }
  49. unsigned char codding (char str[1000], char key)
  50. {
  51.     int i;
  52.     unsigned int temp;
  53.     temp = str [0];
  54.     for (i = 0; i < strlen (str); i++)
  55.     {
  56.         for (int j = 0; j < key; j++)
  57.         {
  58.            
  59.         if (temp & 128)
  60.         {
  61.             temp = temp << 1;
  62.             temp = temp | 1;
  63.         }
  64.         else
  65.         temp = temp << 1;
  66.         }
  67.         return temp;
  68.     }
  69.    
  70. }
  71. unsigned char uncodding (char str[1000], char key)
  72. {
  73.     int i;
  74.     unsigned char temp;
  75.     temp = (unsigned char) str [0];
  76.     for (i = 0; i < strlen (str); i++)
  77.     {
  78.         for (int j = 0; j < key; j++)
  79.         {
  80.            
  81.         if (temp & 1)
  82.         {
  83.         temp = (unsigned char)temp >> 1;
  84.             temp = (unsigned char)temp | 128;  
  85.            
  86.         }
  87.         else
  88.             temp = (unsigned char)temp >> 1;
  89.        
  90.         }
  91.         return (unsigned char)temp;
  92.     }
  93.    
  94. }
  95. void show_binary (unsigned int u)
  96. {
  97. int t;
  98. for (t=256; t>0; t = t/2)
  99.     if (u & t) cout << "1";
  100.     else cout << "0";
  101.     cout << endl;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement