Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Jackson
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Введите длину слова");
- int N= Convert.ToInt32(Console.ReadLine());
- int deg = (int)Math.Round(Math.Log(N, 2));
- int?[] arr = new int?[N + 1 + Convert.ToInt32(deg)];
- input();
- count(false);
- Console.WriteLine("Слово c {0} дополнительными битами:", deg+1);
- output();
- Console.WriteLine("Cлово с ошибкой:");
- add_err();
- output();
- decoding();
- Console.ReadKey();
- void input()
- {
- Console.WriteLine("Введите слово");
- for(int j = 0; j < deg+1; j++)
- {
- arr[(int)Math.Pow(2, j)-1] = 0;
- }
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] == 0)
- continue;
- arr[i] = Convert.ToInt32(Console.ReadLine());
- }
- Console.WriteLine("\n");
- }
- void output()
- {
- for (int i =0 ,j = 0; i < arr.Length; i++)
- {
- if ((i == (int)Math.Pow(2, j) - 1)&&(j<deg+1))
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write(arr[i]);
- j++;
- Console.ResetColor();
- continue;
- }
- Console.Write(arr[i]);
- }
- Console.Write("\n");
- Console.WriteLine("\n");
- }
- void output1(int ind)
- {
- for (int i = 0, j = 0; i < arr.Length; i++)
- {
- if (i==ind)
- {
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write(arr[i]);
- Console.ResetColor();
- continue;
- }
- if ((i == (int)Math.Pow(2, j) - 1) && (j < deg + 1))
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write(arr[i]);
- j++;
- Console.ResetColor();
- continue;
- }
- Console.Write(arr[i]);
- }
- Console.Write("\n");
- Console.WriteLine("\n");
- }
- int count(bool flag)
- {
- int sum=0;
- for (int j = 0; j < deg + 1; j++)
- {
- int? temp = arr[(int)Math.Pow(2, j) - 1];
- for (int z = (int)Math.Pow(2, j); z <= arr.Length; z += (2 * (int)Math.Pow(2, j)))
- {
- for (int i = z - 1; (i < (z - 1 + (int)Math.Pow(2, j)))&&(i< arr.Length); i++)
- {
- arr[(int)Math.Pow(2, j) - 1] = arr[(int)Math.Pow(2, j) - 1] ^ arr[i];
- }
- }
- if (flag==true)
- {
- if (temp != arr[(int)Math.Pow(2, j) - 1])
- {
- sum += (int)Math.Pow(2, j);
- }
- }
- }
- return sum;
- }
- void add_err()
- {
- Random rand = new Random();
- int i = rand.Next(arr.Length);
- arr[i] = arr[i] ^ 1;
- }
- void decoding()
- {
- int err = count(true);
- Console.WriteLine("Ошибка в {0} бите:", err);
- output1(err-1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement