Shortest_path.c
#include <stdio.h>
#include <stdlib.h>//with file
#include <string.h>//available gets (), puts ()
#include "shortest_path.h"
#define MAX 32767
#define MENU "Welcome into the navigation system!" \n========== menu ===========\n0, load the map \n1, set up map \n2, query Shortest path \n3, exit \n========== menu ===========\n "
struct Stmap map;//without network
const char *filepath1= "D:\\spots.dat";
const char *filepath2= "D:\\paths.dat";
Int Load1 ()
{
file *fp;
int i;
fp=fopen (filepath1, "R");
if (fp==null) {printf ("Spots file open exception, read failed"); return-1;}
fread (&map.spotnum,sizeof (int), 1,FP);
for (i=0;i<map.spotnum;i++)
{
fread (map.spot[i].name,sizeof (char), 10,FP);
fread (Map.spot[i].intro,sizeof (char), 20,FP);
}
fclose (FP);
return 0;
}
int load2 ()
{
file *fp;
int i,j;
fp=fopen (filepath2, "R");
if (fp==null) {printf ("Paths file open exception, read failed"); return-1;}
fread (&map.pathmatrix,sizeof (int), 1,FP);
for (i=0;i<map.spotnum;i++)
for (j=0;j<map.spotnum;j++)
Fread (&map.pathmatrix[i][j],sizeof (int), 1,FP);
fclose (FP);
return 0;
}
void Loadmap ()
{
if (load1 () ==0)
printf ("spot read success \ n");
else
printf ("spot read failed \ n");
if (Load2 () ==0)
printf ("Path read success \ n");
Else
printf ("path read failed \ n");
}
void Drawmap ()/Direct input
{
int i;
int a,b;
Char s1[10],s2[10];
printf ("How many sights are there?") (<=20) ");//map.spotmun
Fflush (stdin);
scanf ("%d", &map.spotnum);
printf ("There are a number of attractions and attractions directly connected to the path?" ");//map.pathnum
Fflush (stdin);//empty keyboard buffer, in "stdio.h"
scanf ("%d", &map.pathnum);
for (a=0;a<map.spotnum;a++)//initialize map
for (b=0;b<map.spotnum;b++)
{
if (a==b) map.pathmatrix[a][b]=0;
else Map.pathmatrix[a][b]=max;
}
for (i=0;i<map.spotnum;i++) {
printf ("Please enter the name of the first%d attractions (<=10letters)", i+1);
Fflush (stdin);
Gets (Map.spot[i].name);
printf ("Please enter the introduction of the first%d attractions (<=20letters)", i+1);
Fflush (stdin);
Gets (Map.spot[i].intro);
}//Enter the name and description of the attraction Map.spot[].name;map.spot[].intro
for (i=0;i<map.pathnum;i++) {
do{
printf ("Please enter the start of the%d path", i+1);
Fflush (stdin);
Gets (S1);
for (a=0;a<map.spotnum;a++)
if (!strcmp (MAP.SPOT[A].NAME,S1)) break;//Find the spot number
if (a==map.spotnum) printf ("Does not exist, please re-enter it.") \ n ");
}while (A==map.spotnum);
do{
printf ("Please enter the end of page%d path", i+1);
Fflush (stdin);
Gets (S2);
for (b=0;b<map.spotnum;b++)
if (!strcmp (MAP.SPOT[B].NAME,S2)) break;
if (b==map.spotnum) printf ("Does not exist, please re-enter it.") \ n ");
}while (B==map.spotnum);
printf ("Please enter the length of section%d path", i+1);
Fflush (stdin);
scanf ("%d", &map.pathmatrix[a][b]);
map.pathmatrix[b][a]=map.pathmatrix[a][b];//Input Path length
}
}
void Shortestpath ()//Shortest path, enter start point end output path and distance
{
struct Stspath spath[20];
int s,t,v,w,min;
Char s1[10],s2[10];
int pathorder[20];
struct Stspath *p;//pathorder
do{
printf ("Please enter a starting point");//Find the spot number for the starting point
Fflush (stdin);
Gets (S1);
for (s=0;s<map.spotnum;s++)
if (!strcmp (MAP.SPOT[S].NAME,S1)) break;
if (s==map.spotnum) printf ("Does not exist, please re-enter it.") \ n ");
}while (S==map.spotnum);
do{
printf ("Please enter End");//Find the destination number for the end point
Fflush (stdin);
Gets (S2);
for (t=0;t<map.spotnum;t++)
if (!strcmp (MAP.SPOT[T].NAME,S2)) break;
if (t==map.spotnum) printf ("Does not exist, please re-enter it.") \ n ");
}while (T==map.spotnum);
for (v=0;v<map.spotnum;v++)//initialize spath
{
Spath[v].length=max;
spath[v].in=0;
}
Spath[s].in=1;
spath[s].length=0;
Spath[s].pior=null;
V=s;
while (v!=t) {
for (w=0;w<map.spotnum;w++)
if ((!spath[w].in) && (Spath[w].length>spath[v].length+map.pathmatrix[v][w])) {
SPATH[W].LENGTH=SPATH[V].LENGTH+MAP.PATHMATRIX[V][W];
spath[w].pior=&spath[v];
}
Min=max;
for (w=0;w<map.spotnum;w++)
if ((!spath[w].in) && (spath[w].length<min)) {
Min=spath[w].length;
V=w;
}
Spath[v].in=1;
}
printf ("Shortest path length is%d, shortest path is: \ n", spath[t].length);//print Path
For (V=0,p=&spath[t];p->pior!=null;p=p->pior)
{
Pathorder[v]=p-spath;
v++;
}
Pathorder[v]=s;
for (; v>=0;v--)
printf ("%10s", map.spot[pathorder[v]].name);
}
void Main ()
{
int menu=1;
printf (MENU);
while (menu!=3)
{
printf ("Please enter your option (number) \ n");
Fflush (stdin);
scanf ("%d", &menu);
Switch (menu)
{
Case 0:loadmap ();p rintf ("\ r \ n map loading complete \ n");
Case 1:drawmap ();p rintf ("\ new complete \ \ n");
Case 2:shortestpath ();p rintf ("\ n query complete \ n");
}
}
printf ("Thank you for using it!") ");
}
2. [File] shortest_path.h ~ 430B Download (2)
#ifndef _shortest_path_h_
#define _shortest_path_h_
Vertices of struct stspot{//sights
Char name[11];//attractions name no more than 10
Char intro[20];//Attractions Introduction no more than 20
};
struct stmap{//whole non-direction net
Stspot spot[20];//dot, attraction vector
int pathmatrix[20][20];//edge, path adjacency matrix
int spotnum;
int pathnum;
};
struct stspath//An array of sights when seeking the shortest path
{
Stspath * PIOR;
int in;//can be Boolen
int length;
};
#endif