/* Towers of Hanoi, Recursion and functions.*/ /* Translated from C to C++ Tue Nov 5 14:45:11 PST 1996 */ #include #include static const int MAXSTACK=13; static const char *empty="------------"; class STACKS{ //3 Stacks of disks //This is a bad first prototype. How many improvements can you make? private: char a[MAXSTACK]; char b[MAXSTACK]; char c[MAXSTACK]; //typical stack: 654321------ public: void set_up(int number) { strcpy(b,empty); strcpy(c,empty); //All the disks are initially on A strcpy(a,empty); for(int place=0, disk=number; disk>0; place++, disk--) a[place]=disk+'0';//converts int to char return; }//set_up char pop(char s[])//remove top disk off s { char result; int place; for(place=0; s[place]!=empty[0]; place++) ; place--; result=s[place]; s[place]=empty[0]; return result; }//pop void push(char d, char s[])//Put disk d on top of stack s { int place; for(place=0; s[place]!=empty[0]; place++) ; s[place]=d; return; }//pop void print() { cout<< "A:"<0){ if(n>1){ move_stack_of(n-1,frompeg, tempeg, topeg); stacks.move_disk_number(n, frompeg, topeg); move_stack_of(n-1,tempeg,topeg,frompeg); } else{ stacks.move_disk_number(1, frompeg, topeg); } } else{ cerr <<" ... bad input data\n"; exit(1); } return; }// move_stack_of main() { int number; cout<<"Tower of Hanoi Puzzle. number of disks? "; cin >>number; if(number>9) { cerr << "Number too big, >9\n"; exit(1); } stacks.set_up(number); move_stack_of(number,'A','B','C'); stacks.print(); return 0; }//main