hanoi_tower.c (715B)
1 #include <stdio.h> 2 3 void moves(int, char, char, char); 4 5 int main(int argc, char **argv) 6 { 7 int numDisks; 8 9 printf("\t\tTower of Hanoi\n\n"); 10 11 do 12 { 13 printf("Number of disks: "); 14 scanf("%d", &numDisks); 15 } while (numDisks <= 0); 16 17 printf("\n"); 18 moves(numDisks, 'A', 'B', 'C'); 19 20 return 0; 21 } 22 23 void moves(int numDisks, char tower1, char tower2, char tower3) 24 { 25 if (numDisks == 1) 26 printf("Move disk 1 from tower %c to tower %c\n", tower1, tower3); 27 else 28 { 29 moves(numDisks - 1, tower1, tower3, tower2); 30 printf("Move disk %d from tower %c to tower %c\n", numDisks, tower1, tower3); 31 moves(numDisks - 1, tower2, tower1, tower3); 32 } 33 }