/* Test Towers of Hanoi */ #include #include // Prototype Statements void towers (int n, char source, char dest, char auxiliary); int main (void) { // Local Definitions int numDisks; // Statements cout << "Please enter number of disks: "; cin >> numDisks; cout << "Start Towers of Hanoi\n\n"; towers (numDisks, 'A', 'C', 'B'); cout << "\nI hope you didn't select 64 " << "and end the world!\n"; return 0; } // main /* ==================== towers ==================== Move one disk from source to destination through the use of recursion. Pre the tower consists of n disks source, destination, and auxiliary towers given Post steps for moves printed */ void towers (int n, char source, char dest, char auxiliary) { // Local Definitions static int step = 0; // Statements cout << "Towers (" << n << ", " << source << ", " << dest << ", " << auxiliary << ")" << endl; if (n == 1) cout << "\t=step " << setw(3) << ++step << ": Move from " << source << " to " << dest << endl; else { towers (n - 1, source, auxiliary, dest); cout << "\ \tStep " << setw(3) << ++step << ": Move from " << source << " to " << dest << endl; towers (n - 1, auxiliary, dest, source); } // if else return; } // towers /* Results: Please enter number of disks: 3 Start Towers of Hanoi Towers (3, A, C, B) Towers (2, A, B, C) Towers (1, A, C, B) Step 1: Move from A to C Step 2: Move from A to B Towers (1, C, B, A) Step 3: Move from C to B Step 4: Move from A to C Towers (2, B, C, A) Towers (1, B, A, C) Step 5: Move from B to A Step 6: Move from B to C Towers (1, A, C, B) Step 7: Move from A to C I hope you didn't select 64 and end the world! */