Sunday, February 8, 2015

Fibonacci Lab


using System.Windows.Forms;



namespace Zau_Fibonacci_Lab
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //method to return a Fibonacci value
        private ulong Fibonacci(ulong num)
        {
            if (num == 0) return 0;
            if (num == 1) return 1;
            return Fibonacci(num - 1) + Fibonacci(num - 2);
        }

        private void btnGetFibonnciValue_Click(object sender, EventArgs e)
        {
            ulong userInput;
            ulong.TryParse(txtUserInput.Text, out userInput);

            Func<ulong, ulong> function = Fibonacci;

            AsyncCallback callback = FibonacciCallback;
            function.BeginInvoke(userInput, callback, function);
        }

        //delegate to display on RichTextBox
        private delegate void RichTextBoxHandler(string s, RichTextBox rtb);
        //Method to display on RichTextBox
        private void SetText(string s, RichTextBox rtb)
        {
            if (rtb.InvokeRequired)
            {
                //create a delegate object
                RichTextBoxHandler handler = SetText;
                Invoke(handler, s, rtb);
            }
            else
            {
                rtb.AppendText(s + "\n");
                //scroll down
                rtb.ScrollToCaret();
            }
        }

        private void FibonacciCallback(IAsyncResult ar)
        {
            //retreive the function delegate
            Func<ulong, ulong> function = (Func<ulong, ulong>)ar.AsyncState;
            //use it to get the return value
            ulong fib = function.EndInvoke(ar);
            string s = "Your fibonnacci value is: " + fib.ToString();
            SetText(s, richTextBox1);
        }

        //========================================================
        //method to series fibonnaci values 1 to 45
        public void fib1to45()
        {
            int a = -1, b = 1, sum = 0;
            for (int i = 0; i <= 45; i++)
            {
                sum = a + b;
                a = b;
                b = sum;
                SetText("fib(" + i + ") = " + sum, richTextBox1);
            }
        }

        private void btnGetFibonnaciValue1to45_Click(object sender, EventArgs e)
        {
            Action fibValue = fib1to45;
            fibValue.BeginInvoke(null, null);
        }
    }
}
//Lab Assignment: AsynchronousCalls --Due Friday 2-6
        //Create New Project
        //define a method Fibonacci that takes a value and returns a value. 
        //you could choose int, uint, long,ulong
        //or even double. Just make sure that you pass to it only integers
        //private ulong Fibonacci(ulong num)
        //run the fibonacci series method asynchronously
        //be aware for num > 45 or 50 it may takes over a minute to run, depending on 
        //computer speed.
        //Add a button that reads a value from user and displays its fibonacci value
        //Add a second button that sequence (for loop) thru a list of input values 1 to 45
        //and display the fibonacci of each value.
        //the display should look like  Fib(inputvalue) = value (on each line) in a richTextbox

No comments:

Post a Comment