Exemplo n.º 1
0
        //"Входной" метод класса
        static public ResultOfCounting Calculate(string input)
        {
            input = input.Replace('.', ',');

            //Преобразовываем выражение в постфиксную запись
            string output = GetExpression(input);

            //Решаем полученное выражение
            ResultOfCounting result = Counting(output);

            return(result);
        }
        //после вызова происходит проверка на допустимость входных данных
        //резльтат: либо предупреждние, либо заполненные textboxы и picturebox
        private void Calculate()
        {
            if (InitialExpression.Text == string.Empty)
            {
                MessageBox.Show("Input expression!");
            }
            else
            {
                //получаем результат вычисления выражения из textbox
                //(в ResultOfCounting две переменные: вычисленное значение и булевская о возникших исключениях)

                if (CheckInput.IsCorrectBracketsAndOperators(InitialExpression.Text))
                {
                    ResultOfCounting ResultOrException = RpnAlgorithm.Calculate(InitialExpression.Text);
                    if (ResultOrException.IsCorrectInput)
                    {
                        //освобождаем ресурсы
                        if (pictureBoxForTree != null)
                        {
                            pictureBoxForTree.Dispose();
                        }
                        if (g != null)
                        {
                            g.Dispose();
                        }
                        if (bmp != null)
                        {
                            bmp.Dispose();
                        }


                        Result.Text = Math.Round(ResultOrException.Value, 3,
                                                 MidpointRounding.AwayFromZero).ToString();

                        //заменяем символ унарного минуса на обычный
                        RpnExpression.Text = ResultOrException.Expression.Replace('~', '-');
                        TreeNode Tree = TreeNode.Tree(ResultOrException.Expression);


                        Graph.BuildGraphFromTree(Tree);

                        using (var fs = File.Open("graph.Png", FileMode.Open))

                            bmp = new Bitmap(fs);
                        pictureBoxForTree = new PictureBox
                        {
                            Image    = bmp,
                            SizeMode = PictureBoxSizeMode.AutoSize
                        };
                        File.Delete("graph.Png");
                        flowLayoutPanel1.Controls.Add(pictureBoxForTree);
                    }
                    else
                    {
                        MessageBox.Show("Incorrect expression. Please, check number of brackets, operators or operands you use.");
                    }
                }
                else
                {
                    MessageBox.Show("Incorrect expression. Please, check number of brackets, operators or operands you use.");
                }
            }
        }