View Single Post
Old 13-09-2007, 08:50 PM   #127 (permalink)
jal_desai
In The Zone
 
jal_desai's Avatar
 
Join Date: May 2006
Posts: 426
Default Re: Post ur C/C++ Programs Here

hello ppl, check out this program... it simulates the TOWER OF HANOI problem... just give the number of rings as the input and it will give u the TOTAL ALGORITHM about how to transfer all of them from one rod to the other using an intermediate rod...

CODE:

#include<conio.h>
#include<iostream.h>
class tower
{
int nodisk;
char frmtwr,totwr,auxtwr;
public:
void hanoi(int,char,char,char);
};

void tower::hanoi(int nodisk,char frmtwr,char totwr,char auxtwr)
{
if (nodisk==1)
{
cout<<"\nMove disk 1 from tower "<<frmtwr<<" to tower "<<totwr;
return;
}
hanoi(nodisk-1,frmtwr,auxtwr,totwr);
cout<<"\nMove disk "<<nodisk<<" from tower "<<frmtwr<<" to tower "<<totwr;
hanoi(nodisk-1,auxtwr,totwr,frmtwr);
return;
}

void main()
{
int no;
tower ob;
clrscr();
cout<<"\n\t\t\t\t--- Tower Of Hanoi ---\n";
cout<<"\n\t\t\t--- (assuming towers X, Y & Z) ---\n";
cout<<"\n\nEnter the number of disks: ";
cin>>no;
ob.hanoi(no,'X','Y','Z');
cout<<"\n\nPress any key to continue...";
getch();
}


Enjoy...
__________________
TechExplorer.in
jal_desai is offline