コード例 #1
0
        private void SolveByFormula(int n)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            double sqrt5 = Math.Sqrt(5);
            double phi   = (sqrt5 + 1) / 2;

            Result.Items.Add((long)(Math.Pow(phi, n) / sqrt5 + 0.5));

            stopwatch.Stop();
            TimeForm form = new TimeForm(stopwatch.ElapsedMilliseconds.ToString());

            form.ShowDialog();
        }
コード例 #2
0
        private void RecursivelyFiboAsync(int n)
        {
            cts.Cancel();
            cts = new CancellationTokenSource();
            CancellationToken token = cts.Token;

            stopWatch.Start();
            decimal result = RecursivelyFibo(n, token);

            Result.Items.Add(result);
            stopWatch.Stop();

            TimeForm form = new TimeForm(stopWatch.ElapsedMilliseconds.ToString());

            form.ShowDialog();
        }
コード例 #3
0
        private async void VisualSolutionButton_Click(object sender, EventArgs e)
        {
            Result.Visible = false;
            cts.Cancel();

            try
            {
                len = Convert.ToInt32(NumbersOfTowers.Text);
                if (len < 0)
                {
                    TimeForm form = new TimeForm("Введите корректное количество башен");
                    form.ShowDialog();
                    return;
                }
                int source      = Convert.ToInt32(Source.Text);
                int destination = Convert.ToInt32(Destination.Text);

                Remove();
                panel1.Invalidate();

                cts = new CancellationTokenSource();
                Result.Items.Clear();
                Thread.Sleep(500);

                Build(source, len);

                panel1.Invalidate();

                CancellationToken token = cts.Token;

                if (source == destination)
                {
                    return;
                }

                await Task.Run(() => VisualHanoy(len, source, destination, token));
            }
            catch
            {
                TimeForm form = new TimeForm("Введите корректное количество башен");
                form.ShowDialog();
            }
        }
コード例 #4
0
        private void SolveIteratively(int n)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            decimal[] fib = new decimal[n];
            fib[0] = 1;
            fib[1] = 1;
            for (int i = 2; i < n; i++)
            {
                fib[i] = fib[i - 1] + fib[i - 2];
            }
            Result.Items.Add(fib[n - 1]);
            stopwatch.Stop();

            TimeForm form = new TimeForm(stopwatch.ElapsedMilliseconds.ToString());

            form.ShowDialog();
        }
コード例 #5
0
        private void HanoyAsync()
        {
            cts.Cancel();
            cts = new CancellationTokenSource();
            Result.Items.Clear();

            int n;

            try
            {
                n = Convert.ToInt32(NumbersOfTowers.Text);
                if (n < 0)
                {
                    Result.Items.Add("Введите корректное число");
                    return;
                }
                int source      = Convert.ToInt32(Source.Text);
                int destination = Convert.ToInt32(Destination.Text);

                if (source == destination)
                {
                    Result.Items.Add("Башня уже на месте");
                    return;
                }

                CancellationToken token = cts.Token;

                stopWatch.Start();
                Hanoy(n, source, destination, token);
                stopWatch.Stop();

                TimeForm form = new TimeForm(stopWatch.ElapsedMilliseconds.ToString());
                form.ShowDialog();
            }
            catch
            {
                Result.Items.Add("Введите корректное число");
            }
        }