Fibonacci数列的计算

msdn中在演练BackgroundWorker的时候,使用递归计算Fibonacci数列的f(n)。

        private void startAsyncButton_Click(object sender, EventArgs e)
        {
            //清空
            resultLabel.Text = String.Empty;
            //设置状态

        private int numberToCompute = 0;
        private int highestPercentageReached = 0;

            this.numericUpDown1.Enabled = true;//numericUpDown1为读入需要计算的值
            this.startAsyncButton.Enabled = false;//开始计算按钮
            this.cancelAsyncButton.Enabled = true;//取消按钮

            numberToCompute =(int) numericUpDown1.Value;
            // Reset the variable for percentage tracking.
            highestPercentageReached = 0;

            // Start the asynchronous operation.
            backgroundWorker1.RunWorkerAsync(numberToCompute);

        }

        private void cancelAsyncButton_Click(object sender, EventArgs e)
        {
            // Cancel the asynchronous operation.
            this.backgroundWorker1.CancelAsync();

            // Disable the Cancel button.
            this.cancelAsyncButton.Enabled = false;
            this.startAsyncButton.Enabled = true; ;

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            // Get the BackgroundWorker that raised this event.
            BackgroundWorker worker = sender as BackgroundWorker;
            e.Result = ComputeFibonacci((int)e.Argument, worker, e);

        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

           this.progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // First, handle the case where an exception was thrown.
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
 
                resultLabel.Text = "Canceled";
            }
            else
            {
                // Finally, handle the case where the operation
                // succeeded.
                resultLabel.Text = e.Result.ToString();
            }
            this.numericUpDown1.Enabled = true;
            startAsyncButton.Enabled = true;
            cancelAsyncButton.Enabled = false;

        }

        // This is the method that does the actual work. For this
        // example, it computes a Fibonacci number and
        // reports progress as it does its work.
        long ComputeFibonacci(int n, BackgroundWorker worker, DoWorkEventArgs e)
        {
            // The parameter n must be >= 0 and <= 91.
            // Fib(n), with n > 91, overflows a long.
            if ((n < 0) || (n > 91))
            {
                throw new ArgumentException(
                    "value must be >= 0 and <= 91", "n");
            }

            long result = 0;

            if (worker.CancellationPending)
            {
                e.Cancel = true;
            }
            else
            {
                if (n < 2)
                {
                    result = 1;
                }
                else
                {
                    result = ComputeFibonacci(n - 1, worker, e) +
                             ComputeFibonacci(n - 2, worker, e);
                }

                // Report progress as a percentage of the total task.
                int percentComplete =
                    (int)((float)n / (float)numberToCompute * 100);
                if (percentComplete > highestPercentageReached)
                {
                    highestPercentageReached = percentComplete;
                    worker.ReportProgress(percentComplete);
                }
            }

            return result;
        }

 

//

//当然这是为了演示BackgroundWorker,其实Fibonacci数列可以如下这样计算:

        private void FibonacciForm_Load(object sender, EventArgs e)
        {
            //textBox1.Text = "斐波纳契数列(一种整数数列, 其中每数等于前面两数之和)。"
            //              + "该数列为 1、1、2、3、5、8、........"
            //               + "如果你想知道该数列的第n个数,请在下面输入n-1";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            numberToCompute = (int)numericUpDown1.Value;
            double tem = Fibonacci_2(numberToCompute);
            label1.Text = tem.ToString();
        }
        private double Fibonacci_2(int n)
        {
            if (n < 2)
                return 1;
            else
              return (Math.Pow((Math.Sqrt(5) + 1)/ 2.0, n)  - Math.Pow((1 - Math.Sqrt(5))/ 2.0, n) ) / Math.Sqrt(5);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值