C/C++ data structure course design? Analysis and summary of campus navigation based on easyX visualization project

Table of contents

Preface (important, don't skip it!)

Project Analysis

Basic needs analysis

logical structure analysis

Storage structure analysis

Create an undirected graph

Initialize the undirected graph

Analysis and Selection of the Shortest Path Algorithm

Simple file reading and writing based on file stream

Advanced Needs Analysis

Summary and Reflection


Preface (important, don't skip it!)

First of all, why is there this article? It took me three months to finish learning data structure and C++, but I just stayed at the stage of knowing and using it, without in-depth thinking and understanding. This kind of learning is not practical for me. Yes, so I still want to make something out, no matter how big or small this thing is, it must be rewarding for me, so I found the theme project [Campus Navigation] on the Internet, some schools put it As a course design, so explain in advance: this article will not show the code , only provide the general process and ideas, if you want to copy the answer and complete the homework, you can leave it!

Of course, if you are a computer enthusiast eager for technology, you may wish to read the summary of my experience in this project, and welcome to point out mistakes and discuss together.

Furthermore, the applicable population of this project?

Before starting, I still want to mention this project: Although it is a C++ and data structure project, the knowledge of C++ or data structure that needs to be used is not difficult, so is this project meaningless? If you have certain project experience and have further thinking and experience on C++ and data structures, then the answer is: yes;

But as a beginner like me, it is especially helpful for students who are scribbled to cope with exams, or even take the postgraduate entrance examination data structure. The image of C++ only stays in C plus object-oriented), or this project is used to consolidate part of the knowledge of C++ and data structures, and specifically understand the actual application scenarios of data structures. You may say that I can learn LeetCode or PAT Class A differently, my suggestion is: both are equally important! Besides, brushing questions cannot accumulate project experience.

At the same time, the development cycle of this project is short, which is suitable for fast in and fast out.


Next, I will try my best to take you through a complete project process:

Project Analysis

Project requirements:

C++ data structure project - wicked navigation assistant (I named it ( • ̀ω•́ )✧)

【Problem Description】

Design a campus map of a certain university to provide various service information query services for freshmen.

【basic requirements】

  1. The map contains no less than 12 locations. Use vertices in the graph to represent information such as campus locations, code names, and brief introductions; use edges to represent paths, store path lengths, and other information.
  2. Design the logical structure of the map, and express this structure with C/C++ language.
  3. Please use C/C++ language to write a program to provide freshmen with information query for any location in the map, and provide freshmen with a query for directions between any locations in the map, that is, a shortest path between two points (Note: North-South Campus only accessible via underground corridors).
  4. Using elementary data structures and algorithms, give the corresponding algorithm design, implementation, and time complexity analysis.

【Advanced requirements】

  1. Design a simple UI interface and implement simple interactive functions.
  2. Design the physical structure of the map and visualize this structure graphically.
  3. The relevant information of the corresponding location can be queried through a certain information. (fuzzy search)

【Test Data】

As a developer:

  1. Enter the place names and related information of various places on campus;
  2. Enter the path length between two adjacent locations;

As a user:

  1. Query any location information of Tatung University;
  2. Query the length of the shortest path between two locations.

[Implementation Tips] In general, the roads in the campus are two-way traffic, and the campus flat network can be set to be an undirected graph. Both vertices and edges contain information.

Basic needs analysis

logical structure analysis

Construct a logical map with a weighted undirected graph, regard each location as a vertex, the path between locations as an edge, and set the length of the road as the weight of the edge;

Storage structure analysis

Using the adjacency matrix matrix is ​​the most convenient structure for storing undirected graphs, which is convenient for accessing vertices, and judging whether there are edges between points and points, and conveniently accessing the weights of edges;

In addition, it is necessary to store the number, name, introduction and other related information of each location, so they are encapsulated into a structure vex; vex has three member variables of name, label and introduction, and the vex[vexsnum] structure ( or class) array stores the basic information of all locations in the map;

Encapsulate vex and matrix with graph;

Create an undirected graph

Seventeen on-campus locations were selected, namely:

(numbered from 0 to 16)

Robot Laboratory, Library, North Gate, Medical College, Tianjian Stadium, Mingli Building, South Gate, Aoyu Hall, Qingheyuan Building 10, Hawaii Playground, School Hospital, Bathing Garden, Four Small Gardens, Underground Corridor A1A2B1B2 .

(numbered from 0 to 28)

Set a total of 29 edges (undirected edge/weight: unit ten meters)

A0 Robotics Lab - Library 25

A1 Robotics Laboratory - North Gate 50

A2 Robotics Lab - School of Medicine 90

A3 Robotics Lab——Tianjian Stadium 100

A4 Library - North Gate 25

A5 North Gate - Channel A2 1

A6 Channel A2 - School of Medicine 75

A7 School of Medicine——Tianjian Stadium 2

A8 Faculty of Medicine - Library 90

B9 Channel A2 - Channel A1 5

Channel B10 A1——Mingli Building 2

Channel A1 of B11——South Gate 35

Channel A1 of B12——Sixiaoyuan 20

B13 Channel A1——Hawaii Playground 20

B14 Mingli Building - Four Small Gardens 19

B15 Four Small Gardens——Hawaii Playground 4

B16 Four Small Gardens - Bathing Garden 5

B17 Four Small Gardens - School Hospital 6

B18 School Hospital - Bathing Garden 3

B19 School Clinic——Seiwa 50

B20 Qinghe - Aoyu Hall 3

B21 Seiwa - Nandaimon 30

B22 Qinghe——Hawaii Playground 40

B23 Hawaii Playground - South Gate 18

B24 Hawaii Playground - School Hospital 4

B25 Aoyu Hall - Channel B1 10

Channel B1 of B26——South Gate 3

B27 Channel B1 - Channel B2 5

B28 Channel B2 - School of Medicine 3

Initialize the undirected graph

Because the scope of navigation is only within the two campuses of the school, the possible amount of data is not large, so you can directly save relevant data such as commonly used locations in the project, or save it locally in the form of a file, or in a database.. .Only when the user wants to add an unrecorded location information, perform the construction of a new map, and then update and save it, without the need to enter new data every time, which improves the user experience.

As for how to realize the above functions, the simplest method is used here, directly storing the location information in the form of an array, and traversing the assignment during initialization.

Analysis and Selection of the Shortest Path Algorithm

There are two shortest path algorithms, Dijkstra and Floyd, where Dijkstra's time complexity is O(N^2), which is to calculate the shortest path between a certain point and other points; Floyd's time complexity is O(N^3) , the space complexity is O(N^2). The usual method is to use the Dijkstra algorithm to calculate the shortest path between two points when the user queries (navigation), but considering that the object-oriented is new, the most frequent operation is to search or navigate, that is, to calculate the shortest path, if When the user calculates the shortest path for the first time, the Path array and Dist array generated by Floyd are saved locally, and each subsequent shortest path search only needs to directly access the array subscript, so the time complexity is reduced to O(1) , and most of the time is O(1), it only needs to be updated when the user adds or modifies the graph, which greatly reduces the number of repeated calculations and time complexity.

Of course, in addition to query operations, it is still necessary to provide users with an interface for adding or deleting data. The idea here is the same as above, directly reading and writing in local files is also to optimize user experience.

Simple file reading and writing based on file stream

The reading and writing of files is the soul of this project, because only by saving files locally and implementing read and write operations can the time complexity of finding the shortest path be reduced to O(1), and the user experience is optimized. It enables users to freely add, delete, check and modify data in [Developer Mode], and ensures that the previous operations are retained when the project is started next time.

Advanced Needs Analysis

The advanced requirement is to realize simple image display, and the graphics library based on EasyX is used here. For the introduction of EasyX, there are many detailed tutorials on the forum, so I won’t go into details. If the interface of the basic requirements is encapsulated in the previous process, this step is not difficult. As for the text display problem, I recommend overloading calculations. symbols, which can reduce excessive repetitive steps. 

Of course, the visual window should be designed in the project analysis stage. You can use tools (such as processOn) to draw a rough setting idea, which determines the interface parameters you customize and the effect you want to achieve. Write the code later The time is very important. (lesson)


Show some effects:

 

As for the fuzzy search, at the beginning I planned to cut a certain information according to a fixed step size, and then use map to store each keyword and set to store the corresponding subscript. ) fuzzy search function, considering that the amount of data in this project is not large, it is more convenient to use this function.

The results obtained by searching for the keyword [Wudian]:

Summary and Reflection

My harvest:

In this project, the biggest gain for me was to experience a relatively complete project process, and also gave me a lot of project experience. It can be said that I really experienced the analysis and application of the shortest path algorithm and achieved the goal of consolidating the data structure. Effect. Furthermore, I don't have a deep understanding of the characteristics of the C++ language, and I haven't been able to use the relevant knowledge of C++ such as object-oriented and STL to realize the functions as much as possible. I need to deepen the application of STL by brushing the questions. In addition, in the function of writing the map, I defined some global variables in order to be lazy. I thought that the space would not be a problem on the embedded machine, but it still caused a stack overflow, and I still need to open up the data in the heap in the future;

At the same time, although this project is small, there are still some advanced ideas that have not been realized. For example, the animation of the shortest path is intuitively displayed on the map. On the one hand, I am too good at learning and applying easyX. How to learn some basic operations such as QT, that is, to know more or less about the front-end and back-end knowledge, and not just focus on the technology stack I am in, which is not an advantage for future work; During the drafting stage of the project, I didn’t think deeply about the interface in the future. I started writing as soon as I came up. I didn’t think about the encapsulation and expansion in the later stage. As a result, I kept changing the formal parameters in the process of writing the project. There is no experience in architecture.

Guess you like

Origin blog.csdn.net/ZER00000001/article/details/126294000