Advertisement
Constantine27

Untitled

Oct 2nd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2.  
  3. class Sixth
  4. {
  5.  
  6.     const string alphabet = "abcdefghijklmnopqrstuvwxyz";       // объявление константы
  7.  
  8.     static void Main(string[] args)
  9.     {
  10.         int quantity, shift;
  11.         string word;               // объявление переменных
  12.         string ciphtext;
  13.         if (!int.TryParse(Console.ReadLine(), out quantity) || quantity <= 0 || !int.TryParse(Console.ReadLine(), out shift))   // проверка верности введенных данных
  14.         {
  15.             Console.WriteLine("wrong");     // сообщение об ошибке
  16.             return;     // заавершение работы программы
  17.         }
  18.  
  19.         Checking(quantity, shift);     // вызов метода
  20.  
  21.     }
  22.     static void Caesar(string word, int shift, out string ciphtext)     // метод раскрытия шифра Цезаря
  23.     {
  24.         shift %= 26;                // объявление переменных
  25.         ciphtext = "";
  26.         for (int i = 0; i < word.Length; i++)
  27.         {
  28.             char ch = word[i];
  29.             int index = alphabet.IndexOf(ch);       // расчеты
  30.             if (index < 0)
  31.             {
  32.                 ciphtext += ch.ToString();
  33.             }
  34.             else
  35.             {
  36.                 int SecondIndex = (alphabet.Length + index + shift) % alphabet.Length;
  37.                 ciphtext += alphabet[SecondIndex];
  38.             }
  39.         }
  40.         Console.WriteLine(ciphtext);
  41.     }
  42.  
  43.  
  44.     static void Checking(int quantity, int shift)       // объявление метода проверки
  45.     {
  46.         string word;               // объявление переменных
  47.         string ciphtext;
  48.         for (int i = 0; i < quantity; i++)
  49.         {
  50.             word = Console.ReadLine();
  51.             for (int r = 0; r < word.Length; r++)
  52.             {
  53.                 char ch2 = word[r];
  54.                 if (ch2 < 97 || ch2 > 122)
  55.                 {
  56.                     Console.WriteLine("wrong");         // сообщение об ошибке
  57.                     return;     // завершание работы программы
  58.                 }
  59.             }
  60.             Caesar(word, shift, out ciphtext);      // вызов метода
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement