PDA

View Full Version : گفتگو: مساله برج های هانوی به صورت گرافیکی



alicam
شنبه 11 مهر 1388, 22:23 عصر
سلام
دوستان اگه ممکنه کد زیر رو بخونید.
البته این رو هم ذکر کنم که در این برنامه در قسمتی از دات نت استفاده کردم و در قسمتی که دستم رسیده از کد های native .
فکر میکنم که کامپایلر اشکال پیدا کرده که این رو لود نمیکنه. البته دات نت رو هم دارم.
ممنون
http://barnamenevis.org/forum/attachment.php?attachmentid=37892&stc=1&d=1254597725

xlog_termite
دوشنبه 04 آبان 1388, 19:52 عصر
دوست عزيز اجرا نشد ...!

tdkhakpur
سه شنبه 05 آبان 1388, 00:45 صبح
اصلاح شد ولي براي گرافيكي بودنش احتياج به اصلاح دارد.


#include <iostream>
#include <iomanip>
#include <conio.h>

//define local variable in program and prototype functions
void Hanoyfun(int , const int , const int , const int , bool);
void Draw (int [] , int [] , int []);
void iniArray(int [] , int [] , int [] , int );
void findPlace(int , int );
void print(int , int);
char chreturn(int);
int A[16]={0}, B[16]={0} ,C[16]={0};
int c , printCounter=0;

void Hanoyfun(int CountDisks ,const int FirstDiskPlace ,
const int interiemDiskPlace, const int DistanceDiskPlace , bool Graph)
{
if(Graph == false)
{
//main function hanoys RECESSIVE
if(CountDisks == 1)
print(FirstDiskPlace ,DistanceDiskPlace);
else
{
Hanoyfun ( CountDisks - 1, FirstDiskPlace, DistanceDiskPlace, interiemDiskPlace ,Graph);
print(FirstDiskPlace ,DistanceDiskPlace);
Hanoyfun( CountDisks - 1, interiemDiskPlace, FirstDiskPlace, DistanceDiskPlace , Graph);
}//End second else
}//End First if

else
{
if(CountDisks == 1){
findPlace(FirstDiskPlace , DistanceDiskPlace);
print(FirstDiskPlace ,DistanceDiskPlace);
getch();
}
else
{
Hanoyfun ( CountDisks - 1, FirstDiskPlace, DistanceDiskPlace, interiemDiskPlace ,Graph);
findPlace(FirstDiskPlace , DistanceDiskPlace );
print(FirstDiskPlace ,DistanceDiskPlace);
getch();
Hanoyfun( CountDisks - 1, interiemDiskPlace, FirstDiskPlace, DistanceDiskPlace , Graph);
}//End second else
}//End First else
}//End Hanoyfun

void print(int first , int last)
{
printCounter++;
cout<<printCounter<<":"<<chreturn(first)<<"-->"<<chreturn(last)<<endl;
}//End print function
char chreturn(int givef )
{
if(givef == 1) return 'A';
if(givef == 2) return 'B';
if(givef == 3) return 'C';
return 'A';
}//End chreturn function
/*find palce function
for find places of column of array
*/
void findPlace(int first , int distance)
{
int giveA=-1 , giveB=-1 , giveC=-1 ,i;
//part1 :initioalize giveA and giveB and giveC
for(i=15 ; i>=0 ; i--)
if(A[i]>0){
giveA=i;
break;
}
for(i=15 ; i>=0 ;i--)
if(B[i]>0){
giveB=i;
break;
}
for(i=15 ; i>=0 ; i--)
if(C[i]>0){
giveC=i;
break;
}

//part2 :use first and distance to send draw function
if(first == 1)
{
if(distance == 2){
B[++giveB]=A[giveA];
A[giveA]=0;
Draw(A , B , C);
}
else
{
C[++giveC]=A[giveA];
A[giveA]=0;
Draw(A , B , C);
}//End else
}//End if
if(first == 2)
{
if(distance == 1){
A[++giveA]=B[giveB];
B[giveB]=0;
Draw(A , B , C);
}
else
{
C[++giveC]=B[giveB];
B[giveB]=0;
Draw(A , B , C);
}//End else
}//End if
if(first == 3)
{
if(distance == 1){
A[++giveA]=C[giveC];
C[giveC]=0;
Draw(A , B , C);
}
else
{
B[++giveB]=C[giveC];
C[giveC]=0;
Draw(A , B , C);
}//End else
}//End if
}//End finePlace function
/* if boolean graph veiw be true
this function is useful
and draw in cmd mode hanoy project
*/
void Draw(int AA[] , int BB[] , int CC[])
{
//if you cannot use .NET Freamwork you can
//use clrscr() function
clrscr();
for(int i=0 ;i<c ;i++)
{
for(int j=0; j<AA[c-(i+1)] ; ++j)
cout<<"-";
cout<<endl;
}
cout<<"Cluomn A"<<endl;
/////////////////////////////////
for(int i=0 ;i<c ;i++)
{
for(int j=0; j<BB[c-(i+1)] ; ++j)
cout<<"-";
cout<<endl;
}
cout<<"Cluomn B"<<endl;
////////////////////////////////
for(int i=0 ;i<c ;i++)
{
for(int j=0; j<CC[c-(i+1)] ; ++j)
cout<<"-";
cout<<endl;
}
cout<<"Cluomn C"<<endl;
////////////////////////////////
}//End Draw
int main(void)
{
int k;
const int f=1 , d=2 , i=3;
int Number=1;
bool Graphview=true ;
//while for main screen
while(true){
cout<<"<<Welcome To Mehran project>>\n\n\n<<Hanoy prject>>...\n"
<<"Choose your destination:"<<endl<<"1.Graph view"<<endl <<
"2.Number view"<<endl<<"3.Qiut"<<endl;
cin>>Number;

switch(Number)
{
case 1:
//if you cannot use .NET Freamwork you can
//use clrscr() function
clrscr();
Graphview =true;
printCounter=0;
cout<<"Enter one number==>";
cin>>c;
//initioalize array and his Control
// for(int i=0 ; i< c ; i++){
for(k=0 ; i< c ; k++){
A[k]=c-i;B[k]=0;C[k]=0;
}
if(0 < c && c < 16)
Hanoyfun(c , f ,i ,d , Graphview);
else
cout<<"\n\t\aERROR : Your enterd number to view must be in rang of 1 to 15\n";
getch();
clrscr ();
break;
case 2:
clrscr();
Graphview = false;
printCounter=0;
cout<<"Enter one number==>";
cin>>c;
if(0 < c && c< 16)
Hanoyfun(c , f ,i ,d , Graphview);
else
cout<<"\n\t\aERROR : Your enterd number to view must be in rang of 1 to 15\n";
getch();
clrscr ();
break;
case 3:
return 0;
break;
default:
clrscr();
// beep();
cout<<"ERROR : You must enter one number in rage of 1 to 3...\nTry again";
getch();
clrscr();
break;
}//End switch
}//End While
getch();
return 0;
}//End main

arahan
دوشنبه 22 مهر 1392, 12:37 عصر
دوستان کسی گرافیکیش رو نداره؟
من واقعا بهش احتیاج دارم
لطفا کمک کنید