Esempio n. 1
0
        private void buttonCalculate_Click(object sender, EventArgs e)
        {
            if (double.TryParse(textBoxA.Text, out double a) && double.TryParse(textBoxB.Text, out a) &&
                double.Parse(textBoxA.Text) < double.Parse(textBoxB.Text) && comboBoxMethods.SelectedIndex > -1)
            {
                List <string> sections      = listBox1.Items.Cast <string>().ToList();
                List <double> times         = new List <double>();
                List <double> parallelTimes = null;
                if (checkBoxParallelMode.Checked)
                {
                    parallelTimes = new List <double>();
                }

                Method   currentMethod   = (Method)comboBoxMethods.SelectedIndex;
                Integral currentIntegral = this.GetInstance(currentMethod);

                currentIntegral.StartValue = int.Parse(textBoxA.Text);
                currentIntegral.EndValue   = int.Parse(textBoxB.Text);

                if (checkBoxLambda.Checked)
                {
                    currentIntegral.Integrand = (x) => { return(2 * x - Math.Log(2 * x) + 234); };
                }
                else
                {
                    currentIntegral.Integrand = this.GetIntegrandValue;
                }

                double    result    = 0.0;
                Stopwatch stopwatch = new Stopwatch();

                foreach (string section in sections)
                {
                    currentIntegral.Steps = Convert.ToInt32(section);

                    stopwatch.Reset();
                    stopwatch.Start();
                    result = currentIntegral.Calculate();
                    stopwatch.Stop();

                    times.Add(stopwatch.ElapsedMilliseconds / 1000.0);

                    if (checkBoxParallelMode.Checked)
                    {
                        stopwatch.Reset();
                        stopwatch.Start();
                        currentIntegral.CalculateAsync();
                        stopwatch.Stop();

                        parallelTimes.Add(stopwatch.ElapsedMilliseconds / 1000.0);
                    }
                }

                labelResult.Text = $"Результат: {result.ToString()}";

                this.chartResult.Series.Clear();
                this.chartResult.Titles.Clear();

                this.chartResult.Titles.Add("Зависимость времени выполнения от количества разбиений.");

                Series series = this.chartResult.Series.Add("Время выполнения");
                series.ChartType = SeriesChartType.Line;

                for (int i = 0; i < times.Count; i++)
                {
                    series.Points.AddXY(sections[i], times[i]);
                }

                if (checkBoxParallelMode.Checked)
                {
                    Series parallelSeries = this.chartResult.Series.Add("Параллельный вариант");
                    parallelSeries.ChartType = SeriesChartType.Line;

                    for (int i = 0; i < parallelTimes.Count; i++)
                    {
                        parallelSeries.Points.AddXY(sections[i], parallelTimes[i]);
                    }
                }
            }
            else
            {
                MessageBox.Show("Параметры A и B заданы не корректно!");
            }
        }
Esempio n. 2
0
        private void buttonCalculate_Click(object sender, EventArgs e)
        {
            if (double.TryParse(textBoxA.Text, out double a) && double.TryParse(textBoxB.Text, out a) &&
                double.Parse(textBoxA.Text) < double.Parse(textBoxB.Text) && comboBoxMethods.SelectedIndex > -1)
            {
                List <string> sections      = listBox1.Items.Cast <string>().ToList();
                List <double> times         = new List <double>();
                List <double> parallelTimes = null;
                if (checkBoxParallelMode.Checked)
                {
                    parallelTimes = new List <double>();
                }


                CalculationType currentMethod;
                switch (comboBoxMethods.SelectedIndex)
                {
                case 0: currentMethod = CalculationType.AverageRectangle; break;

                case 1: currentMethod = CalculationType.Trapezium; break;

                case 2: currentMethod = CalculationType.Simpson; break;

                default: MessageBox.Show("Метод не был выбран!"); return;
                }

                IntegralInputParameters inputParameters = new IntegralInputParameters
                {
                    IntegrandExpression = textBoxFunction.Text,
                    StartValue          = int.Parse(textBoxA.Text),
                    EndValue            = int.Parse(textBoxB.Text),
                    ParameterName       = "x"
                };

                Integral currentIntegral = Integral.GetIntegral(currentMethod, inputParameters);

                double    result    = 0.0;
                Stopwatch stopwatch = new Stopwatch();

                foreach (string section in sections)
                {
                    currentIntegral.IterationsNumber = Convert.ToInt32(section);

                    stopwatch.Reset();
                    stopwatch.Start();
                    result = currentIntegral.Calculate();
                    stopwatch.Stop();

                    times.Add(stopwatch.ElapsedMilliseconds / 1000.0);

                    if (checkBoxParallelMode.Checked)
                    {
                        stopwatch.Reset();
                        stopwatch.Start();
                        currentIntegral.CalculateAsync();
                        stopwatch.Stop();

                        parallelTimes.Add(stopwatch.ElapsedMilliseconds / 1000.0);
                    }
                }

                labelResult.Text = $"Результат: {result.ToString()}";

                this.chartResult.Series.Clear();
                this.chartResult.Titles.Clear();

                this.chartResult.Titles.Add("Зависимость времени выполнения от количества разбиений.");

                Series series = this.chartResult.Series.Add("Время выполнения");
                series.ChartType = SeriesChartType.Line;

                for (int i = 0; i < times.Count; i++)
                {
                    series.Points.AddXY(sections[i], times[i]);
                }

                if (checkBoxParallelMode.Checked)
                {
                    Series parallelSeries = this.chartResult.Series.Add("Параллельный вариант");
                    parallelSeries.ChartType = SeriesChartType.Line;

                    for (int i = 0; i < parallelTimes.Count; i++)
                    {
                        parallelSeries.Points.AddXY(sections[i], parallelTimes[i]);
                    }
                }
            }
            else
            {
                MessageBox.Show("Параметры A и B заданы не корректно!");
            }
        }