SoFunction
Updated on 2025-03-05

Implement navigation function in C language

This article shares the specific code for implementing navigation functions in C language for your reference. The specific content is as follows

#include<>
#include<>

#define NUM 25
#define INFINITY 32767
#define False 0
#define True 1

typedef struct
{
    int number;//The number of vertex    const char *sight;//The information of the vertex } VertexType;//The type of vertex 
 typedef struct
 {
     VertexType vex[NUM];//Storing vertex information     int arcs[NUM][NUM];//Add-matrix array     int vexnum;//Number of vertices }MGraph;
 
MGraph G;
 
 /**
 Create a graph from the number of incoming nodes
 **/ 
 void GreateMGraph(int v) 
 {
     =v;//The number of incoming nodes     for(int i=1;i<;i++){
         [i].number=i;
     }//Configure vertex number     
     /**
 Edit vertex information  
 **/ 
     [0].sight="Name of each attraction";
     [1].sight="The gate";
     [2].sight="Administrative Office Building";
     [3].sight="North District Classroom Training Center";
     [4].sight="Teaching Building No. 1";
     [5].sight="Teaching Building No. 2";
     [6].sight="Experimental Building";
     [7].sight="Teaching Building No. 3";
     [8].sight="library";
     [9].sight="Kindergarten room";
     [10].sight="supermarket";
     [11].sight="Liuxinyuan";
     [12].sight="Bath Center";
     [13].sight="Lixiu Garden";
     [14].sight="Comprehensive Building";
     [15].sight="swimming pool";
     [16].sight="Main track and field";
     [17].sight="Comprehensive Cultural and Sports Hall";
     
     /**
 First set the distance between all vertices to INFINITY
 **/
     for(int i=1;i<=;i++)
     {
         for(int j=1;j<=;j++)
         {
             [i][j]=INFINITY;
         }
     }
     
     /**
 Set the distance between each vertex
 **/
     [1][2]=[2][1]=255;
     [1][4]=[4][1]=501;
     [1][5]=[5][1]=535;
     [1][6]=[6][1]=705;
     [1][7]=[7][1]=722;
     [1][8]=[8][1]=790;
     [2][3]=[3][2]=530;
     [2][4]=[4][2]=450;
     [2][5]=[5][2]=484;
     [2][6]=[6][2]=654;
     [2][7]=[7][2]=663;
     [2][8]=[8][2]=748;
     [3][8]=[8][3]=1054;
     [3][17]=[17][3]=713;
     [4][5]=[5][4]=436;
     [4][6]=[6][4]=158;
     [4][7]=[7][4]=527;
     [4][8]=[8][4]=534;
     [5][6]=[6][5]=688;
     [5][7]=[7][5]=561;
     [5][8]=[8][5]=603;
     [6][7]=[7][6]=428;
     [6][8]=[8][6]=329;
     [6][9]=[9][6]=547;
     [7][8]=[8][7]=254;
     [8][11]=[11][8]=421;
     [8][17]=[17][8]=879;
     [9][10]=[10][9]=178;
     [10][11]=[11][10]=213;
     [10][12]=[12][10]=114;
     [12][13]=[13][12]=415;
     [13][14]=[14][13]=104;
     [13][16]=[16][13]=427;
     [13][15]=[15][13]=576;
     [14][17]=[17][14]=688;
     [15][16]=[16][15]=213;
     [16][17]=[17][16]=214;
 }
 
 /**
 Show campus map
 **/
 void Map()
 {
  printf("\n\n\n");
  printf("  ********************* Henan University of Finance and Economics and Law *************************************************);
  printf("\n\n\n");
  printf("                                         ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------);
  printf("                               |                          |            \n");
  printf("                               |                          |            \n");
  printf("   12 Bathroom Center--------------------------------------------------------------------------------------------------------------------);
  printf("       |                       |                          |            \n");
  printf("   10 Supermarket----11 Liuxinyuan);
  printf("    |          |              |---------------------------------------------------------------------------------);
  printf("   9 water rooms |);
  printf("    |          ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------);
  printf("    |                          |                            |          \n");
  printf("  |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------);
  printf("                     |         |              |             |          \n");
  printf("                     |         |              |             |          \n");
  printf("                                                                                                                                                                         �);
  printf("                               |                            |          \n");
  printf("                               |                            |          \n");
  printf("                                   |---2 Administrative Building------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------);
  printf("                               |                                       \n");
  printf("                               |                                       \n");
  printf("                                                                                                                                                                         �); 
}

 /**
 Introduction to the overview of various attractions on campus
 **/
 void Info(int sight_num,char data[][200])
{
    if(sight_num==1)
        puts(data[1]);
    if(sight_num==2)
        puts(data[2]);
    if(sight_num==3)
        puts(data[3]);
    if(sight_num==4)
        puts(data[4]);
    if(sight_num==5)
        puts(data[5]);
    if(sight_num==6)
        puts(data[6]);
    if(sight_num==7)
        puts(data[7]);
    if(sight_num==8)
        puts(data[8]);
    if(sight_num==9)
        puts(data[9]);
    if(sight_num==10)
        puts(data[10]);
    if(sight_num==11)
        puts(data[11]);
    if(sight_num==12)
        puts(data[12]);
    if(sight_num==13)
        puts(data[13]);
    if(sight_num==14)
        puts(data[14]);
    if(sight_num==15)
        puts(data[15]);
    if(sight_num==16)
        puts(data[16]);
    if(sight_num==17)
        puts(data[17]);
    if(sight_num==18)
        puts(data[18]);
    if(sight_num==19)
        puts(data[19]);
    if(sight_num==20)
        puts(data[20]);
    if(sight_num==21)
        puts(data[21]);
    if(sight_num==22)
        puts(data[22]);
    if(sight_num==23)
        puts(data[23]);
    if(sight_num==24)
        puts(data[24]);
    if(sight_num==25)
        puts(data[25]);
}

/**
 Start Menu
 **/
int Menu()
{
    int c;
    Map();
    printf("\t\tWelcome to use the navigation map system of Henan University of Finance and Economics\n");
    printf("\t\t 1. Query the location path \n");
    printf("\t\t 2. Introduction to location information \n");
    printf("\t\t 3.Exit \n");
    printf("  ********************* Henan University of Finance and Economics and Law *************************************\n");
    printf("Please enter your selection:");
    scanf("%d",&c);
    return c;
}

/**
 Navigation function of map
 **/

 /**
 Output the shortest path between any two points
 **/
 void guide_Dispath_two(MGraph g,int dist[],int path[],int S[],int v,int i)//v is the starting point, i is the end point {
     int apath[NUM],d=0;             //Storage the shortest path and the number of vertices (the end point in the path is the first)    int j,k;                          //k is used to store the front node of the end point    
    if(S[i]==1 && i!=v)
    {
        printf("The path length from vertex %d to vertex %d is: %d\t path is:",v,i,dist[i]);
        apath[d]=i;                //Put the end point in the first position in the array        k=path[i];
        
        if(k==-1)
            printf("No path");
            
        /**
 Use loops to store each node in the shortest path into the apath array
 **/
        else
        {
            while(k!=v)
            {
                d++;
                apath[d]=k;
                k=path[k];
            }
        }
        d++; apath[d]=v;         //Add the starting point into        printf("%d",apath[d]);   //Output starting point        for(j=d-1;j>=0;j--)
        {
            printf("->%d",apath[j]); //Cyclic output of each node in the shortest path        }            
    } 
 }

//The vertex numbered v is the starting point and w is the end pointvoid guide_Dijkstra(MGraph g,int v,int w)
{
    int dist[NUM],path[NUM];
    int S[NUM];                         //S[i]=1 means that the vertex i is in S, S[i]=0 means that the vertex i is in U    int MINdis,i,j,u;
    for(i=1;i<=;i++)
    {
        dist[i]=[v][i];           //Distance initialization (distance from vertex v)        S[i]=0;                         //S[]Empty        if([v][i]<INFINITY)       //Path initialization            path[i]=v;                  //When there are edges from vertex v to vertex i, the previous vertex i is set to vertex v        else
            path[i]=-1;                 //When vertex v reaches vertex i, the previous vertex i is set to -1     }
     S[v]=1;path[v]=0;                  //Put the source point number v into S     for(i=1;i<=-1;i++)          //Loop until the shortest path of all vertices is found     {
         MINdis=INFINITY;                //MINdis sets the initial value of maximum length         for(j=1;j<=;j++)       //Select the vertex u that is not in S (i.e. in U) and has the minimum and shortest path length         {
             if(S[j]==0 && dist[j]<MINdis)
             {
                 u=j;
                 MINdis=dist[j];
             }
         }
             S[u]=1;                   //The vertex u is added to S             for(j=1;j<=;j++)  //Modify the shortest path of the vertex that is not in S (i.e. in U)             {
                 if(S[j]==0)
                     if([u][j]<INFINITY && dist[u]+[u][j]<dist[j])
                     {
                         dist[j]=dist[u]+[u][j];
                         path[j]=u;
                     }
              }  
      }
      
      guide_Dispath_two(g,dist,path,S,v,w); //The shortest output path }
 /**
 Load the scenic spot information in the file into an array
 **/
 void load_sight_data(char data[][200],MGraph g)
{
    FILE *fp;
    int i;
    
    if((fp=fopen("C:\\Users\\admin\\Desktop\\Navigation\\sight_data.txt","r"))==NULL){
        printf("File can't open");
        return;
    }

    for(i=1;i<=;i++)
    {
        fgets(data[i],200,fp);
    }
    fclose(fp);
}

/**
 Modify attractions information
 **/
void change_sight_data(char user_change_data[200],MGraph g)
{
    FILE *fin,*ftp;
    int i;
    fin=fopen("C:\\Users\\admin\\Desktop\\Navigation\\sight_data.txt","r");//Read to open the original file    ftp=fopen("C:\\Users\\admin\\Desktop\\Navigation\\","w");//Write to open temporary file    if(fin==NULL || ftp==NULL)
    {
        printf("Failed to open the file");
        return; 
     }
     
    for(i=1;i<=;i++)
    {
        char change_data[200];
        fgets(change_data,200,fin);
        if(change_data[0]==user_change_data[0] && change_data[1]==user_change_data[1])
        {
            fputs(user_change_data,ftp);// Use fputs to directly write user_change_data to the file without line breaks, and need to add line breaks.            fprintf(ftp,"\n");
        }
        else
        {
            fputs(change_data,ftp);//When using the fgets function to get the change_data array, the newline character will be saved, and there is no need to add newline character here.        }
    }
    fclose(fin);
    fclose(ftp);
    remove("C:\\Users\\admin\\Desktop\\Navigation\\sight_data.txt");
    rename("C:\\Users\\admin\\Desktop\\Navigation\\","C:\\Users\\admin\\Desktop\\Navigation\\sight_data.txt");
}

/**
 Add attractions to the file
 **/
void add_sight_data(char change_data[200],MGraph g)
{
    FILE *fp;
    if((fp=fopen("C:\\Users\\admin\\Desktop\\Navigation\\sight_data.txt","a"))==NULL){
        printf("File can't open");
        return;
    }
    fprintf(fp,"\n");
    fputs(change_data,fp);
    fprintf(fp,"\n");
    rewind(fp);
    
    fclose(fp);
 }
 
 /**
 Modify the password and save it to the password file
 **/
 void change_password(char password[30])
 {
     FILE *fp;
     fp=fopen("C:\\Users\\admin\\Desktop\\Navigation\\password_temp.txt","w");
    if(fp==NULL)
    {
        printf("Failed to open the file");
        return; 
     }
     
     fputs(password,fp);
     fclose(fp);
     remove("C:\\Users\\admin\\Desktop\\Navigation\\");
     rename("C:\\Users\\admin\\Desktop\\Navigation\\password_temp.txt","C:\\Users\\admin\\Desktop\\Navigation\\");
  }
  
  /**
      Pack the password into the password array
 **/
 void load_password(char password[30])
  {
      FILE *fp;
      if((fp=fopen("C:\\Users\\admin\\Desktop\\Navigation\\","r"))==NULL){
        printf("File can't open");
        return;
    }
    
    fgets(password,30,fp);
    
    fclose(fp);
 } 
 
 int admin_Menu()
 {
     int c;
    printf("************ Management System*********************\n"); 
    printf("\t\tWelcome to the administrator operating system\n");
     printf("1. Modify the login password\n");
    printf("2. Add new attractions\n");
    printf("3. Modify the attractions information\n");
    printf("4. Create a new attraction path\n");
    printf("5.Exit\n");
    printf("**************************************\n");
    printf("Please enter your selection:");
    scanf("%d",&c);
    return c; 
     
 }
 
int main()
{
    GreateMGraph(17);
    char sight_data[NUM][200];
    char password[30];
    int choice;
    int Menu_choice;
    int start,end;
    do
    {
        printf("Welcome to use the navigation system of Henan University of Finance and Economics\n");
        printf("Your identity is: 1. Administrator  2. Visitor  3. Exit\n");
        scanf("%d",&choice);
        if(choice==2)
        {
            do
            {    
                Menu_choice=Menu();
                if(Menu_choice==1)
                {
                    printf("Please enter your current location:");
                    scanf("%d",&start);
                    printf("\n");
                    printf("Please enter the location you want to reach:");
                    scanf("%d",&end);
            
                    guide_Dijkstra(G,start,end); 
                }
                if(Menu_choice==2)
                {
                    int sight_num;
                    load_sight_data(sight_data,G);
                    printf("Please enter the number of the attraction you want to query:");
                    scanf("%d",&sight_num);
                    Info(sight_num,sight_data);
                }
                if(Menu_choice==3)
                {
                    break;
                }
            
            }while(1);
        }
    
        if(choice==1)
        { 
            getchar();
            load_password(password);
            char user_input_psw[30];
            printf("Please enter the administrator's login password:");
            gets(user_input_psw);
            if(strcmp(password,user_input_psw)==0)
            {
                 printf("The password is correct!\n"); 
                int admin_choice;
                 do
                 {
                     admin_choice=admin_Menu();
                     
                     if(admin_choice==1)
                     {
                        getchar();
                        char user_change_psw[30];
                        printf("Please enter a new password:\n"); 
                         gets(user_change_psw);
                        change_password(user_change_psw);
                        load_password(password);
                        printf("Password modification is successful!\n"); 
                     }
                     if(admin_choice==2)
                     {
                         getchar();
                         char uadd_sight_data[200];
                        printf("Please enter the attraction you want to add and the attraction information:\n");
                        gets(uadd_sight_data);
                        add_sight_data(uadd_sight_data,G);
                        load_sight_data(sight_data,G);
                        ++;
                        
                        char *p;
                        p=strtok(uadd_sight_data," ");
                        p=strtok(NULL," ");
                        [].number=;
                        [].sight=p;
                        
                        printf("Added successfully!\n"); 
                     }
                     if(admin_choice==3)
                     {
                         getchar();
                         char user_sight_data[200];
                         printf("Please enter the attraction information you want to modify:\n");
                         gets(user_sight_data);
                         change_sight_data(user_sight_data,G);
                         load_sight_data(sight_data,G);
                         printf("Modification was successful!\n");
                     }
                     if(admin_choice==4)
                     {
                         int new_start,new_end;
                         int length;
                         printf("Please enter the route between which two points you want to add:\n");
                         printf("starting point:");
                        scanf("%d",&new_start);
                        printf("\n");
                        printf("end:");
                        scanf("%d",&new_end);
                        printf("Please enter the distance between the two vertices:\n");
                        scanf("%d",&length);
                        printf("\n");
                        
                        [new_start][new_end]=[new_end][new_start]=length;
                        printf("Route added successfully!\n"); 
                     }
                     if(admin_choice==5)
                     {
                         break;
                     }
                 }while(1);
             }
         
             if(strcmp(password,user_input_psw)!=0)
             {
                 printf("Error password! \n"); 
              } 
         }
         
         if(choice==3)
         {
             break;
          } 
    }while(1);
    
    return 0; 
 } 

Files are used to store passwords
The sight_data.txt file is used to store attractions information:

1 The gate is the only way to get to and from the school
2 Administrative office building One of the most impressive buildings in the school
3 North District Metalwork Training Center, and several rows of classrooms with a sense of historical vicissitudes
4 Teaching Building No. 1 Mainly has small classrooms for English and professional classes
5 Teaching Building No. 2 is mainly used for professional courses, with a voice room on the fifth and sixth floors
6 Experimental Building Where students take various experimental classes
7 Teaching Building No. 3 There is a large classroom, which is generally arranged to take basic courses
8 Library The school provides students with a place for study and self-study, and it is also the place with the most collection of books in the school.
9 Boiling Water Room The only place in the school that provides hot water for classmates
10 Supermarket The school’s only medium-sized supermarket, where you can buy various daily necessities
11 Liuxinyuan The student canteen is a good environment. Because it is close to the girls' apartment, there are more girls in this canteen.
12 Bathroom center The environment is OK but the scale is too small, and the supply is in short supply every day
13 Lixiuyuan mainly deals in pasta. Our school’s affordable canteen is located in the boys’ apartment area, where most boys eat here
14 Comprehensive Building A teaching building with a long history, next to it is the Second Student Club, and the school party is held here
15 Swimming Pool Where to take swimming lessons for freshmen
16 Main track and field Standard 400m track, location for students to take outdoor physical education classes
17 Comprehensive Cultural and Sports Hall: The place where indoor physical education classes is newly built and more impressive

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.