Page 1 of 1

MFC Fibonacci Number Tutorial: A basic MFC application which calculates the nth number in the Fibonac Rate Topic: -----

#1 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Posted 13 July 2008 - 04:11 PM

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:

Posted 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”.

Posted 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:

Posted 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:

Posted 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:

Posted 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.

Posted 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:
//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:
//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!

Posted Image



Is This A Good Question/Topic? 0
  • +

Replies To: MFC Fibonacci Number Tutorial:

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Posted 13 July 2008 - 04:20 PM

Not content with a whole bunch of sort tutorials, now you're on to MFC? :o

Great tutorials though, keep up the good work :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1