|
From: chromatonus 7. <chr...@ho...> - 2002-01-23 01:16:23
|
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 |
|
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
|
|
From: Ioannis V. <no...@ya...> - 2002-01-23 02:07:54
|
That was cool but you had ommitted stdio.h for getchar().
I converted your code to be valid ANSI C++ (you had coded in preANSI
style):
/* Jay Silverman //
// Lab 19.6 The Towers of Hanoi //
// 12-4-01 */
#include <iostream>
#include <fstream>
#include <cstdio>
void solveHanoi(const int disks,const int from,const int to,const int
use,
int &moves, std::ofstream &fout, const int choice);
int main(){
using namespace std;
// 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, std::ofstream &fout,const int choice)
{
using namespace std;
// 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);
}
I just made the conversion haven't checked how the code works.
Ioannis
* Ioannis Vranos
* Programming pages: <http://www.noicys.d2g.com/>
http://www.noicys.d2g.com
* Alternative URL: <http://run.to/noicys> http://run.to/noicys
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Jason
Silverman
Sent: Wednesday, January 23, 2002 3:20 AM
To: dev...@li...
Subject: Re: [Dev-C++] Tower of Hanoi
Let's try this again. This time I'll actually send the email to the
group. *sigh* =)
|
|
From: <tho...@ya...> - 2002-01-24 02:59:52
|
hi Jay, hey Ioannis,
i executed the hanoi code in the ioannis ANSI version
and it runs on windowNT perfectly.
but i compiled it with borland.
thomas
--- Ioannis Vranos <no...@ya...> ha scritto: >
That was cool but you had ommitted stdio.h for
> getchar().
>
> I converted your code to be valid ANSI C++ (you had
> coded in preANSI
> style):
>
>
> /* Jay Silverman //
> // Lab 19.6 The Towers of Hanoi //
> // 12-4-01 */
>
>
> #include <iostream>
> #include <fstream>
> #include <cstdio>
>
> void solveHanoi(const int disks,const int from,const
> int to,const int
> use,
> int &moves, std::ofstream &fout, const int
> choice);
>
> int main(){
>
> using namespace std;
>
> // 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, std::ofstream &fout,const int choice)
> {
> using namespace std;
>
> // 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++;
>
>
=== message truncated ===
______________________________________________________________________
Dillo con una cartolina!
http://it.greetings.yahoo.com/
|
|
From: Evandro L. P. Da S. <eva...@di...> - 2003-12-30 03:14:14
|
That was cool but you had ommitted stdio.h for getchar().
I converted your code to be valid ANSI C++ (you had coded in preANSI
style):
/* Jay Silverman //
// Lab 19.6 The Towers of Hanoi //
// 12-4-01 */
#include <iostream>
#include <fstream>
#include <cstdio>
void solveHanoi(const int disks,const int from,const int to,const int
use,
int &moves, std::ofstream &fout, const int choice);
int main(){
using namespace std;
// 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, std::ofstream &fout,const int choice)
{
using namespace std;
// 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);
}
I just made the conversion haven't checked how the code works.
Ioannis
* Ioannis Vranos
* Programming pages: <http://www.noicys.d2g.com/>
http://www.noicys.d2g.com
* Alternative URL: <http://run.to/noicys> http://run.to/noicys
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Jason
Silverman
Sent: Wednesday, January 23, 2002 3:20 AM
To: dev...@li...
Subject: Re: [Dev-C++] Tower of Hanoi
Let's try this again. This time I'll actually send the email to the
group. *sigh* =)
|
|
From: Luigi S. <gi...@li...> - 2002-01-23 16:26:04
Attachments:
hanoi.tgz
|
Some time ago I was asked to solve this: Towers of Hanoi: given a stack of disks, threaded onto one peg and arranged from bottom to top in decreasing size, move the stack to a second peg under the following two conditions: - one disk is moved at a time - at no time may a larger disk be placed above a smaller one A third peg is available for temporarily holding disks. If your problem is similar to the one above, one solution could be using "induction" as suggested by Mr. Silverman: if you have one disk, move it and you are over; when there is more than a disk, say n, move the n - 1 small ones on the temporary peg, then place the n-th (the biggest) on the destination peg, finally move the small ones from the temporary peg to the destination one. I've attached an archive with two directories, one has my very simple (original) C solution and the other has a C++ version of it. Let me know if you find them of some help. At 01:16 23/01/02 +0000, chromatonus 712 wrote: >Anyone has a simple source code for this (any number of disks)? Thank you! -- Luigi gi...@li... |