/*********************************************************** Author : Yan Huang Date : 02.18.07 Synopsis: recursive hanoi tower, use STL stack *************************************************************/ #include #include #include /* use reference to make sure you are working on the three given stacks in all the recursive function calls */ class Pole{ public: stack elements; string poleName; Pole(string name) { poleName = name;}; }; void hanoi(int discs, Pole & fromPole, Pole & toPole, Pole &aux) { int d; if(discs >= 1) { hanoi(discs-1, fromPole, aux, toPole); d = fromPole.elements.top(); cout << "Move Disk" << d << " from " << fromPole.poleName<< " to " << toPole.poleName<< endl; fromPole.elements.pop(); toPole.elements.push(d); hanoi(discs-1,aux, toPole, fromPole); } } int main(){ Pole fromPole("A"), toPole("C"), aux("B"); int numb; cout << "Please enter the number of disks : "; cin >> numb; for( int i = 0; i < numb; i++ ) { fromPole.elements.push(numb-i); } hanoi(numb, fromPole, toPole, aux); }