Advertisement
Guest User

Not a virus

a guest
Mar 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Jackson
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.WriteLine("Введите длину слова");
  10.             int N= Convert.ToInt32(Console.ReadLine());
  11.             int deg = (int)Math.Round(Math.Log(N, 2));
  12.             int?[] arr = new int?[N + 1 + Convert.ToInt32(deg)];
  13.  
  14.             input();
  15.             count(false);
  16.  
  17.             Console.WriteLine("Слово c {0} дополнительными битами:", deg+1);
  18.             output();
  19.  
  20.             Console.WriteLine("Cлово с ошибкой:");
  21.             add_err();
  22.             output();
  23.  
  24.             decoding();
  25.  
  26.             Console.ReadKey();
  27.  
  28.  
  29.             void input()
  30.             {
  31.  
  32.                 Console.WriteLine("Введите слово");
  33.  
  34.                 for(int j = 0; j < deg+1; j++)
  35.                 {
  36.                     arr[(int)Math.Pow(2, j)-1] = 0;
  37.                 }
  38.  
  39.                 for (int i = 0; i < arr.Length; i++)
  40.                 {
  41.                     if (arr[i] == 0)
  42.                         continue;
  43.                     arr[i] = Convert.ToInt32(Console.ReadLine());
  44.                 }
  45.                 Console.WriteLine("\n");
  46.             }
  47.  
  48.             void output()
  49.             {
  50.                 for (int i =0 ,j = 0; i < arr.Length; i++)
  51.                 {
  52.                     if ((i == (int)Math.Pow(2, j) - 1)&&(j<deg+1))
  53.                     {
  54.                         Console.ForegroundColor = ConsoleColor.Red;
  55.                         Console.Write(arr[i]);
  56.                         j++;
  57.                         Console.ResetColor();
  58.                         continue;
  59.                     }
  60.                     Console.Write(arr[i]);
  61.                 }
  62.                 Console.Write("\n");
  63.                 Console.WriteLine("\n");
  64.             }
  65.  
  66.             void output1(int ind)
  67.             {
  68.                 for (int i = 0, j = 0; i < arr.Length; i++)
  69.                 {
  70.                     if (i==ind)
  71.                     {
  72.                         Console.ForegroundColor = ConsoleColor.Cyan;
  73.                         Console.Write(arr[i]);
  74.                         Console.ResetColor();
  75.                         continue;
  76.                     }
  77.                     if ((i == (int)Math.Pow(2, j) - 1) && (j < deg + 1))
  78.                     {
  79.                         Console.ForegroundColor = ConsoleColor.Red;
  80.                         Console.Write(arr[i]);
  81.                         j++;
  82.                         Console.ResetColor();
  83.                         continue;
  84.                     }
  85.                     Console.Write(arr[i]);
  86.                 }
  87.                 Console.Write("\n");
  88.                 Console.WriteLine("\n");
  89.             }
  90.  
  91.             int count(bool flag)
  92.             {
  93.                 int sum=0;
  94.                 for (int j = 0; j < deg + 1; j++)
  95.                 {
  96.                     int? temp = arr[(int)Math.Pow(2, j) - 1];
  97.  
  98.                     for (int z = (int)Math.Pow(2, j); z <= arr.Length; z += (2 * (int)Math.Pow(2, j)))
  99.                     {
  100.                         for (int i = z - 1; (i < (z - 1 + (int)Math.Pow(2, j)))&&(i< arr.Length); i++)
  101.                         {
  102.                             arr[(int)Math.Pow(2, j) - 1] = arr[(int)Math.Pow(2, j) - 1] ^ arr[i];
  103.                            
  104.                         }
  105.                     }
  106.  
  107.                     if (flag==true)
  108.                     {
  109.                         if (temp != arr[(int)Math.Pow(2, j) - 1])
  110.                         {
  111.                             sum += (int)Math.Pow(2, j);
  112.                         }
  113.                     }
  114.                 }
  115.  
  116.                 return sum;
  117.             }
  118.  
  119.             void add_err()
  120.             {
  121.                 Random rand = new Random();
  122.                 int i = rand.Next(arr.Length);
  123.                 arr[i] = arr[i] ^ 1;
  124.             }
  125.  
  126.             void decoding()
  127.             {
  128.                 int err = count(true);
  129.                 Console.WriteLine("Ошибка в {0} бите:", err);
  130.                 output1(err-1);
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement