Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace CeaserCode
- {
- class Program
- {
- static void Main(string[] args)
- {
- #region code
- StreamReader st = new StreamReader("input.txt");
- StreamWriter ou = new StreamWriter("output.txt");
- StreamWriter fou = new StreamWriter("finaloutput.txt");
- //StreamReader st1 = new StreamReader("output.txt");
- string input = null;
- string local = null;
- string output = null;
- Console.WriteLine("vvedite sdvig");
- int sdvig = Convert.ToInt32(Console.ReadLine());
- //char a = 't';
- //char b = 'z';
- //Console.WriteLine(Convert.ToInt32(a));
- //Console.WriteLine(Convert.ToInt32(b));
- while (true)
- {
- local = st.ReadLine();
- if (local == null) break;
- input += local;
- //local.ToUpper();
- }
- //Console.WriteLine(input);
- Char[] LettersUnconverted = new Char[input.Length];
- input = input.ToLower();
- LettersUnconverted = input.ToCharArray();
- for (int i = 0; i < LettersUnconverted.Length; i++)
- {
- if (Char.IsLetter(LettersUnconverted[i]))
- {
- int j = Convert.ToInt32(LettersUnconverted[i]) - 97;
- j += sdvig;
- j = j % 26;
- j += 97;
- output = output + Convert.ToChar(j);
- }
- else output = output + LettersUnconverted[i];
- }
- ou.WriteLine(output);
- //Console.WriteLine(output);
- Console.ReadLine();
- #endregion
- #region decode
- //while (true)
- //{
- // local = st1.ReadLine();
- // if (local == null) break;
- // input += local;
- // //local.ToUpper();
- //}
- string decodeinp = output;
- 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' };
- 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 };
- int totalcount = 0;
- int[] count = new int[26];
- Double[] Count = new Double[26];
- char[] decodelet = new char[decodeinp.Length];
- decodelet = decodeinp.ToCharArray();
- for (int i = 0; i < decodelet.Length; i++)
- {
- if (char.IsLetter(decodelet[i]))
- {
- int j = Convert.ToInt32(decodelet[i]) - 97;
- count[j]++;
- totalcount++;
- }
- }
- int control = 0;
- for (int i = 0; i < 26; i++)
- {
- Count[i] = (Convert.ToDouble(count[i]) / totalcount) * 100;
- }
- for (int i = 0; i < 26; i++)
- if (Count[i] == Count.Max())
- control = i;
- int popravka = 4 - control;
- string finaloutput = null;
- for (int i = 0; i < decodeinp.Length; i++)
- {
- if (Char.IsLetter(decodeinp[i]))
- {
- int j = Convert.ToInt32(decodeinp[i]) - 97;
- j = j + popravka;
- if (j < 0)
- {
- j = j + 26;
- }
- //j = j % 26;
- j += 97;
- finaloutput = finaloutput + Convert.ToChar(j);
- j = 0;
- }
- else finaloutput = finaloutput + decodeinp[i];
- }
- fou.WriteLine(finaloutput);
- //Console.ReadLine();
- #endregion
- Console.WriteLine("Закодированный файл находится в файле 'output'. Декодированный файл находится в файле 'finaloutput'.");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement