|
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/
|