コード例 #1
0
        /// <summary>
        /// Кодирует или декодирует сообщение.
        /// </summary>
        /// <param name="sender">CodeWindow</param>
        /// <param name="e">RoutedEventArgs</param>
        private void CodeButton_Click(object sender, RoutedEventArgs e)
        {
            if (flag)
            {
                if (first.Text.Length != AGCode.K)
                {
                    error.Visibility = Visibility.Visible;
                    error.Text       = $"   Длина кодируемого слова должна равняться {AGCode.K}.";
                    return;
                }
                try
                {
                    int[] ourMessage     = MyStatics.ToIntArray(first.Text, AGCode.K);
                    int[] ourCodeMessage = AGCode.Encode(ourMessage);
                    second.Text = "";

                    for (int i = 0; i < ourCodeMessage.Length; i++)
                    {
                        second.Text += ourCodeMessage[i];
                    }
                }
                catch (Exception ex)
                {
                    error.Visibility = Visibility.Visible;
                    error.Text       = ex.Message;
                }
            }
            else
            {
                if (first.Text.Length != AGCode.N)
                {
                    error.Visibility = Visibility.Visible;
                    error.Text       = $"Длина кодового слова должна равняться {AGCode.N}.";
                    return;
                }


                try
                {
                    int[] ourDecodedMessage;
                    int[] ourMessage = MyStatics.ToIntArray(first.Text, AGCode.N);
                    ourDecodedMessage = AGCode.Decode(ourMessage);
                    second.Text       = "";
                    for (int i = 0; i < ourDecodedMessage.Length; i++)
                    {
                        second.Text += ourDecodedMessage[i];
                    }
                }
                catch (MistakesNumberException ex)
                {
                    error.Visibility = Visibility.Visible;
                    error.Text       = ex.Message;
                    return;
                }
                catch (Exception ex)
                {
                    error.Visibility = Visibility.Visible;
                    error.Text       = ex.Message;
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Генерирует алгеброгеометрический код.
        /// </summary>
        /// <param name="sender">CodeGeneratingWindow</param>
        /// <param name="e">RoutedEventArgs</param>
        private void NextWindowButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                t.Content = "";
                if (groupSize == 0)
                {
                    throw new ArgumentNullException("количество переменных", "Невведено количество переменных!");
                }

                /*
                 *              for (int i = 0; i < equations.Document.Blocks.Count; i++)
                 *              {
                 *                  equations.Document.Blocks.ElementAt<Block>(i).Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
                 *              }
                 */
                foreach (Block block in equations.Document.Blocks)
                {
                    block.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
                }

                #region Формирование системы уравнений в памяти
                line  = new TextRange(equations.Document.ContentStart, equations.Document.ContentEnd).Text;
                lines = line.Split('\n');

                for (int i = 0; i < lines.Length; i++)
                {
                    try
                    {
                        if (i != lines.Length - 1)
                        {
                            lines[i] = lines[i].Remove(lines[i].Length - 1, 1);
                        }
                        MyStatics.Reading(lines[i], groupSize, systemOfEquations, i);
                    }
                    catch (TokenizerException ex)
                    {
                        t.Content += (i + 1) + " уравнение: " + ex.Message + "\n";
                        equations.Document.Blocks.ElementAt <Block>(ex.Index).Foreground = new SolidColorBrush(Color.FromArgb(255, 200, 21, 21));
                        flag = false;
                    }
                    catch (ParserException ex)
                    {
                        t.Content += (i + 1) + " уравнение: " + ex.Message + "\n";
                        equations.Document.Blocks.ElementAt <Block>(ex.Index).Foreground = new SolidColorBrush(Color.FromArgb(255, 200, 21, 21));
                        flag = false;
                    }
                    catch (UnknownCodeMessageException ex)
                    {
                        t.Content += (i + 1) + " уравнение: " + ex.Message + "\n";
                        equations.Document.Blocks.ElementAt <Block>(ex.Index).Foreground = new SolidColorBrush(Color.FromArgb(255, 200, 21, 21));
                        flag = false;
                    }
                }
                #endregion

                if (flag)
                {
                    #region  ешение системы уравнений
                    theAnswer = solver.Solve(systemOfEquations);
                    if (theAnswer.Matrix.Count == 0)
                    {
                        throw new NullReferenceException("Система уравнений не имеет решений!");
                    }
                    #endregion

                    #region Формирование кода и вывод на экран его основных параметров
                    AGCode = new Code(theAnswer, lines);

                    CodeDescriptionWindow win = new CodeDescriptionWindow(AGCode);
                    win.Top  = this.Top;
                    win.Left = this.Left;
                    win.Show();
                    this.Close();
                    #endregion
                }
                flag = true;
            }
            catch (CodeGeneratingException ex)
            {
                t.Content = ex.Message;
                foreach (Block block in equations.Document.Blocks)
                {
                    block.Foreground = new SolidColorBrush(Color.FromArgb(255, 200, 21, 21));
                }
            }
            catch (ArgumentNullException)
            {
                size.IsDropDownOpen = true;
            }
            catch (NullReferenceException ex)
            {
                t.Content += ex.Message;
                foreach (Block block in equations.Document.Blocks)
                {
                    block.Foreground = new SolidColorBrush(Color.FromArgb(255, 200, 21, 21));
                }
            }
            catch (TokenizerException ex)
            {
                t.Content += ex.Message + "\n";
                equations.Document.Blocks.ElementAt(ex.Index).Foreground = new SolidColorBrush(Color.FromArgb(255, 200, 21, 21));
            }
            catch (ParserException ex)
            {
                t.Content += ex.Message + "\n";
                equations.Document.Blocks.ElementAt(ex.Index).Foreground = new SolidColorBrush(Color.FromArgb(255, 200, 21, 21));
            }
            catch (UnknownCodeMessageException ex)
            {
                t.Content += ex.Message + "\n";
                equations.Document.Blocks.ElementAt(ex.Index).Foreground = new SolidColorBrush(Color.FromArgb(255, 200, 21, 21));
            }
            finally
            {
                systemOfEquations = new List <Equation>();
            }
        }