سلام.
من برنامه زیر را برای ساختمان داده میخواستم.
اگه کمکم کنین ممنون میشم.
تابع هانوی را به روش های بازگشتس و غیر بازگشتی برنامه نویسی کنید(در صورت امکان خروجی گرافیکی باشد)
Printable View
سلام.
من برنامه زیر را برای ساختمان داده میخواستم.
اگه کمکم کنین ممنون میشم.
تابع هانوی را به روش های بازگشتس و غیر بازگشتی برنامه نویسی کنید(در صورت امکان خروجی گرافیکی باشد)
سلام......................
کد:
/* Algorithmic solution is as follows 1) if TopN==1, move the single disc from A to C and stop. 2) Move the top n-1 discs from A to B, using C as Inter. 3) Move the remaining disc from A to C. 4) Move the n-1 discs from B to C, using A as destination(dest).*/ #include <stdio.h> #include <conio.h> void tower(int,char,char,char); /*prototype*/ int main() { int ndisk; clrscr(); printf("\n Enter number of disks <<<::: "); scanf("%d",&ndisk); tower(ndisk,'A','B','C'); /*Calling Function*/ getch(); return 0; } /* End of program */ /********************************************/ // src = Source | aux = Auxiliry | dest = Destination void tower(int topN, char src,char aux,char dest) { if(topN == 1) { printf("\n Disk 1 from %c to %c ",src,dest); } else { tower(topN-1,src,dest,aux); //src to aux printf("\n Disk %d from %c to %c ",topN,src,dest); tower(topN-1,aux,src,dest); //aux to dest } }
کد:* hanoi.cpp solves the Towers of Hanoi puzzle recursively.
*
* Input: numDisks, the number of disks to be moved
* Output: a sequence of moves that solve the puzzle */
#include <iostream.h>
#include <conio.h>
void Move(int n, char source, char destination, char spare);
int count = 0;
int main()
{
const char PEG1 = 'A', // the three pegs
PEG2 = 'B',
PEG3 = 'C';
cout << "This program solves the Hanoi Towers puzzle.\n\n";
cout << "Enter the number of disks: ";
int numDisks; // the number of disks to be moved
cin >> numDisks;
cout << endl;
Move(numDisks, PEG1, PEG2, PEG3); // the solution
// getch();
return 0;
}
/* Move is a recursive function to solve the
* Towers of Hanoi puzzle.
*
* Receive: n, the number of disks to be moved;
* source, peg containing disk to move
* destination, peg where to move disk
* spare, peg to store disks temporarily,
* Output: A message describing the move
**************************************************/
void Move(int n,
char source, char destination, char spare)
{
if (n <= 1) // anchor
cout << "Move the top disk from " << source
<< " to " << destination << endl;
else
{ // inductive case
Move(n-1, source, spare, destination);
Move(1, source, destination, spare);
Move(n-1, spare, destination, source);
}
}
منبع: sohail2d
سلام.
خيلي ممنون ,عالي بود