Exemplo n.º 1
0
        private void UpdateUIHamming()
        {
            var len = (int)nudInfWordLength.Value;

            rtbOriginalBits.Text = string.Join
                                   (
                _splitter,
                rtbIncoming.Text
                .GetBitsString()
                .Split(len)
                                   );
            rtbWithAdditionalBits.Text = string.Join
                                         (
                _splitter,
                rtbIncoming.Text
                .GetHammingCode((int)nudInfWordLength.Value)
                .Split(len + HammingCode.GetKeysCount(len))
                                         );
        }
Exemplo n.º 2
0
        private void SetAddingData()
        {
            dgvAddingData.Rows.Clear();
            dgvAddingData.Columns.Clear();

            var code = _infWord.InsertControlBits();

            for (int i = 0; i < code.Length; i++)
            {
                dgvAddingData.Columns.Add("", "");
            }

            var index = dgvAddingData.Rows.Add();

            for (int i = 0; i < code.Length; i++)
            {
                dgvAddingData.Rows[index].Cells[i].Value = code[i];
            }

            for (int i = 0; i < HammingCode.GetKeysCount(_infWord.Length); i++)
            {
                var k = (1 << i) - 1;

                index = dgvAddingData.Rows.Add();

                for (int j = 0; j < code.Length; j++)
                {
                    if (j >= k && (j - k) / (k + 1) % 2 == 0)
                    {
                        dgvAddingData.Rows[index].Cells[j].Value = '*';
                    }
                }
            }

            code  = code.CalcControlBits();
            index = dgvAddingData.Rows.Add();

            for (int i = 0; i < code.Length; i++)
            {
                dgvAddingData.Rows[index].Cells[i].Value = code[i];
            }
        }
Exemplo n.º 3
0
        private void UpdateErrorCheck()
        {
            dgvErrorCheck.Rows.Clear();
            dgvErrorCheck.Columns.Clear();

            var errStyle = new DataGridViewCellStyle
            {
                Font      = new Font(SystemFonts.DefaultFont, FontStyle.Bold),
                ForeColor = Color.WhiteSmoke,
                BackColor = Color.Red
            };

            var keyStyle = new DataGridViewCellStyle
            {
                Font      = new Font(SystemFonts.DefaultFont, FontStyle.Bold),
                ForeColor = Color.WhiteSmoke,
                BackColor = Color.DarkRed
            };

            var code = _infWord.InsertControlBits().CalcControlBits();

            for (int i = 0; i < code.Length; i++)
            {
                dgvErrorCheck.Columns.Add("", $"{i+1}");
            }

            var index = dgvErrorCheck.Rows.Add();

            for (int i = 0; i < code.Length; i++)
            {
                dgvErrorCheck.Rows[index].Cells[i].Value = code[i];
            }

            var codeWithError = new StringBuilder(code);
            var keys          = new List <int>();
            var rand          = new Random();
            int errIndex;

            for (int i = 0; i < HammingCode.GetKeysCount(_infWord.Length); i++)
            {
                keys.Add((1 << i) - 1);
            }

            do
            {
                errIndex = rand.Next(codeWithError.Length);
            } while (keys.Contains(errIndex));

            codeWithError[errIndex] = codeWithError[errIndex].Equals('0') ? '1' : '0';

            index = dgvErrorCheck.Rows.Add();

            for (int i = 0; i < codeWithError.Length; i++)
            {
                dgvErrorCheck.Rows[index].Cells[i].Value = codeWithError[i];
            }

            dgvErrorCheck.Rows[index].Cells[errIndex].Style = errStyle;

            codeWithError = new StringBuilder(codeWithError.ToString().CalcControlBits());

            index = dgvErrorCheck.Rows.Add();

            for (int i = 0; i < codeWithError.Length; i++)
            {
                dgvErrorCheck.Rows[index].Cells[i].Value = codeWithError[i];
            }

            index = keys
                    .Where(key => !code[key].Equals(codeWithError[key]))
                    .Sum(key =>
            {
                dgvErrorCheck.Rows[0].Cells[key].Style = keyStyle;
                dgvErrorCheck.Rows[2].Cells[key].Style = keyStyle;
                return(key + 1);
            });

            lError.Text = $"Ошибка в {index} бите";
            dgvErrorCheck.ClearSelection();
        }