コード例 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                labelerr.Text = "";
                fx            = comboBoxf.Text;
                if (v() == 0)
                {
                }
                else
                {
                    progressBar1.Visible = true;
                    timer1.Start();
                    Decimal a     = Decimal.Parse(aBox.Text);
                    Decimal b     = Decimal.Parse(bBox.Text);
                    double  tol   = double.Parse(tolBox.Text);
                    int     k_max = int.Parse(k_maxBox.Text);

                    Stopwatch stopWatch = new Stopwatch();

                    stopWatch.Start();
                    ////данные для входного интерфейса.
                    Bisection j    = new Bisection();
                    var       test = j.Calculate(a, b, fx, tol, k_max);
                    //  obj.GoldenIterationMax(a, a, b, tol, k_max, fx);//метод вычисления
                    stopWatch.Stop();

                    TimeSpan ts = stopWatch.Elapsed;

                    if (test.err != "")
                    {
                        MessageBox.Show(test.err);
                    }
                    else
                    {
                        ms.Text           = ts.TotalSeconds.ToString("0.0");
                        fx1outBox.Text    = test.fx.ToString(" 0e0");
                        x1uotFBox.Text    = test.X.ToString();
                        outTolBox.Text    = test.Abc.ToString("0e0");
                        countinerBox.Text = test.iteration.ToString();


                        if (test.iteration == k_max && test.Abc > (decimal)tol)
                        {
                            labelerr.Text = "Решение с данной точностью за K_Max \n не удалось найти.";
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Не удалось распознать F. " + ex.Message);
            }
        }
コード例 #2
0
        private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                labelerr.Text = "";
                _fx           = comboBoxf.Text;
                if (v() != 0)
                {
                    var model = new BisectionModel
                    {
                        Func         = _fx,
                        Tol          = double.Parse(tolBox.Text),
                        IterationMax = int.Parse(k_maxBox.Text),
                        PointA       = decimal.Parse(aBox.Text),
                        PointB       = decimal.Parse(bBox.Text)
                    };

                    progressBar1.Visible = true;
                    progressBar1.Maximum = model.IterationMax;
                    progressBar1.Value   = 0;

                    var stopWatch = new Stopwatch();
                    stopWatch.Start();

                    var bisection = new Bisection();

                    bisection.ProgressBarIncrement += ProgressBarIncrement;

                    var result = await Task.Run(() => bisection.Calculate(model));

                    stopWatch.Stop();
                    var ts = stopWatch.Elapsed;

                    if (!string.IsNullOrWhiteSpace(result.Error))
                    {
                        MessageBox.Show(result.Error);
                    }
                    else
                    {
                        Sec.Text          = ts.TotalSeconds.ToString("0.0");
                        fx1outBox.Text    = result.Fx.ToString(" 0e0");
                        x1uotFBox.Text    = result.X.ToString(CultureInfo.InvariantCulture);
                        outTolBox.Text    = result.Abc.ToString("0e0");
                        countinerBox.Text = result.Iteration.ToString();

                        if (result.Iteration == model.IterationMax && result.Abc > (decimal)model.Tol)
                        {
                            labelerr.Text = @"Решение с заданной точностью \n за K_Max(" + model.IterationMax +
                                            @")итераций не удалось найти.";
                        }
                    }

                    bisection.ProgressBarIncrement -= ProgressBarIncrement;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(@"Не удалось распознать F. " + ex.Message);
            }
        }