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

📄 runform.~cpp

📁 这是我的作业
💻 ~CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <fstream.h>
#include "RunForm.h"
#include "InitForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SUIButton"
#pragma link "SUIForm"
#pragma resource "*.dfm"
#pragma link "HemisphereButton"


TFrm_Run *Frm_Run;
void shortestPath(int n,int source,int dist[],int pre[]);
//---------------------------------------------------------------------------
__fastcall TFrm_Run::TFrm_Run(TComponent* Owner)
        : TForm(Owner)
{
CompList=new TList;
}
//---------------------------------------------------------------------------

void _fastcall TFrm_Run::RegisterCompClass()
{

 //  try
 //  {FindClass("THemiBtn");
 //  }
  // catch(Exception &e)
 RegisterClass(__classid(THemiBtn));

 //  }
}
void __fastcall TFrm_Run::FormCreate(TObject *Sender)
{
Panel1->DoubleBuffered=true;
Button1->Enabled=false;
RegisterCompClass();
LoadComp();
LoadLine();
}
//---------------------------------------------------------------------------
void _fastcall TFrm_Run::LoadComp()
{
   char buf[2];

   char graphbuf[64];
   ifstream fin;
   fin.open(DATAFILE);
   fin.read(buf,1);
   fin.read(graphbuf,64);
   fin.close();
   for(int i=0;i<MAX;i++)
     for(int j=0;j<MAX;j++)
     {
         Graph[i][j]=graphbuf[i*MAX+j]-48;
         if(i!=j&&Graph[i][j]==0) Graph[i][j]=MAX_INF;
     }
   TMemoryStream *ms=new  TMemoryStream();
   THemiBtn * HemiBtn;
   HemiBtn=new THemiBtn(this);
   ms->LoadFromFile(CMPFILENAME) ;
   for(int i=0;i<*buf-48;i++)
   {
     HemiBtn= (THemiBtn*)(ms->ReadComponent(NULL));
     HemiBtn->Parent =Panel1;
     HemiBtn->OnClick=MyBtnClick;
     CompList->Add(HemiBtn);
   }
   delete ms;
}

void _fastcall TFrm_Run::LoadLine()
{
   THemiBtn* pBtn1=new THemiBtn(this);
   THemiBtn* pBtn2=new THemiBtn(this);
   int X1,Y1,X2,Y2;
   for(int i=0;i<MAX;i++)
     for(int j=0;j<MAX;j++)
       if(Graph[i][j]!=0&&Graph[i][j]<MAX_INF)
         {
           pBtn1=(THemiBtn*)(CompList->Items[i]);
           pBtn2=(THemiBtn*)(CompList->Items[j]);
            X1= pBtn1->Left+RADIO/2;
            Y1= pBtn1->Top+RADIO/2;
            X2= pBtn2->Left+RADIO/2;
            Y2= pBtn2->Top+RADIO/2;
        //  Y1= (THemiBtn*)(CompList->Items[i])->Top+RADIO/2 ;
        //   X2= (THemiBtn*)(CompList->Items[j])->Left+RADIO/2;
        //   Y2= (THemiBtn*)(CompList->Items[j])->Top+RADIO/2;
          Image1->Canvas->Pen->Color=clBlack;
          Image1->Canvas->Pen->Width=2;
          Image1->Canvas->MoveTo(X1,Y1);
          Image1->Canvas->LineTo(X2,Y2);
          Image1->Canvas->TextOutA((pBtn1->Left+pBtn2->Left)/2,(pBtn1->Top+pBtn2->Top)/2,IntToStr(Graph[i][j]));
          }
    // delete pBtn1;
   //  delete pBtn2;
}

void __fastcall TFrm_Run::MyBtnClick(TObject *Sender)
{
   THemiBtn* pBtn;
   if(pBtn=dynamic_cast<THemiBtn*>(Sender))
   {

         if(BtnTemp!=pBtn->Name)BitCount++;
         BtnTemp=pBtn->Name;
         if(BitCount==1)
           {
            char *str;
             str=pBtn->Name.c_str();
             Source=int(*str)-97;
            Label1->Caption="选择目的节点";

           }
         if(BitCount==2)
           {
             char *str;
             str=pBtn->Name.c_str();
             Dest=int(*str)-97;
             Label1->Caption="点开始按钮" ;
             BitCount=0;
             Button1->Enabled=true;
           }
   }

}
void shortestPath(int n,int source,int dist[],int pre[])
{
  int s[8],min,temp;
  for(int i=0;i<n;i++)
    {
      dist[i]=Graph[source][i];

      s[i]=0;

     pre[i]=dist[i]<MAX_INF? source:(-1);

    // cout<<pre[i];
    }
  s[source]=1;
  pre[source]=-1;
  for(int i=0;i<n-1;i++)
   {
     min=MAX_INF;
     temp=-1 ;
     for(int j=0;j<n;j++)
     if(s[j]==0)
      if(dist[j]!=0&&dist[j]<min)
      {
        min=dist[j];
        temp=j;
      }
      if(temp==-1) return;
      s[temp]=1;
    //  pre[i]=temp;
      for(int j=0;j<n;j++)
       if(s[j]==0&&Graph[temp][j]<1000)

         if(dist[j]>dist[temp]+Graph[temp][j])
         { dist[j]=dist[temp]+Graph[temp][j];
          pre[j]=temp;  }

   }



}
void __fastcall TFrm_Run::FormClose(TObject *Sender, TCloseAction &Action)
{
Action=caFree;
}
//---------------------------------------------------------------------------


void __fastcall TFrm_Run::suitempButton1Click(TObject *Sender)
{
THemiBtn* pBtn1=new THemiBtn(this);
THemiBtn* pBtn2=new THemiBtn(this);
int pre[MAX]={0,0,0,0,0,0,0,0};
int dist[MAX]={0,0,0,0,0,0,0,0};
int result;
int S_temp,D_temp,temp;
shortestPath(CompList->Count,Source,dist,pre);

temp=Dest;
while(temp!=Source)
{
  S_temp=temp;
  D_temp=pre[temp];
  if(D_temp!=-1)
  {
    pBtn1=(THemiBtn*)(CompList->Items[S_temp]);
    pBtn2=(THemiBtn*)(CompList->Items[D_temp]);
    Image1->Canvas->Pen->Color=clRed;
    Image1->Canvas->Pen->Width=2;
    Image1->Canvas->MoveTo(pBtn1->Left+RADIO/2,pBtn1->Top+RADIO/2);
    Image1->Canvas->LineTo(pBtn2->Left+RADIO/2,pBtn2->Top+RADIO/2);
    temp=D_temp;
  }
  else break;
}

Button1->Enabled=false;
Label1->Caption="选择目的节点";

}
//---------------------------------------------------------------------------

void __fastcall TFrm_Run::suiButton1Click(TObject *Sender)
{
LoadLine();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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