示例#1
0
        public override BitNumber Decode(BitNumber codeWord)
        {
            BitNumber result = null;
            if (isPossibleBuildTriangular(codeWord.Length))
            {
                int diagonalSize = getLengthDiagonal(codeWord.Length);
                BitNumber[] triangle = messageToTriangle(codeWord);
                //Создаём исходную матрицу (если не было ошибки)
                BitNumber[] sourceTriange = new BitNumber[triangle.Length - 1];
                Array.Copy(triangle, sourceTriange, sourceTriange.Length);
                for (int i = 0; i < sourceTriange.Length; i++)
                {
                    sourceTriange[i].Length--;
                }

                BitNumber maybeCodeWord = Generate(BitNumber.Split(sourceTriange));
                if (codeWord.Equals(maybeCodeWord))
                {
                    result = BitNumber.Split(sourceTriange);
                }
                else //возникла ошибка
                {

                }
            }
            else
            {
                throw new ImpossibleBuildTriangularException("Impossible build triangular!");
            }

            return result;
        }
示例#2
0
 private void GetNkCodeButton_Click(object sender, RoutedEventArgs e)
 {
     StringBuilder result = new StringBuilder();
     if (messageTextBox.Text != "")
     {
         try
         {
             BitNumber bitNumber = new BitNumber(messageTextBox.Text);
             foreach (NkCode code in generatorsNkCodes)
             {
                 BitNumber b = code.Generate(bitNumber);
                 result.Append(code.GetType().Name + ": " + b + " \r\n");
             }
         }
         catch (Exception ee)
         {
             MessageBox.Show("Сообщение не соответствует формату: только нули и единицы. Или " + ee.Message);
         }
         finally
         {
             resultLabel.Content = result.ToString();
         }
     }
     else
     {
         MessageBox.Show("Пустое сообщение нельзя кодировать");
     }
 }
示例#3
0
        public override BitNumber Decode(BitNumber codeWord)
        {
            BitNumber result = new BitNumber();
            Size oldSize = calculateOptimalSquareSize(codeWord.Length);
            Size newSize = new Size(oldSize.Width - 1, oldSize.Height - 1);

            return result;
        }
示例#4
0
 protected bool checkOnParity(BitNumber vector)
 {
     bool result = false;
     if (getCountOnes(vector) % 2 == 0)
     {
         result = true;
     }
     return result;
 }
示例#5
0
 public override BitNumber Decode(BitNumber codeWord)
 {
     BitNumber result = new BitNumber(codeWord.Length - 1);
     for (int i = 0; i < result.Length; i++)
     {
         result[i] = codeWord[i];
     }
     return result;
 }
示例#6
0
 public override BitNumber Generate(BitNumber message)
 {
     BitNumber result = new BitNumber(message.Length * 2);
     for (int i = 0; i < message.Length; i++)
     {
         result[i * 2]     = message[i];
         result[i * 2 + 1] = message[i];
     }
     return result;
 }
示例#7
0
 protected int getCountOnes(BitNumber vector)
 {
     int result = 0;
     for (int i = 0; i < vector.Length; i++)
     {
         if (vector[i] == true)
         {
             result++;
         }
     }
     return result;
 }
示例#8
0
 public BitNumber Decode(BitNumber message, BitNumber[][] tableAdjacentClasses)
 {
     for (int i = 0; i < tableAdjacentClasses.GetLength(0); i++)
     {
         for (int j = 0; j < tableAdjacentClasses.GetLength(i); j++)
         {
             if (message.Equals(tableAdjacentClasses[i][j]))
             {
                 return message ^ tableAdjacentClasses[i][0];
             }
         }
     }
     return null;
 }
示例#9
0
        public override BitNumber Generate(BitNumber message)
        {
            BitNumber[] triangle = null;
            int diagonalSize;
            try
            {
                diagonalSize = getLengthAddedDiagonal(message.Length) - 1;
                triangle = new BitNumber[diagonalSize + 1]; //+1 на проверку первого столба
            }
            catch (ImpossibleBuildTriangularException)
            {
                throw new ImpossibleBuildTriangularException("Incorrect size message for this method!");
            }

            //Генерация треугольника
            for (int i = 0, start = 0, lengthLine = diagonalSize; i < diagonalSize; lengthLine--, i++)
            {
                triangle[i] = new BitNumber(message, start, start + lengthLine - 1);
                start += lengthLine;
            }

            //Установка проверочного в каждой строке
            for (int i = 0; i < diagonalSize; i++)
            {
                int countOnesByRow = 0;
                for (int j = 0; j > i; j++)
                {
                    if (triangle[j][triangle[i].Length] == true)
                    {
                        countOnesByRow++;
                    }
                }
                bool ckeckedBit = triangle[i].IsParity == (countOnesByRow % 2 == 0);
                triangle[i].PushBack(ckeckedBit);
            }

            //проверка первого столбца
            int onesByFirstRow = 0;
            for (int i = 0; i < triangle.Length - 1; i++)
            {
                if (triangle[i][0] == true)
                {
                    onesByFirstRow++;
                }
            }
            triangle[triangle.Length - 1][0] = (onesByFirstRow % 2 == 0);

            return BitNumber.Split(triangle);
        }
示例#10
0
        public override BitNumber Generate(BitNumber message)
        {
            BitNumber result = new BitNumber(message.Length + 1);
            if (!checkOnParity(message))
            {
                result[message.Length] = true;
            }

            for (int i = 0; i < message.Length; i++)
            {
                result[i] = message[i];
            }

            return result;
        }
示例#11
0
        public BitNumber[][] GenerateTableAdjacentClasses(int n)
        {
            BitNumber[][] table = new BitNumber[n][];
            table[0] = new BitNumber[n];
            for (int i = 0; i < table[0].Length; i++)
            {
                table[0][i] = new BitNumber(i, n);
            }

            for (int j = 1; j < n; j++)
            {
                table[j] = new BitNumber[n];
                table[j][0] = new BitNumber(j, n);
                for (int i = 0; i < table[j].Length; i++)
                {
                    table[j][i] = (new BitNumber(i, n) ^ table[j][0]);
                }
            }
            return table;
        }
示例#12
0
        public BitNumber(BitNumber b, int start, int end)
        {
            if (start > b.Length - 1 || end > b.Length - 1 )
            {
                throw new IndexOutOfRangeException("start or end out of range in Bitnumber Constructor");
            }

            if (start > end)
            {
                int temp = start;
                start = end;
                end = temp;
            }

            source = new BitArray(end - start + 1);
            for (int i = 0; i < source.Length; i++)
            {
                source[i] = b[start + i];
            }
        }
示例#13
0
        public override BitNumber Generate(BitNumber message)
        {
            BitNumber result = new BitNumber();
            Size oldSize = calculateOptimalSquareSize(message.Length);

            //добавление бита чётности по строкам
            BitNumber[] table = new BitNumber[(int)oldSize.Height + 1];
            int start = 0;
            int lengthLine = (int)oldSize.Width;
            int end = lengthLine - 1;
            for (int i = 0; i < table.Length - 1; i++ )
            {
                table[i] = new BitNumber(message, start, end);
                //если чётно, то не нужна ещё одна единица
                bool parityBit = !table[i].IsParity;
                table[i].PushBack(parityBit);
                start = end + 1;
                end += lengthLine;
            }

            //по столбцам
            table[(int)oldSize.Height] = new BitNumber((int)oldSize.Width + 1);
            for (int i = 0; i < table[0].Length; i++)
            {
                int countOnes = 0;
                for (int j = 0; j < table.Length - 1; j++)
                {
                    if (table[j][i] == true)
                    {
                        countOnes++;
                    }
                }
                table[(int)oldSize.Height][i] = countOnes % 2 != 0;
            }
            //на бит в нижнем левом углу до фонаря

            result = BitNumber.Split(table);

            return result;
        }
示例#14
0
        private BitNumber[] messageToTriangle(BitNumber message)
        {
            BitNumber[] triangle = null;
            int diagonalSize;
            try
            {
                diagonalSize = getLengthAddedDiagonal(message.Length) - 1;
                triangle = new BitNumber[diagonalSize];
            }
            catch (ImpossibleBuildTriangularException)
            {
                throw new ImpossibleBuildTriangularException("Incorrect size message for this method!");
            }

            //Генерация треугольника
            for (int i = 0, start = 0, lengthLine = diagonalSize; i < diagonalSize; lengthLine--, i++)
            {
                triangle[i] = new BitNumber(message, start, start + lengthLine - 1);
                start += lengthLine;
            }

            return triangle;
        }
示例#15
0
        /// <summary>
        /// Объединение массива Bitnumber в один Bitnumber
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static BitNumber Split(BitNumber[] array)
        {
            int length = 0;
            foreach (BitNumber b in array)
            {
                length += b.Length;
            }

            BitNumber result = new BitNumber(length);

            int indexInResult = 0;
            foreach (BitNumber b in array)
            {
                for (int i = 0; i < b.Length; i++)
                {
                    result[indexInResult++] = b[i];
                }
            }

            return result;
        }
示例#16
0
 public abstract BitNumber Generate(BitNumber message);
示例#17
0
 public abstract BitNumber Decode(BitNumber codeWord);
示例#18
0
 public BitNumber(BitNumber bitNumber)
 {
     source = new BitArray(bitNumber.source);
 }