Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace CeaserCode
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14. #region code
  15.             StreamReader st = new StreamReader("input.txt");
  16.             StreamWriter ou = new StreamWriter("output.txt");
  17.             StreamWriter fou = new StreamWriter("finaloutput.txt");
  18.             //StreamReader st1 = new StreamReader("output.txt");
  19.  
  20.             string input = null;
  21.             string local = null;
  22.             string output = null;
  23.             Console.WriteLine("vvedite sdvig");
  24.             int sdvig = Convert.ToInt32(Console.ReadLine());
  25.             //char a = 't';
  26.             //char b = 'z';
  27.             //Console.WriteLine(Convert.ToInt32(a));
  28.             //Console.WriteLine(Convert.ToInt32(b));
  29.  
  30.             while (true)
  31.             {
  32.                 local = st.ReadLine();
  33.                 if (local == null) break;
  34.                 input += local;
  35.                 //local.ToUpper();
  36.             }
  37.             //Console.WriteLine(input);
  38.             Char[] LettersUnconverted = new Char[input.Length];
  39.             input = input.ToLower();
  40.             LettersUnconverted = input.ToCharArray();
  41.             for (int i = 0; i < LettersUnconverted.Length; i++)
  42.             {
  43.                 if (Char.IsLetter(LettersUnconverted[i]))
  44.                 {
  45.  
  46.                     int j = Convert.ToInt32(LettersUnconverted[i]) - 97;
  47.                     j += sdvig;
  48.                     j = j % 26;
  49.                     j += 97;
  50.                     output = output + Convert.ToChar(j);
  51.                 }
  52.                 else output = output + LettersUnconverted[i];
  53.  
  54.             }
  55.             ou.WriteLine(output);
  56.             //Console.WriteLine(output);
  57.             Console.ReadLine();
  58.             #endregion
  59.             #region decode
  60.             //while (true)
  61.             //{
  62.             //    local = st1.ReadLine();
  63.             //    if (local == null) break;
  64.             //    input += local;
  65.             //    //local.ToUpper();
  66.             //}
  67.             string decodeinp = output;
  68.             char[] alphabet = new char[26] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
  69.             double[] friqency = new double[26] { 8.16, 1.49, 2.78, 4.25, 12.7, 2.28, 2.01, 6.09, 6.96, 0.15, 0.77, 4.02, 2.4, 6.75, 7.5, 1.92, 0.09, 5.98, 6.32, 9.05, 2.75, 0.97, 2.36, 0.15, 1.97, 0.07 };
  70.             int totalcount = 0;
  71.             int[] count = new int[26];
  72.             Double[] Count = new Double[26];
  73.             char[] decodelet = new char[decodeinp.Length];
  74.             decodelet = decodeinp.ToCharArray();
  75.             for (int i = 0; i < decodelet.Length; i++)
  76.             {
  77.                 if (char.IsLetter(decodelet[i]))
  78.                 {
  79.                     int j = Convert.ToInt32(decodelet[i]) - 97;
  80.                     count[j]++;
  81.                     totalcount++;
  82.                 }
  83.  
  84.             }
  85.  
  86.             int control = 0;
  87.  
  88.             for (int i = 0; i < 26; i++)
  89.             {
  90.                 Count[i] = (Convert.ToDouble(count[i]) / totalcount) * 100;
  91.  
  92.             }
  93.  
  94.  
  95.  
  96.             for (int i = 0; i < 26; i++)
  97.                 if (Count[i] == Count.Max())
  98.                     control = i;
  99.             int popravka = 4 - control;
  100.             string finaloutput = null;
  101.             for (int i = 0; i < decodeinp.Length; i++)
  102.             {
  103.                 if (Char.IsLetter(decodeinp[i]))
  104.                 {
  105.                     int j = Convert.ToInt32(decodeinp[i]) - 97;
  106.                     j = j + popravka;
  107.                     if (j < 0)
  108.                     {
  109.                         j = j + 26;
  110.                     }
  111.                     //j = j % 26;
  112.                     j += 97;
  113.                     finaloutput = finaloutput + Convert.ToChar(j);
  114.                     j = 0;
  115.                 }
  116.                 else finaloutput = finaloutput + decodeinp[i];
  117.  
  118.             }
  119.             fou.WriteLine(finaloutput);
  120.  
  121.             //Console.ReadLine();
  122.             #endregion
  123.             Console.WriteLine("Закодированный файл находится в файле 'output'. Декодированный файл находится в файле 'finaloutput'.");
  124.             Console.ReadLine();
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement