fabis_sparks

KatyaCaesarCrypt

Dec 15th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. // KatyaCezarCrypt.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. int main()
  7. {
  8.     setlocale(LC_ALL, "Russian");
  9.     using namespace std;
  10.     char buff[50] = { 0 };
  11.     const short int start_alphabet = 97; // ASCII код буквы 'a'
  12.     const short int end_alphabet = 122; // ASCII код буквы  'z'
  13.     cout << "Введите сообщение,которое будет закодированно: ";
  14.     cin >> buff;
  15.     cout << endl;
  16.     cout << "Введите шаг кодировки: ";
  17.     short int step;
  18.     cin >> step;
  19.     cout << endl;
  20.     // Кодируем сообщение
  21.     for (int i = 0;i < strlen(buff); i++)
  22.         // сделаем проверку на то, чтобы мы не вышли за границу ASCII кодов алфавита английского языка строчных букв
  23.         if ((int(buff[i])+step) > end_alphabet) {
  24.         // Выясняем на сколько нам следует передвинуть в начале
  25.         short int new_pos = int(buff[i])+step - end_alphabet;
  26.         // Присваиваем закодированное значение для буквы
  27.         buff[i]= start_alphabet + new_pos - 1;
  28.         }
  29.         else buff[i] = buff[i] + step;
  30.     cout << "Encode: " << endl;
  31.     cout << buff << endl;
  32.     // Декодируем сообщение 
  33.     for (int i = 0;i < strlen(buff); i++)
  34.         // опять проверка на выход из границ
  35.         if ((int(buff[i])-step) < start_alphabet) {
  36.         // перебрасываем в конец алфавита, если ушли за начало
  37.         short int new_pos =  start_alphabet - int(buff[i]-step) ;
  38.         // присваиваем раскодированное значение
  39.         buff[i]=end_alphabet - new_pos + 1;
  40.         }
  41.         else buff[i] = buff[i] - step;
  42.     cout << "Decode: " << endl;
  43.     cout << buff << endl;
  44.    
  45.     system("pause");
  46.     return 0;
  47. }
Add Comment
Please, Sign In to add comment