输入两个点的坐标 求两点之间的距离c语言,C语言求两点之间的最短路径

#include

#include

# define n 6

# define NULL 0

#define UP 72

#define DOWN 80

#define ESC 27

#define Enter 13

int i1=0,j1=3,m1=0,n1=0;

int start=0;

char str2[][100]={"THE INTRODUTION OF THE ROUTINE !",

"AUGMENT THE RELATIVE DATA !",

"SEARCH FOR THE INFORMATION !",

"QUIT THE ROUTINE !"};

char str3[][100]={"THE ROUTINE WAS PROGRAMED IN ",

" 2006--07--10",

"PROGRAMER : ",

"CAO JUN BIN "};

int dd[n+1][n+1], pp[n+1][n+1],a[n];

typedef struct

{int number;
int edge[n+1][n+1];
}graph;

graph g;

int maxdist=2000,mindist;

struct viewpoint

{int x;
int y;
int number;
char inf[10];
}bd[n];

struct point

{int number;
int x_1;
int y_1;
}point[n];

typedef struct point f;

struct path

{int V1;
int V2;
int length;
}p[n*n/2];

void mainmenu ();

void con();

void choosepoint() ;

void interface();

void interface1();

void again();

void drawbuilding();

void drawedge();

void floyd(graph g,int m, int dd[n+1][n+1],int pp[n+1][n+1]);

void display(int i,int j,int pp[n+1][n+1],int dd[n+1][n+1]);

void drawpath();

void buildingmap( );

void pathmap( );

main()

{ int gdriver=9,gmode=2;

initgraph(&gdriver,&gmode,"c:\\TURBOC2");

mainmenu();

}

void mainmenu ()

{int a,flag=1,i,j;
char c; char str[10];
setbkcolor(BLACK);
setcolor(LIGHTGRAY);
rectangle(20,20,600,475);
settextstyle(4,0,4);
setcolor(LIGHTRED);
outtextxy(160,70,"THE MAIN MENU ");
settextstyle(3,0,1);
setcolor(CYAN);
outtextxy(160,160,str2[0]);
outtextxy(160,200,str2[1]);
outtextxy(160,240,str2[2]);
outtextxy(160,280,str2[3]);
while(flag){ c=getch();
switch(c){case DOWN:i1++;j1++;
m1=i1%4;n1=j1%4;
con();break;
case UP :if(i1==-1)i1=3;if(j1==-1)j1=3;
m1=i1%4;n1=j1%4;a=m1;m1=n1;n1=a;
con();
i1--;j1--;break;
case ESC :flag=0;break;
case Enter:cleardevice();
if(m1==0){interface1();getch();cleardevice();
mainmenu();
}

if(m1==1){ setbkcolor(BLACK);

setcolor(YELLOW);

rectangle(20,20,600,475);

rectangle(30,30,590,465) ;

setcolor(LIGHTRED);

settextstyle(3,0,1);

setcolor(LIGHTRED);

outtextxy(160,160,"PLESE CHOOSE THE SEVICE !");

outtextxy(120,200," AUGMENT THE INFORMATION OF THE BUILDING (B)!");

outtextxy(120,240," AUGMENT THE INFORMATION OF THE BUILDING (P) !");

switch(getch()){case'B':{cleardevice();buildingmap();cleardevice();};break;

case 'P':{cleardevice();pathmap();cleardevice();};break;

default: cleardevice();mainmenu();}

}

if(m1==2){

interface();

getch();}

if(m1==3){exit(0);
}

}

}

}

void con()

{setcolor(YELLOW);
outtextxy(160,160+m1*40,str2[m1]);
setcolor(CYAN);
outtextxy(160,160+n1*40,str2[n1]);
}

void interface()

{ int i,j;

char str[10];

setbkcolor(BLACK);

setcolor(LIGHTGRAY);

rectangle(20,20,600,100);

rectangle (20,20,600,470);

rectangle (20,100,450,470) ;

rectangle(20,350,450,470);

rectangle(20,100,300,350);

settextstyle(3,0,3);

outtextxy(200,30,"WELCOME TO");

outtextxy(120,60,"NORTHEAST DIANLI UNIVERSITY!") ;

outtextxy(460,200,"FROM : ___");

outtextxy(485,250,"TO : ___");

settextstyle(2,0,5);

rectangle(475,315,535,335);

rectangle(475,365,535,385);

outtextxy(455,150,"(PLEASE INPUT!)");

outtextxy(175,360,"THE RESULT !");

outtextxy(500,320,"F");

outtextxy(500,370,"Q");

outtextxy(480,400,"Input");

outtextxy(460,420, "the number 1-->6");

setcolor(RED);

drawbuilding();

drawedge();

i=0;

while(i'6') i=getch();

sprintf(str,"%c",i);

outtextxy(560,205,str);

while(j'6') j=getch();

sprintf(str,"%c",j);

outtextxy(560,255,str);

i=i-48;j=j-48;

floyd(g,n,dd[n+1][n+1],pp[n+1][n+1]);

display (i,j,pp[n+1][n+1], dd[n+1][n+1]);

drawpath( );

}

void interface1()

{

setbkcolor(BLACK);

setcolor(YELLOW);

rectangle(20,20,600,475);

rectangle(30,30,590,465) ;

settextstyle(4,0,4);

setcolor(LIGHTRED);

settextstyle(3,0,1);

setcolor(LIGHTRED);

outtextxy(160,160,str3[0]);

outtextxy(160,200,str3[1]);

outtextxy(210,240,str3[2]);

outtextxy(210,280,str3[3]);

}

void again()

{int i;
if(start==0) {for (i=0;i<300;i++) delay(1000);}

}

void drawbuilding()

{ int i,a;char str[10];

FILE *fp;

if ((fp=fopen("map.dat","r"))==NULL)

{

printf("Can not open the file of map.dat.\n");

exit(0);

}

for(i=0;i

{fread(&bd[i],sizeof(struct viewpoint),1,fp);
point[i].number=bd[i].number;
point[i].x_1=bd[i].x;
point[i].y_1=bd[i].y;
setcolor(RED);
a=bd[i].number;
sprintf(str,"%d",a);
again();
circle(bd[i].x,bd[i].y,8);
setcolor(LIGHTGRAY);
outtextxy(bd[i].x-2,bd[i].y-5,str);
outtextxy(310,i*30+120,str);
outtextxy(325,i*30+120,":");
outtextxy(340,i*30+120,bd[i].inf);
again();
}

fclose(fp);

}

void drawedge()

{ FILE *fp;char str[10];

int i,j,x1,x2,y1,y2,a;

if((fp=fopen("pmap.dat","r"))==NULL)

{ printf ("can't open the file\n");

exit(0);

}

for (i=0;i<=n;i++)

{for (j=0;j<=n;j++)
g.edge[i][j]=maxdist;}

for(j=1;j<=n;j++)

g.edge[j][j]=0;

while(!feof(fp))

{ fread(&p[i],sizeof(struct path),1,fp);

if(p[i].V1!=0)

{

{g.edge[p[i].V1][p[i].V2]=p[i].length;
g.edge[p[i].V2][p[i].V1]=p[i].length;
}

setcolor(LIGHTBLUE);

for(j=0;j

{if(p[i].V1-point[j].number==0)
{x1=point[j].x_1;
y1=point[j].y_1;
}

}

for(j=0;j

{if(p[i].V2==point[j].number)
{ x2=point[j].x_1;
y2=point[j].y_1;
}

}

line(x1,y1,x2,y2);

setcolor(LIGHTGRAY);

a=p[i].length;

sprintf(str,"%d",a);

outtextxy((x1+x2)/2,(y1+y2)/2,str);

again();

}

}

fclose(fp);

start=1;

}

void floyd(graph g,int m,int dd[n+1][n+1],int pp[n+1][n+1])

{int i,j,k,w;
for(i=0;i<=m;i++)
{for(j=0;j<=m;j++)
dd[i][j]=maxdist;}

for(i=0;i<=m;i++)

{for(j=0;j<=m;j++)
pp[i][j]=0;
}

for(i=1;i<=m;i++)

{ for(j=1;j<=m;j++)

{dd[i][j]=g.edge[i][j];
if(dd[i][j] pp[i][j]=j;
else
pp[i][j]=-1;
} }

for(k=1;k<=m;k++)

for(i=1;i<=m;i++)

for(j=1;j<=m;j++)

if(dd[i][j]>(dd[i][k]+dd[k][j]))

{

dd[i][j]=dd[i][k]+dd[k][j];

pp[i][j]=k;

pp[j][i]=k;

}

}

void display(int i,int j,int pp[n+1][n+1],int dd[n+1][n+1])

{

int pre,flag,k,w=40;int m=0;

int str[10];

pre=k=j;

do { pre=pp[i][pre];

if(pre!=k)

sprintf(str,"%d",k);

outtextxy(120+w,390,str);

outtextxy(130+w,390,"

a[m]=k;

m=m+1;

w=w+40;

k=pre;

pre=pp[i][pre];

flag=pre;

}while(flag!=k) ;

sprintf(str,"%d",flag);

outtextxy(120+w,390,str);

a[m]=flag;

outtextxy(130+w,390,"

sprintf(str,"%d",i);

outtextxy(160+w,390,str);

a[m+1]=i;

a[m+2]=NULL;

outtextxy(60,390,"THE WAY IS:");

outtextxy(60,420,"THE SHORTEST WAY IS :");

sprintf(str,"%d",dd[i][j]);

outtextxy(240,420,str);

}

void drawpath( )

{ int i,j,x1,x2,y1,y2;

char l;

setcolor(RED);

for(i=0;i

{if(a[i+1]!=NULL)
{ again();
for(j=0;j {if(a[i]-point[j].number==0)
{x1=point[j].x_1;
y1=point[j].y_1;
}

}

for(j=0;j

{if(a[i+1]==point[j].number)
{ x2=point[j].x_1;
y2=point[j].y_1;
}

}

line(x1,y1,x2,y2);

}

}

i=0;

while(i!='Q'&i!='F'&i!='q'&i!='f') i=getch();

switch(i)

{ case 'Q': {cleardevice();
mainmenu();};break;

case 'q': {cleardevice();
mainmenu();};break;

case 'F': {cleardevice();interface();};break;

case 'f': {cleardevice();interface();};break;

}

}

void buildingmap( )

{ FILE *fp;

int i;

setbkcolor(BLACK);

setcolor(YELLOW);

rectangle(100,40,450,475);

rectangle(110,50,440,465) ;

setcolor(LIGHTRED);

settextstyle(2,0,4);

setcolor(LIGHTRED); if((fp=fopen("ss1-map","ab"))==NULL)

{ printf ("can't open the file\n");

exit(0);

}

outtextxy(160,100,"Please input the information of the building! ");

for(i=0;i

{ outtextxy(160,100+20*(i+1),"

scanf("%d,%d,%d,%s\n",&bd[i].x,&bd[i].y,&bd[i].number,bd[i].inf );

if(fwrite(&bd[i],sizeof(struct viewpoint),1,fp)!=1)

printf("file write error\n");

}

fclose(fp);

if(getch()=='Q') {cleardevice();mainmenu();}

}

void pathmap()

{FILE *fp;
int i;
cleardevice();
setbkcolor(BLACK);
rectangle(100,40,450,475);
rectangle(110,50,440,465) ;
setcolor(LIGHTRED);
settextstyle(2,0,4);
if((fp=fopen("pp1-map","ab"))==NULL)
{ printf ("can't open the file\n");
exit(0);
}

fseek(fp,0L,2);

outtextxy(160,100,"Please input the information of the building! ");

for(i=0;i

{ outtextxy(160,100+20*(i+1),"

scanf("%d,%d,%d\n",&p[i].V1,&p[i].V2,&p[i].length );

if(fwrite(&p[i],sizeof(struct path),1,fp)!=1)

printf("file write error\n");

}

fclose(fp);

if(getch()=='Q') {cleardevice();mainmenu();}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值