⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit2.cpp

📁 用c++builder編寫的漢諾塔實例
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
String Str[100];
String sort[5];
int s[5],st[5];
int cnt=0,i,j,k,temp;
int c1,c2,c3,c4,c5,timer=0;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm2::Panel2DragOver(TObject *Sender, TObject *Source,
      int X, int Y, TDragState State, bool &Accept)
{
if(Source==sma)
 {
 sma->Left=X;
 sma->Top=Y;
 }
 if(Source==mid)
 {mid->Left=X;
 mid->Top=Y;
 }
 if(Source==big)
 {
 big->Left=X;
 big->Top=Y;
 }
 if(Source==big1)
 {
 big1->Left=X;
 big1->Top=Y;
 }
if(Source==big2)
 {
 big2->Left=X;
 big2->Top=Y;
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm2::endClick(TObject *Sender)
{
Panel1->Hide();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::hX1Click(TObject *Sender)
{
Form2->Close();
}
//---------------------------------------------------------------------------
han(int n,String a,String c,String b)
{
static int i=0;
if(n==1)
{
Str[i]="yuanpan"+IntToStr(n)+"cong" +a+ "yidao"+c;
i++;
}
else
{
han(n-1,a,b,c);
Str[i]="yuanpan"+IntToStr(n)+"cong"+a+"yidao"+c;
i++;
han(n-1,b,c,a);

}
}
void __fastcall TForm2::inpClick(TObject *Sender)
{
int n,i,j=1;
String a="A",b="B",c="C";
n=StrToInt(InputBox("yuanpanshuruhe","yuanpanshuliang:","3"));
for(i=0;i<n;i++)
j=j*2;
lbout->Items->Add("zonggongbandongle" + IntToStr(j-1)+" ci");
han(n,a,c,b);
for(i=0;i<j;i++)
lbout->Items->Add(Str[i]);
}

//---------------------------------------------------------------------------


void __fastcall TForm2::Label5Click(TObject *Sender)
{
lbout->Clear();        
}
//---------------------------------------------------------------------------

void __fastcall TForm2::N5Click(TObject *Sender)
{
 Panel1->Show();
 PageControl1->Hide();
}
//---------------------------------------------------------------------------

void __fastcall TForm2::FormCreate(TObject *Sender)
{
Panel1->Hide();
PageControl1->Hide();
sgout->Cells[0][0]="zhuantai/weizhi";
sgout->Cells[0][1]="yuanshishuju";
lbout1->Items->Add("qipaopaixufaxiangxibuzhou:");
}
//---------------------------------------------------------------------------

void __fastcall TForm2::w1Click(TObject *Sender)
{
PageControl1->Show();
Panel1->Hide();
}
//---------------------------------------------------------------------------


void __fastcall TForm2::Button3Click(TObject *Sender)
{
PageControl1->Hide();
}
//---------------------------------------------------------------------------

void __fastcall TForm2::einKeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
if(cnt<5)
{
if(Key==13||Key==108)


{
sort[cnt]=ein->Text;
s[cnt]=StrToInt(ein->Text);
st[cnt]=s[cnt];
ein->Text="";
cnt++;
switch(cnt)
{
 case 1: p1->Caption=sort[0];
         break;
 case 2: p2->Caption=sort[1];
         break;
 case 3: p3->Caption=sort[2];
         break;
 case 4: p4->Caption=sort[3];
         break;
 case 5: p5->Caption=sort[4];
         break;
 }
 }
 }
 else
 {
 sin->Caption=" qingzhixingpaixu";
 sin->Color=clRed;
 }
}
//---------------------------------------------------------------------------

void __fastcall TForm2::executeClick(TObject *Sender)
{
for(i=0;i<cnt;i++)
{
sgout->Cells[i+1][1]=sort[i];
sgout->Cells[i+1][0]="di"+IntToStr(i+1)+"ge shu";
}
for(i=0;i<cnt-1;i++)
{
lbout1->Items->Add("di"+IntToStr(i+1)+"lunzhihuande buzhou:");
for(j=0;j<cnt-1;j++)
{
if(s[j+1]<s[j])
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
lbout1->Items->Add(" "+IntToStr(j+1)+
". jiangdi"+IntToStr(j+1)+"geshu("+
IntToStr(s[j+1])+")yudi "+IntToStr(j+2)+"geshu("+
IntToStr(s[j])+")jiaohuan");
}
sgout->Cells[0][i+2]="di"+IntToStr(i+1)+"lunzhihuan";
for(k=0;k<cnt;k++)
{
sort[k]=IntToStr(s[k]);
sgout->Cells[k+1][i+2]=sort[k];
}
}
}
sgout->Cells[0][cnt+2]="paixujieguowei";
for(k=0;k<cnt;k++)
{
sort[k]=IntToStr(s[k]);
sgout->Cells[k+1][cnt+2]=sort[k];
}
}
//---------------------------------------------------------------------------

void __fastcall TForm2::showClick(TObject *Sender)
{
for(i=0;i<cnt;i++)
{
if(s[i]==st[0])
c1=i+1;
if(s[i]==st[1])
c2=i+1;
if(s[i]==st[2])
c3=i+1;
if(s[i]==st[3])
c4=i+1;
if(s[i]==st[4])
c5=i+1;
}
sgout->Cells[0][cnt+1]="paixuqingzhongwei";
sgout->Cells[1][cnt+1]=StrToInt(c1);
sgout->Cells[2][cnt+1]=StrToInt(c2);
sgout->Cells[3][cnt+1]=StrToInt(c3);
sgout->Cells[4][cnt+1]=StrToInt(c4);
sgout->Cells[5][cnt+1]=StrToInt(c5);
timer=1;        
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Timer1Timer(TObject *Sender)
{
if(timer==1)
{
switch(c1)
{
case 1:if(sp1->Top>20){
sp1->Top=sp1->Top-3;
p1->Top=p1->Top-3;
Timer1->Interval=200;}
break;
case 2:if(sp1->Top>40){
sp1->Top=sp1->Top-3;
p1->Top=p1->Top-3;
Timer1->Interval=400;}
break;
case 3:if(sp1->Top>60){
sp1->Top=sp1->Top-3;
p1->Top=p1->Top-3;
Timer1->Interval=600;}
break;
case 4:if(sp1->Top>80){
sp1->Top=sp1->Top-3;
p1->Top=p1->Top-3;
Timer1->Interval=800;}
break;
case 5:if(sp1->Top>100){
sp1->Top=sp1->Top-3;
p1->Top=p1->Top-3;
Timer1->Interval=1000;}
break;
}
}
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Timer2Timer(TObject *Sender)
{
if(timer==1)
{
switch(c2)
{
case 1:if(sp2->Top>20){
sp2->Top=sp2->Top-3;
p2->Top=p2->Top-3;
Timer2->Interval=200;}
break;
case 2:if(sp2->Top>40){
sp2->Top=sp2->Top-3;
p2->Top=p2->Top-3;
Timer2->Interval=400;}
break;
case 3:if(sp2->Top>60){
sp2->Top=sp2->Top-3;
p2->Top=p2->Top-3;
Timer2->Interval=600;}
break;
case 4:if(sp2->Top>80){
sp2->Top=sp2->Top-3;
p2->Top=p2->Top-3;
Timer2->Interval=800;}
break;
case 5:if(sp2->Top>100){
sp2->Top=sp2->Top-3;
p2->Top=p2->Top-3;
Timer2->Interval=1000;}
break;
}
}
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Timer3Timer(TObject *Sender)
{
if(timer==1)
{
switch(c3)
{
case 1:if(sp3->Top>20){
sp3->Top=sp3->Top-3;
p3->Top=p3->Top-3;
Timer3->Interval=200;}
break;
case 2:if(sp3->Top>40){
sp3->Top=sp3->Top-3;
p3->Top=p3->Top-3;
Timer3->Interval=400;}
break;
case 3:if(sp3->Top>60){
sp3->Top=sp3->Top-3;
p3->Top=p3->Top-3;
Timer3->Interval=600;}
break;
case 4:if(sp3->Top>80){
sp3->Top=sp3->Top-3;
p3->Top=p3->Top-3;
Timer3->Interval=800;}
break;
case 5:if(sp3->Top>100){
sp3->Top=sp3->Top-3;
p3->Top=p3->Top-3;
Timer3->Interval=1000;}
break;
}
}
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Timer4Timer(TObject *Sender)
{
  if(timer==1)
{
switch(c4)
{
case 1:if(sp4->Top>20){
sp4->Top=sp4->Top-3;
p4->Top=p4->Top-3;
Timer4->Interval=200;}
break;
case 2:if(sp4->Top>40){
sp4->Top=sp4->Top-3;
p4->Top=p4->Top-3;
Timer4->Interval=400;}
break;
case 3:if(sp4->Top>60){
sp4->Top=sp4->Top-3;
p4->Top=p4->Top-3;
Timer4->Interval=600;}
break;
case 4:if(sp4->Top>80){
sp4->Top=sp4->Top-3;
p4->Top=p4->Top-3;
Timer4->Interval=800;}
break;
case 5:if(sp4->Top>100){
sp4->Top=sp4->Top-3;
p4->Top=p4->Top-3;
Timer4->Interval=1000;}
break;
}
}
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Timer5Timer(TObject *Sender)
{
if(timer==1)
{
switch(c5)
{
case 1:if(sp5->Top>20){
sp5->Top=sp5->Top-3;
p5->Top=p5->Top-3;
Timer5->Interval=200;}
break;
case 2:if(sp5->Top>40){
sp5->Top=sp5->Top-3;
p5->Top=p5->Top-3;
Timer5->Interval=400;}
break;
case 3:if(sp5->Top>60){
sp5->Top=sp5->Top-3;
p5->Top=p5->Top-3;
Timer5->Interval=600;}
break;
case 4:if(sp5->Top>80){
sp5->Top=sp5->Top-3;
p5->Top=p5->Top-3;
Timer5->Interval=800;}
break;
case 5:if(sp5->Top>100){
sp5->Top=sp5->Top-3;
p5->Top=p5->Top-3;
Timer5->Interval=1000;}
break;
}
}
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Button4Click(TObject *Sender)
{
lbout1->Clear();
ein->ReadOnly=false;
ein->Text="";
for(i=0;i<10;i++)
{for(j=0;j<10;j++)
sgout->Cells[i+1][j+1]="";}
p1->Caption=sort[0]="";
p2->Caption=sort[1]="";
p3->Caption=sort[2]="";
p4->Caption=sort[3]="";
p5->Caption=sort[4]="";
 cnt=0;timer=0;
sp1->Top=264;
sp2->Top=264;
sp3->Top=264;
sp4->Top=264;
sp5->Top=264;
p1->Top= 248;
p2->Top= 248;
p3->Top= 248;
p4->Top= 248;
p5->Top= 248;
sin->Caption="paixushujushuru";
sin->Color=clSkyBlue;
}
//---------------------------------------------------------------------------


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -