Sunday, July 12, 2009

Convert C++ to C - Hanoi Tower?

So I need to convert this into C, please help me. This means no using iostream or count or cin, instead it has to be printf, GetInteger(), etc. Help me!! Here is the program:


#include %26lt;iostream%26gt;


using namespace std;


void Hanoi(int n,char A,char B,char C);


double moves (int n);


int main ()


{


int n=0;


char ch;


char A='A', B='B', C='C';


do{


cout%26lt;%26lt;"\nEnter number of disks --%26gt; ";


cin %26gt;%26gt; n;


if(n%26lt;=0)break;


Hanoi(n,A,B,C);


cout%26lt;%26lt;moves(n)%26lt;%26lt;" moves needed to move "%26lt;%26lt;n%26lt;%26lt;" disks"%26lt;%26lt;endl;


cout%26lt;%26lt;"\nContinue (Y/N) ";


cin%26gt;%26gt;ch;


}while(toupper(ch)=='Y');


if(toupper(ch)=='N')


exit(0);


return 0;


}


void Hanoi(int n,char A,char B,char C){


if(n==1)


cout%26lt;%26lt;"Move disk from "%26lt;%26lt;A%26lt;%26lt;" to "%26lt;%26lt;C%26lt;%26lt;'\n';


else{


Hanoi(n-1,A,C,B);


Hanoi(1,A,B,C);


Hanoi(n-1,B,A,C);


}


}


double moves (int n){


double r;


if(n == 1)


r = 1;


else


r = 1 + 2 * moves(n - 1);


return r;


}

Convert C++ to C - Hanoi Tower?
I won't write your program, but


cout can be converted to printf


cin can be converted to gets (for strings) and scanf for strings and integers...etc.


you can use getchar/getch for getting a single character.


No comments:

Post a Comment