//******************************************************************
// TestTowers program
// This program, a test driver for the DoTowers function, reads in
// a value from standard input and passes this value to DoTowers
//******************************************************************
#include
#include // For setw()\
using namespace std;
void DoTowers( int, int, int, int );
int main()
{
int circleCount; // Number of circles on starting peg
cout << "Input number of circles: ";
cin >> circleCount;
cout << "OUTPUT WITH " << circleCount << " CIRCLES" << endl
<< endl;
cout << "CALLED FROM #CIRCLES" << setw(8) << "BEGIN"
<< setw(8) << "AUXIL." << setw(5) << "END"
<< " INSTRUCTIONS" << endl
<< endl;
cout << "Original :";
DoTowers(circleCount, 1, 2, 3);
return 0;
}
//******************************************************************
void DoTowers(
/* in */ int circleCount, // Number of circles to move
/* in */ int beginPeg, // Peg containing circles to move
/* in */ int auxPeg, // Peg holding circles temporarily
/* in */ int endPeg ) // Peg receiving circles being moved
// This recursive function moves circleCount circles from beginPeg
// to endPeg. All but one of the circles are moved from beginPeg
// to auxPeg, then the last circle is moved from beginPeg to endPeg,
// and then the circles are moved from auxPeg to endPeg.
// The subgoals of moving circles to and from auxPeg are what
// involve recursion
// Precondition:
// All parameters are assigned && circleCount >= 0
// Postcondition:
// The values of all parameters have been printed
// && IF circleCount > 0
// circleCount circles have been moved from beginPeg to
// endPeg in the manner detailed above
// ELSE
// No further actions have taken place
{
cout << setw(6) << circleCount << setw(9) << beginPeg
<< setw(7) << auxPeg << setw(7) << endPeg << endl;
if (circleCount > 0)
{
cout << "From first:";
DoTowers(circleCount - 1, beginPeg, endPeg, auxPeg);
cout << setw(58) << "Move circle " << circleCount
<< " from " << beginPeg << " to " << endPeg << endl;
cout << "From second:";
DoTowers(circleCount - 1, auxPeg, beginPeg, endPeg);
}
}