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

📄 urouting.cpp

📁 实现最短路径算法。 实现最短路径算法。
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Urouting.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TRoutingForm *RoutingForm;
//---------------------------------------------------------------------------
__fastcall TRoutingForm::TRoutingForm(TComponent* Owner)
        : TForm(Owner)

{
    m_xSou=113.26818, m_ySou=23.17236 ,m_xDes=113.26739 ,m_yDes=23.12543;
    m_N=5;
}
//---------------------------------------------------------------------------
void __fastcall TRoutingForm::SpeedButton1Click(TObject *Sender)
{
       if(OpenDialog1->Execute())
       {
             NodeFile=OpenDialog1->FileName ;
             nodefile->Text=NodeFile;
       }
}
//---------------------------------------------------------------------------

void __fastcall TRoutingForm::SpeedButton2Click(TObject *Sender)
{
      if(OpenDialog1->Execute())
       {
             ArcFile=OpenDialog1->FileName ;
             arcfile->Text=ArcFile;
       }
}
//---------------------------------------------------------------------------

void __fastcall TRoutingForm::SpeedButton3Click(TObject *Sender)
{
       if(OpenDialog1->Execute())
       {
             AttrFile=OpenDialog1->FileName ;
             attrfile->Text=AttrFile;
       }
}
//---------------------------------------------------------------------------


void TRoutingForm::drawanarc(int arcNo,double minx,double maxx,double miny,double maxy,double dx,double dy)
{

        ArcNode  *currArc;
        double xx,yy;
        int sx,sy;
         Image1->Canvas->Pen->Color=clRed;
        currArc=arcGraph->nextArc;
     	while(currArc && currArc->arcNo != arcNo)currArc = currArc->nextArc;
        if(currArc)
        {
         xx= currArc->xd[0];
         yy= currArc->yd[0];
         sx= (int)((xx-minx)/dx);
         sy= Image1->Height-(int)((yy-miny)/dy);
         Image1->Canvas->MoveTo(sx,sy);
         for(int k=1;k<currArc->arcNum;k++)
              {
                     xx= currArc->xd[k];
                     yy=currArc->yd[k];
                     sx= (int)((xx-minx)/dx);
                     sy= Image1->Height-(int)((yy-miny)/dy);
                     Image1->Canvas->LineTo(sx,sy);

              }
       } 
}

void TRoutingForm::writearc(FILE *ooo,int arcNo)
{

        ArcNode  *currArc;
        double xx,yy;
        int sx,sy;

        currArc=arcGraph->nextArc;
     	while(currArc && currArc->arcNo != arcNo)currArc = currArc->nextArc;
        if(currArc)
        {
              fprintf(ooo,"%d\n", currArc->arcNum);
              for(int k=0;k<currArc->arcNum;k++)
              {
                     xx= currArc->xd[k];
                     yy=currArc->yd[k];
                     fprintf(ooo,"%lf %lf\n",xx,yy);
              }
       } 
}
void __fastcall TRoutingForm::Button1Click(TObject *Sender)
{
        FILE *fil,*arcf;
        fil = fopen("pathdata.dat","w");
        arcf=fopen("arcfile.dat","w");
  //////////
        int      vexNo,countArc,countVex,arcNum,arcNo,headNo,tailNo;
	char name[MAXNAME];
	double   x,y,v,d;

        ArcNode  *currArc,*nextArc;
        arcGraph = (ArcNode *)malloc(sizeof(ArcNode));
	if(arcGraph == 0)  ShowMessage("error");
	     //	return false;
        arcGraph->nextArc=0;
        currArc= arcGraph;
        FILE *lineFp;

	if(!(lineFp = fopen(ArcFile.c_str(),"r")))   ShowMessage("error");
	     //	return false;

        double minx=100000000.0,maxx=0,miny=1000000000.0,maxy=0;
        double dx,dy;

        while(!feof(lineFp))
	{
		if(fscanf(lineFp,"%d %d %d %s %lf %lf %d",&arcNo,&headNo,&tailNo,name,&v,&d,&arcNum) == 7)
		{

                        nextArc  = (ArcNode *)malloc(sizeof(ArcNode));
                        if(nextArc == 0)   ShowMessage("error");
                                  // return false;
                        nextArc->arcNo  = arcNo;

                        memset(nextArc->info.arcName,'\0',MAXNAME);
                        memcpy(nextArc->info.arcName,name,MAXNAME);
                        memset(nextArc->info.roadName,'\0',MAXNAME);
                        nextArc->info.distance = d;
                        nextArc->info.time =d/v;
                        nextArc->info.headVexNo = headNo;
                        nextArc->info.tailVexNo = tailNo;
                        nextArc->nextArc = 0;

                        nextArc->arcNum  = arcNum;
                        nextArc->xd=new double[arcNum];
                        nextArc->yd=new double[arcNum];
                        for(int j=0 ;j<arcNum;j++)
			{
				fscanf(lineFp,"%lf %lf ",&x,&y);
                                if(minx>x)minx=x;
                                if(maxx<x)maxx=x;
                                if(miny>y)miny=y;
                                if(maxy<y)maxy=y;
                                nextArc->xd[j]=x;
                                nextArc->yd[j]=y;
			}
                        currArc->nextArc=nextArc;
                        currArc=nextArc;
                }
        }

        fclose(lineFp);

 //////////////////
       dx=(maxx-minx)/Image1->Width;
       dy=(maxy-miny)/Image1->Height;
       Image1->Canvas->Pen->Color=clBlack;
       int sx,sy;
       double xx,yy;
       for(currArc= arcGraph->nextArc; currArc;  currArc=currArc->nextArc)
         {
              xx= currArc->xd[0];
              yy=currArc->yd[0];
              sx= (int)((xx-minx)/dx);
              sy= Image1->Height-(int)((yy-miny)/dy);
              Image1->Canvas->MoveTo(sx,sy);
              for(int k=1;k<currArc->arcNum;k++)
              {
                     xx= currArc->xd[k];
                     yy=currArc->yd[k];
                     sx= (int)((xx-minx)/dx);
                     sy= Image1->Height-(int)((yy-miny)/dy);
                     Image1->Canvas->LineTo(sx,sy);

              }

         }

 //////////////////////////////

        PathsNode *pathDis = (PathsNode*)malloc(sizeof(PathsNode));
	PathsNode *pathTim = (PathsNode*)malloc(sizeof(PathsNode));
      	bool shortFlag = routing_short(NodeFile.c_str(),ArcFile.c_str(),AttrFile.c_str(),m_xSou ,m_ySou ,m_xDes ,m_yDes,&pathDis,&pathTim,m_N);
        int kkk=0;
  	if(shortFlag)
	{
	     	if(pathDis>0)
             //   if(pathTim>0)
		{



		     PathsNode *currPaths = pathDis;
                    // PathsNode *currPaths = pathTim;
			PathNode  *currPath;
			while(currPaths)
			{
				currPath  = currPaths->path;
                                AnsiString pp;
                                char ppp[30];
                                sprintf(ppp,"%3d", currPath->vexNo);
                                fprintf(fil,"%3d", currPath->vexNo);
                                pp=pp+ ppp;
                                kkk++;
				while(currPath)
				{
                                     
                                     sprintf(ppp,"->%3d", currPath->arcNo);
                                     fprintf(fil,"->%3d", currPath->arcNo);
                                     if(kkk==1)drawanarc( currPath->arcNo, minx, maxx, miny, maxy, dx, dy);
                                     if(kkk==1)writearc(arcf, currPath->arcNo);
                                     pp=pp+ ppp;
                                     currPath = currPath->next;
                                }
                                Memo1->Lines->Add(pp);

                                fprintf(fil,"\n");
				currPaths = currPaths->next;
		         }
		}
	}
	path_destruct(pathDis);
	path_destruct(pathTim);
        fcloseall();
}
//---------------------------------------------------------------------------

void __fastcall TRoutingForm::x1edExit(TObject *Sender)
{
      m_xSou=StrToFloat(x1ed->Text);
}
//---------------------------------------------------------------------------

void __fastcall TRoutingForm::y1edExit(TObject *Sender)
{
        m_ySou=StrToFloat(y1ed->Text);
}
//---------------------------------------------------------------------------

void __fastcall TRoutingForm::x2edChange(TObject *Sender)
{
          m_xDes=StrToFloat(x2ed->Text);
}
//---------------------------------------------------------------------------

void __fastcall TRoutingForm::y2edExit(TObject *Sender)
{
        m_yDes=StrToFloat(y2ed->Text);
}
//---------------------------------------------------------------------------

void __fastcall TRoutingForm::Button3Click(TObject *Sender)
{
        Close();
}
//---------------------------------------------------------------------------

void __fastcall TRoutingForm::lineedExit(TObject *Sender)
{
        m_N=StrToInt( lineed->Text);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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