Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 109,372 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 970 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



MFC Fibonacci Number Tutorial:

 
Reply to this topicStart new topic

> MFC Fibonacci Number Tutorial:, A basic MFC application which calculates the nth number in the Fibonac

captainhampton
Group Icon



post 13 Jul, 2008 - 04:11 PM
Post #1


If you’ve been programming even for a small amount of time, odds are you’ve calculated or at least should be familiar with the Fibonacci Sequence. Rightfully so, this is one of the most important sequences of numbers pertaining to the field of computer science. So maybe you’ve got a console application of something basic to find the sequence and you want to really impress your professor and get some extra credit. Or maybe you just want to further your knowledge on programming and wish to take me up on a GUI driven program to find the nth value in the Fibonacci Sequence. For whatever the reason may be, I will step you through a basic MFC application to do that very thing:

First, start up Microsoft Visual Studio and go to File->New->Project. Make sure to highlight MFC application and enter an appropriate title for this project. I just called it Fibonacci Sequence:

IPB Image

Click OK when you’re done, and a pop window will show now, click next and on the upper left hand corner you should see some radio buttons. Make sure that you have the “Dialog Based” radio button selected and proceed to click “Finish”.

IPB Image

OK now you are presented with a dialog box with some buttons and text. Just delete everything on here so that you have a fresh and clean dialog box like so:

IPB Image

Alright we’re now ready to rock, on the right hand side you will see a “Toolbox” menu with a bunch of little gadgets. For our example we will make use of the “Edit Control”, “Static Text”, “Group Box, and “Button” items. Simply click and drag them over to the dialog box and set them as you like. Here is my example:

IPB Image

Ok, doesn’t have to be perfect just trying to get some type of GUI interface to fuel all of the code behind it. Speaking of which, let’s get down to the actual coding aspect of it all. First things first, we must create variables of which to utilize. Right click on the “Edit Control” box underneath the “nth” static text. Once the menu comes up, click on “Add Variable to get a window like this:

IPB Image

Now to make a few minor adjustments, underneath the “Category:” select “Value”. Under variable type select “double” and finally to name this variable under the “Variable name” field. Let’s call it “nth”. Easy enough right? Hit finish and the variable is created.

IPB Image

Now just do the same thing for the variable under the “Number in Sequence” field, however make sure to give it a unique variable name! I will call it “fib”.

So now we have our variables, but this application still does not do much of anything. Let’s make those lazy buttons at the bottom do something! Double click on the “Clear” button. Once you double click you will be sent to the actual code fueling this. For now it is an empty function definition which we will change. The “Clear” button must do what else but clear, or set the variables to “0”. That is done with the following code.

Fibonacci Sequence Clear Button:
CODE

//Fibonacci Sequence Clear
void CCalculatorDlg::OnBnClickedButton64()
{
    UpdateData(true);
    nth = 0;
    fib = 0;
    UpdateData(false);
}


Be sure to keep the code surrounded by the “UpdateData(true); and UpdateData(false);

Now we need to add the code for the “Calculate” button, which is done as such. This code simply calculate the nth value in the Fibonacci Sequence and sets the appropriate variable we created in the “Edit Box” to display its result. Here is the code:

Fibonacci Sequence Calculate Button:
CODE

//Fibonacci Sequence Calculate
void CCalculatorDlg::OnBnClickedButton65()
{
    UpdateData(true);
    if ( nth != 0 && fib == 0 ){
        unsigned int i = 1, j = 0, k, t;
            for (k = 1; k <= nth; k++){
                t = i + j;
                i = j;
                j = t;
            }//end for
            fib = j;
    }//end if
    UpdateData(false);
}

Alright, so that is basically it. We have created a program to not only calculate the nth value in the Fibonacci Sequence, but to do it in style, so go ahead and try picking up some girls at the bar with your new program and impress them with your new found MFC skills!

IPB Image

Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

gabehabe
Group Icon



post 13 Jul, 2008 - 04:20 PM
Post #2
Not content with a whole bunch of sort tutorials, now you're on to MFC? ohmy.gif

Great tutorials though, keep up the good work icon_up.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 9/6/08 11:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month