Campus navigation assistant - course design

Posted by derezzz on Tue, 01 Feb 2022 13:34:07 +0100

Neusoft campus navigation assistant

Function description

1. Design the campus plan. Among them, there are at least 12 representative places. The vertex in the plan represents the representative place in the school, and the weight on the edge represents the distance between the two points;

2. In order to realize the management of the sub functions of campus navigation system, the main control menu is designed;

3. Provide visitors with the query of relevant information at any place in the figure;

four. Provide visitors with the query of the shortest route between any two places in the map and the query of the shortest route from the pointing place to all other places.

 

Specific functions

This program includes three modules: main program module, work area module and undirected network operation module. The calling relationship is shown in the figure.

The main interface is as follows:

Figure 1 main interface

 

1. Store the code, name, brief introduction and other information of scenic spots for users to query, and provide visitors with the query of relevant information of any scenic spots in the figure, as shown in the figure (Note: the following is only an example, including 12 scenic spots, which does not represent the real map of the college).  

Figure 2 query information of each scenic spot

 

 2. The shortest route and distance from the scenic spot to other scenic spots can be calculated according to the starting scenic spot number entered by the user, as shown in the figure.

Figure 3 query the shortest route and distance from the designated scenic spot to all other scenic spots

 

 3. Provide visitors with the shortest path query between any scenic spots in the figure, as shown in the figure.

Figure 4 shortest path query between any scenic spots

 

 4. Select the modify map information in the main interface, and a submenu will appear. You can add and delete scenic spots or edges for the campus plan, and modify the weights on the edges. As shown in the figure.

Figure 5. Information of modified figure

The submenu function is not displayed. The source code is given below. Try it yourself. The compiler uses Dev C++ 5.6.3. There should be no serious bugs, and there may be small bugs. This course design is enough, so there is no excessive debugging.

 

code

Only part of the code is annotated, and the core things are basically annotated. There are too many others to annotate. You can tell me what you don't understand or have suggestions.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define INFINITY 65535 / * ∞ is set to the maximum value of double byte unsigned integer 65535*/
#define Max 100           
typedef int Vertex;         /* The vertex is represented by the vertex subscript, which is an integer */
typedef int WeightType;        /* The weight of the edge is set to integer */

int Dist[Max][Max], Path[Max][Max];  /*Global variables, easy to use Freud to find the shortest path */

typedef struct DataType
{
	int num;      //Location serial number
	char name[100];  //Location name
	char information[1000];  //It can't be simpler
}DataType;

typedef struct MGraph
{
	DataType Data[13];   //There are 12 vertices in the initial graph 
	WeightType G[Max][Max]; //The arc weight between two points in the figure, that is, the distance 
	int Nv, Ne;    // Number of vertices and edges in the graph 
}MGraph;

void Menu();//First level menu 
void Menu2(); //Secondary menu 
int choose(MGraph* Map);//First level menu selection
void choose2(MGraph* Map); //Function selection of secondary menu 
MGraph* CreateGraph(); //Initialization diagram  
MGraph* ShortFloyd(MGraph* Map);  //Floyd algorithm to find the shortest path 
void Data_Observe(MGraph* Map);//Overview of scenic spots 
void ShortPath_Observe(MGraph* Map);//Shortest path query 
void Path_Observe(MGraph* Map); //Path query 
void Data_Ob(MGraph* Map);//View scenic spot information 
void New_CreateGraph(MGraph* Map); //Rebuild diagram 
void UpDate(MGraph* Map); //Update scenic spot information 
void Add_Node(MGraph* Map); //Increase scenic spots 
void Add_Edge(MGraph* Map); //Add path 
void Delete_Node(MGraph* Map); //Delete attractions 
void Delete_Edge(MGraph* Map);//Delete path 

int main()
{
	int i, j;
	MGraph* Map;
	for (i = 1; i <= Map->Nv; i++)
		for (j = 1; j <= Map->Nv; j++)
			Dist[i][j] = INFINITY;    //  The shortest path between any two points is initialized to a maximum value 
	Map = CreateGraph();
	Map = ShortFloyd(Map);
	return choose(Map);

}

void Menu()   //  First order interface function 
{
	printf("\n\n\n");
	printf("--------------------------------------------------------------------------------------\t\n");
	printf("                                Welcome to campus navigation assistant                                \t\n");
	printf("                                                                                      \t\n");
	printf("                                Welcome to Guangdong Neusoft college!                               \t\n");
	printf("                                                                                      \t\n");
	printf("                    1.Overview of scenic spot information                 2.Shortest route query                     \t\n");
	printf("                                                                                      \t\n");
	printf("                    3.Route information query                 4.Scenic spot information query                     \t\n");
	printf("                                                                                      \t\n");
	printf("                    5.Change diagram information                   6.sign out                             \t\n");
	printf("                                                                                      \t\n");
	printf("--------------------------------------------------------------------------------------\t\n");
	printf("\n\n Please select the function you need and enter the code:\n");
}
void Menu2() //Secondary interface menu 
{
	printf("\n\n\n");
	printf("--------------------------------------------------------------------------------------\t\n");
	printf("                                                                                      \t\n");
	printf("                    1.Build map again                 2.Update information                             \t\n");
	printf("                                                                                      \t\n");
	printf("                    3.Increase scenic spots                 4.Add path                             \t\n");
	printf("                                                                                      \t\n");
	printf("                    5.Delete attractions                 6.Delete path                             \t\n");
	printf("                                                                                      \t\n");
	printf("                                    7.sign out                                            \t\n");
	printf("                                                                                      \t\n");
	printf("--------------------------------------------------------------------------------------\t\n");
	printf("\n\n Please select the function you need and enter the code:\n");
}
int choose(MGraph* Map) //First level menu selection 
{
	int num;
	while (1) {
	a:		Menu();//  Output interface bar 
		scanf("%d", &num);//Accept options 
		system("pause");
		system("cls");
		switch (num)
		{
		case 1: Data_Observe(Map);
			system("pause");
			system("cls");
			goto a;
		case 2:ShortPath_Observe(Map);
			system("pause");
			system("cls");
			goto a;
		case 3:Path_Observe(Map);
			system("pause");
			system("cls");
			goto a;
		case 4:Data_Ob(Map);
			system("pause");
			system("cls");
			goto a;
		case 5:choose2(Map);
			system("pause");
			system("cls");
			goto a;
		case 6: return 0;
		default:
			break;
		}

	}
}
void choose2(MGraph* Map)
{
	int num;
	while (1) {
	a:		Menu2();//  Output interface bar 
		scanf("%d", &num);//Accept options 
		system("pause");
		system("cls");
		switch (num)
		{
		case 1: New_CreateGraph(Map);
			system("pause");
			system("cls");
			goto a;
		case 2:UpDate(Map);
			system("pause");
			system("cls");
			goto a;
		case 3:Add_Node(Map);
			system("pause");
			system("cls");
			goto a;
		case 4:Add_Edge(Map);
			system("pause");
			system("cls");
			goto a;
		case 5:Delete_Node(Map);
			system("pause");
			system("cls");
			goto a;
		case 6:Delete_Edge(Map);
			system("pause");
			system("cls");
			goto a;
		case 7:choose(Map);
		default:
			break;
		}
	}

}

MGraph* CreateGraph()   //  Initialization diagram 
{
	MGraph* Map;
	int i, j, k;
	Map = (MGraph*)malloc(sizeof(MGraph));  //Open up memory 

	Map->Ne = 22;   //  The figure has 22 edges 
	Map->Nv = 13;   //   The graph has 12 vertices 
	for (i = 1; i <= Map->Nv; i++)
		Map->Data[i].num = i;    //Initialization vertex numbers are 1-12
	for (j = 1; j <= Map->Nv; j++)
		for (k = 1; k <= Map->Nv; k++)
		{
			Map->G[j][k] = INFINITY;  // Initialize the distance between each two points to a maximum 
		}


	Map->G[1][2] = Map->G[2][1] = 1200;   //The road outside the north gate and the south gate is 1200 meters away
	Map->G[1][6] = Map->G[6][1] = 350;    //350 from north gate to block AB
	Map->G[2][3] = Map->G[3][2] = 20;     //South gate to the National Academy of Education 20
	Map->G[2][4] = Map->G[4][2] = 600;    //South gate to administration building 600
	Map->G[2][11] = Map->G[11][2] = 15;   //South gate to College of continuing education 15
	Map->G[2][12] = Map->G[12][2] = 200;   //South gate to library 200
	Map->G[3][9] = Map->G[9][3] = 450;      //From National Academy of education to dormitory 450    
	Map->G[3][10] = Map->G[10][3] = 500;  	//500 from the National Academy of education to the second canteen
	Map->G[4][6] = Map->G[6][4] = 30;  	   //30 from administration building to block AB
	Map->G[4][11] = Map->G[11][4] = 250;   //250 from administration building to College of Continuing Education
	Map->G[4][12] = Map->G[12][4] = 370;   //Administration building to library 370
	Map->G[5][6] = Map->G[6][5] = 150;  //150 from stadium to block AB
	Map->G[5][8] = Map->G[8][5] = 200;  //Block AB to block C 200
	Map->G[6][12] = Map->G[12][6] = 380; //Block AB to library 380
	Map->G[7][8] = Map->G[8][7] = 100;   //Block EF to block C 100
	Map->G[7][9] = Map->G[9][7] = 100;   //Block EF to dormitory 100
	Map->G[7][10] = Map->G[10][7] = 30; //Block EF to canteen 30
	Map->G[7][12] = Map->G[12][7] = 50;  //Block EF to library 30
	Map->G[8][10] = Map->G[10][8] = 60;  //Block C to canteen 60	
	Map->G[8][12] = Map->G[12][8] = 20;  //Block C to library 20
	Map->G[9][10] = Map->G[10][9] = 50;  //Dormitory to canteen 50
	Map->G[10][12] = Map->G[12][10] = 40;  //Dormitory to library 40


	strcpy(Map->Data[1].name, "North Gate of school");   //Store the name of each point in the diagram and assign values in turn
	strcpy(Map->Data[2].name, "South main entrance of College");
	strcpy(Map->Data[3].name, "Institute of International Education");
	strcpy(Map->Data[4].name, "Administration building");
	strcpy(Map->Data[5].name, "stadium");
	strcpy(Map->Data[6].name, "AB Teaching building");
	strcpy(Map->Data[7].name, "EF Teaching building");
	strcpy(Map->Data[8].name, "C Teaching building");
	strcpy(Map->Data[9].name, "Staff Dormitories");
	strcpy(Map->Data[10].name, "School 2 canteen");
	strcpy(Map->Data[11].name, "College of Continuing Education");
	strcpy(Map->Data[12].name, "library");

	strcpy(Map->Data[1].information, "The Chinese division of this sect has recently");    //Store an introduction to each attraction in the diagram 
	strcpy(Map->Data[2].information, "School gate, school bus entrance and exit");
	strcpy(Map->Data[3].information, "The international window of the college is 6 floors high");
	strcpy(Map->Data[4].information, "Administrative office building, 6 floors high");
	strcpy(Map->Data[5].information, "Outdoor standard basketball court");
	strcpy(Map->Data[6].information, "One of the earliest teaching buildings and the largest department of the College--The computer department is here! The building is 4 floors high");
	strcpy(Map->Data[7].information, "The computer department has a centralized classroom, a flipped classroom area and a building height of 4 floors");
	strcpy(Map->Data[8].information, "Laboratory concentration area, 4 floors high");
	strcpy(Map->Data[9].information, "Freshmen's dormitory, teacher's apartment, professor's apartment, 5 floors high");
	strcpy(Map->Data[10].information, "Dry meal people rush!!!");
	strcpy(Map->Data[11].information, "Nearest to the bus stop, you can take the bus when you go out");
	strcpy(Map->Data[12].information, "The building area is 46000 square meters and the building is 4 floors high");
	return Map;
}

MGraph* ShortFloyd(MGraph* Map)    //  Freud algorithm, find the shortest path 
{

	int v, w, k; // v. W and K represent the starting point, destination and newly added point respectively 
	for (v = 1; v < Map->Nv; v++) {
		for (w = 1; w < Map->Nv; w++)
		{
			Dist[v][w] = Map->G[v][w]; // Dist represents the shortest path between two points and is initialized as the weight between two points 
			Path[v][w] = w;   //  Path records the previous point of the shortest path 
		}
	}
	for (k = 1; k < Map->Nv; k++) //The shortest path of each two points is found by using the third-order cycle 
	{
		for (v = 1; v < Map->Nv; v++)
		{
			for (w = 1; w < Map->Nv; w++)
			{
				if ((Dist[v][k] != INFINITY || Dist[k][w] != INFINITY) && (Dist[v][w] > (Dist[v][k] + Dist[k][w])))
				{
					Dist[v][w] = Dist[v][k] + Dist[k][w];
					Path[v][w] = Path[v][k];
					/* If the path composed of newly added points is less than the minimum path, update the minimum path,
					And add the newly added points to the shortest path */
				}
			}
		}
	}
	return Map;  // Returns the type of the graph 
}
void Data_Observe(MGraph* Map)//Overview of scenic spots 
{
	int i;
	printf("\n Guangdong Neusoft college has the following%d Attractions:\n\n",Map->Nv-1);
	for (i = 1; i < Map->Nv; i++)
	{
		printf("%d.", Map->Data[i].num);   //Output attraction number 
		printf("%s: ", Map->Data[i].name);   //Output scenic spot name 
		printf("%s\n\n", Map->Data[i].information);  // Introduction to export attractions 
	}
}

void ShortPath_Observe(MGraph* Map) //Shortest path query 
{
	int start, pass, end;
	printf("Please enter the current scenic spot number and the scenic spot number you want to go to(Space separated):\n");
	scanf("%d %d", &start, &end);   // start end accepts the starting point and destination numbers respectively 
	while (start < 1 || start>Map->Nv-1 || end < 1 || end>Map->Nv-1)  //illegal input 
	{
		printf("Input error, please re-enter:\n");
		scanf("%d %d", &start, &end);
	}
	if (start == end)  //illegal input 
	{
		printf("Input error, you are already here,Please re-enter:\n");
		scanf("%d %d", &start, &end);
	}
	system("cls");
	printf("\n%s->%s The minimum path for is:%d rice\n", Map->Data[start].name, Map->Data[end].name, Dist[start][end]);
	pass = Path[start][end];  // pass is used as an intermediate variable to accept the parent node in the shortest path 
	printf("\n The shortest path is:   %s", Map->Data[start].name);   // Output shortest path 
	while (pass != end)
	{
		printf("-> %s", Map->Data[pass].name);
		pass = Path[pass][end];  //  Constantly update the pass intermediate node 
	}
	printf("-> %s\n", Map->Data[end].name);

}

void Path_Observe(MGraph* Map) //Path query 
{
	int i, j;
	int num;
	printf("Please enter a starting attraction number:");
	scanf("%d", &num);
	while (num < 1 || num>Map->Nv-1)  //illegal input 
	{
		printf("Input error, please re-enter:\n");
		scanf("%d", &num);
	}
	system("cls");
	printf("--------------------------------------------------------------------------------------\t\n");
	for (i = 1; i < Map->Nv; i++) {
		if (i != num) {
			j = Path[num][i];  // l is used as an intermediate variable to accept the parent node in the shortest path 
			printf("\n The route is: %s", Map->Data[num].name);   // Output shortest path 
			while (j != i)
			{
				printf("-> %s", Map->Data[j].name);
				j = Path[j][i];  //  Constantly update l nodes 
			}
			printf("-> %s\n", Map->Data[i].name);
			printf("%s reach%s The total length of the route is: %d rice\n", Map->Data[num].name, Map->Data[i].name, Dist[num][i]);
			printf("--------------------------------------------------------------------------------------\t\n");

		}
	}
}

void Data_Ob(MGraph* Map) //View scenic spot information 
{
	int num;
	printf("Please enter a scenic spot number:");
	scanf("%d", &num);
	while (num < 1 || num>Map->Nv-1)  //illegal input 
	{
		printf("Input error, please re-enter:\n");
		scanf("%d", &num);
	}
	system("cls");
	printf("--------------------------------------------------------------------------------------\t\n");
	printf("%d.", Map->Data[num].num);   //Output attraction number 
	printf("%s: \n\n", Map->Data[num].name);   //Output scenic spot name 
	printf("(Abbreviated Edition)%s\n", Map->Data[num].information);  // Introduction to export attractions 
	printf("--------------------------------------------------------------------------------------\t\n");
}
void New_CreateGraph(MGraph* Map) //Rebuild diagram 
{
	int NV, NE;
	int V1,V2,W;
	int i,j,k;
	printf("\n Enter the number of scenic spots and paths in the new map:\n");
	scanf("%d %d", &NV, &NE);
	Map->Nv = NV+1;
	Map->Ne = NE;
	printf("\n Enter the name of the new map%d Names of scenic spots and related information: \n",Map->Nv-1);
	for(i=1;i<Map->Nv;i++)
	{
		Map->Data[i].num = i; 
		scanf("%s", &Map->Data[i].name);
		scanf("%s", &Map->Data[i].information);
	}
	system("pause");
	system("cls");
	for (j = 1; j <= Map->Nv; j++)
		for (k = 1; k <= Map->Nv; k++)
		{
			Map->G[j][k] = INFINITY;  // Initialize the distance between each two points to a maximum 
		}
	printf("\n Building new map path:\n");
	for(i=0;i<Map->Ne;i++){
		printf("\n\n Enter connected attractions:");
		scanf("%d %d",&V1,&V2);
		printf("\n\n Enter the distance between attractions:");
		scanf("%d",&W);
		Map->G[V1][V2] = Map->G[V2][V1] = W;
	}
	ShortFloyd(Map);
}

void UpDate(MGraph* Map) //Update scenic spot information 
{
	int num; 
	int i;
	printf("\n Enter the attraction number that needs to be changed\n\n");
	for(i=1;i<Map->Nv;i++)
		printf("%d.%s: %s\n\n",i,Map->Data[i].name,Map->Data[i].information);
	scanf("%d",&num);
	while (num < 1 || num>Map->Nv-1)  //illegal input 
	{
		printf("Input error, please re-enter:\n");
		scanf("%d", &num);
	}
	system("cls");

	printf("take%s Amend to read:\n",Map->Data[num].name);
	scanf("%s",&Map->Data[num].name);   
	printf("take%s Amend to read:\n",Map->Data[num].information);
	scanf("%s",&Map->Data[num].information);
	system("cls");

} 

void Add_Node(MGraph* Map) //Increase scenic spots 
{
	int num;
	int i;
	printf("The existing scenic spots are as follows\n");
	for(i=1;i<Map->Nv;i++)
		printf("%d.%s: %s\n\n",i,Map->Data[i].name,Map->Data[i].information);
	printf("Please add new attractions in order\n\n"); 
	printf("\n Please enter serial number:");
	scanf("%d",&num);
	printf("\n Please enter the name of the attraction:");
	scanf("%s",&Map->Data[num].name);
	printf("\n Please enter the scenic spot profile:");
	scanf("%s",&Map->Data[num].information);
	Map->Nv++;
	printf("\n\n Modified successfully\n\n");
}

void Add_Edge(MGraph* Map) //Add path 
{
	int i;
	int V1,V2,W;
	printf("The existing scenic spots are as follows\n");
	for(i=1;i<Map->Nv;i++)
		printf("%d.%s: %s\n\n",i,Map->Data[i].name,Map->Data[i].information);
	printf("\n Please enter two scenic spots to add a path:"); 
	scanf("%d %d",&V1,&V2); 
	while (V1 < 1 || V1>Map->Nv-1 || V2 < 1 || V2>Map->Nv-1)  //illegal input 
	{
		printf("Input error, please re-enter:\n");
		scanf("%d %d", &V1, &V2);
	}
	if (V1 == V2)  //illegal input 
	{
		printf("Input error, please re-enter:\n");
		scanf("%d %d", &V1, &V2);
	}
	printf("\n Please enter the distance between scenic spots:");
	scanf("%d",&W);
	Map->G[V1][V2] = Map->G[V2][V1] = W;
	Map->Ne++;
	ShortFloyd(Map);

}

void Delete_Node(MGraph* Map) //Delete attractions 
{
	int num;
	int i,j;
	printf("The existing scenic spots are as follows\n");
	for(i=1;i<Map->Nv;i++)
		printf("%d.%s: %s\n\n",i,Map->Data[i].name,Map->Data[i].information);
	printf("Please enter the serial number of the scenic spot to be deleted\n");
	scanf("%d",&num);
	while (num < 1 || num>Map->Nv-1)  //illegal input 
	{
		printf("Input error, please re-enter:\n");
		scanf("%d", &num);
	}
	for(i=num;i<Map->Nv;i++){
		strcpy(Map->Data[i].name,Map->Data[i+1].name);
		strcpy(Map->Data[i].information,Map->Data[i+1].information);
	} 
	Map->Nv--;
	for(j=num;j<Map->Nv;j++){
		for(i=1;i<Map->Nv;i++){
			Map->G[j][i]=Map->G[j+1][i];
		    Map->G[i][j]=Map->G[i][j+1];
		}
	}
	for(i=1;i<Map->Nv;i++){
		if(Map->G[num][i]!=INFINITY||Map->G[i][num]!=INFINITY);
		   Map->Ne--;
	}
	ShortFloyd(Map);
}

void Delete_Edge(MGraph* Map) //Delete path 
{
	int num;
	int V1,V2;
	printf("Please enter the two attractions for which you want to delete the path:\n");
	scanf("%d %d",&V1,&V2);
	if(Map->Nv-1 == 1){
		printf("There is only one scenic spot at present,No path deletion"); 
		goto b;
	}
	while (V1 < 1 || V1>Map->Nv-1 || V2 < 1 || V2>Map->Nv-1)  //illegal input 
	{
		printf("Input error, please re-enter:\n");
		scanf("%d %d", &V1, &V2);
	}
	if (V1 == V2)  //illegal input 
	{
		printf("Input error, please re-enter:\n");
		scanf("%d %d", &V1, &V2);
	}
	Map->G[V1][V2]=Map->G[V2][V1]=INFINITY;
	Map->Ne--;
b:	ShortFloyd(Map);
}

Summary

Freshman life is coming to an end. It's so fast. In fact, when filling in volunteers, majors were filled in casually, and they knew nothing about computers at that time. Learning programming with a try attitude, I found that programming is really interesting and fun, which is always more fun than playing games every day. Through this program design, you must write the underlying code well, or you will get a bug when you add a function, and your head will explode. It is estimated that the school curriculum will remain unchanged for several years. I hope the younger students who will come to Neusoft soon can see this code.

Topics: C Programming data structure