|
From: Jason S. <mys...@op...> - 2002-01-23 01:20:34
|
Let's try this again. This time I'll actually send the email to the group. *sigh* =)
-------
Just did this for an extra credit project, actually. :)
I'll include the full source code here....don't just copy, please. It actually did take some work to do. :)
Jason
-------
/* Jay Silverman //
// Lab 19.6 The Towers of Hanoi //
// 12-4-01 */
#include <iostream.h>
#include <fstream.h>
void solveHanoi(const int disks,const int from,const int to,const int use,
int &moves,ofstream &fout, const int choice);
void main(){
// Var. declar.
int disks=0,moves=0,choice=0;
ofstream fout("TowersofHanoi.out");
// Intro text
cout<<"Welcome to Joy's Towers of Hanoi Solver App!\n";
cout<<"--------------------------------------------\n\n";
fout<<"Welcome to Joy's Towers of Hanoi Solver App!\n";
fout<<"--------------------------------------------\n\n";
cout<<" The formula for the minimum number of moves is\n";
cout<<" simple. It is # of moves = (2^disks)-1.\n\n";
fout<<" The formula for the minimum number of moves is\n";
fout<<" simple. It is # of moves = (2^disks)-1.\n\n";
cout<<" How was this derived? Well, one must first know\n";
cout<<" the three things needed done to solve this puzzle.\n\n";
fout<<" How was this derived? Well, one must first know\n";
fout<<" the three things needed done to solve this puzzle.\n\n";
cout<<" Once one knows that, you can use it to create a very\n";
cout<<" simple yet not necessarily easily thought of recursive\n";
cout<<" function, and using that, some thought, and perhaps some\n";
cout<<" playing around, derive the formula.\n\n";
fout<<" Once one knows that, you can use it to create a very\n";
fout<<" simple yet not necessarily easily thought of recursive\n";
fout<<" function, and using that, some thought, and perhaps some\n";
fout<<" playing around, derive the formula.\n\n";
getchar();
cout<<" Mathematical induction is mean. :)\n\n";
fout<<" Mathematical induction is mean. :)\n\n";
getchar();
cout<<" How long will 64 disks take, at one move per two seconds?\n\n";
cout<<" (2^disks)-1 is equal to 18,446,744,073,709,551,615 moves.\n";
cout<<" Multiply that by 2, and you will find out it will take\n";
cout<<" 36,893,488,147,419,103,230 seconds to complete. Not bad! >.<\n\n";
fout<<" How long will 64 disks take, at one move per two seconds?\n\n";
fout<<" (2^disks)-1 is equal to 18,446,744,073,709,551,615 moves.\n";
fout<<" Multiply that by 2, and you will find out it will take\n";
fout<<" 36,893,488,147,419,103,230 seconds to complete. Not bad! >.<\n\n";
getchar();
// Get number of disks, must be 1 or higher...
while(disks<1){
cout<<"Number of disks? ";
fout<<"Number of disks? ";
cin>>disks;
fout<<disks<<endl;
}
// Does the user want to pause after each move?
while(choice!=1 && choice!=2){
cout<<"\nPause after each move? (1) Yes, (2) No? ";
fout<<"\nPause after each move? (1) Yes, (2) No? ";
cin>>choice;
fout<<choice<<endl;
}
// Solve it! :)
solveHanoi(disks,1,2,3,moves,fout,choice);
// How many moves did it take?
cout<<"\n\n** Only "<<moves<<" move(s)! Fun. :) **";
fout<<"\n\n** Only "<<moves<<" move(s)! Fun. :) **";
getchar();
fout.close();
}
// Function which solves the puzzle. Required a better
// understading of the puzzle than I had in fourth
// grade ( :) ) and a bit of playing around to get to work.
void solveHanoi(const int disks,const int from,const int to,const int use,
int &moves,ofstream &fout,const int choice)
{
// No disks? No function.
if (disks==0) return;
// The main recursive loop code, does
// everything while outputting the moves
// and couting the number of moves...
solveHanoi(disks-1,from,use,to,moves,fout,choice);
cout<<"\n Move disk from peg "<<from<<" to peg "<<to<<".";
fout<<"\n Move disk from peg "<<from<<" to peg "<<to<<".\n";
if (choice==1) getchar();
else cout<<endl;
moves++;
solveHanoi(disks-1,use,to,from,moves,fout,choice);
}
----- Original Message -----
From: chromatonus 712
To: dev...@li...
Sent: Tuesday, January 22, 2002 8:16 PM
Subject: [Dev-C++] Tower of Hanoi
Anyone has a simple source code for this (any number of disks)? Thank you!
_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|