Professor Zhou Ligong has dedicated years to developing the courses "Programming and Data Structure" and "Programming for AMetal Framework and Interface (I)." After the publication of these materials, a wave of learning swept through the electronics industry. With Professor Zhou’s authorization, this public account is serializing the content from the book "Programming and Data Structure," offering it as a valuable resource for learners.
The second chapter focuses on programming techniques, specifically covering 2.2.5: Structure Arrays. This section introduces the concept of using arrays of structures in C programming, which is essential for organizing complex data efficiently.
As an example, we will use a console menu system to demonstrate how to implement a multi-branch selection structure. Typically, a menu includes options such as creating a new file, opening an existing file, saving the current file, and exiting. These functions can be organized into a function table using a structure array.
Each menu option can be represented by a string, such as "new file," "open file," "save file," and "exit." By defining a structure type called `CmdEntry`, we can store both the function pointer and the corresponding menu text.
We then declare a structure array, `cmdArray`, which acts as a function table. Each element of this array contains the function address and its associated menu description. For instance, `cmdArray[0]` might represent the "new file" option, while `cmdArray[1]` could represent "open file."
Accessing the members of a structure array follows standard syntax. To access the help message of the third entry, we would write `cmdArray[2].cHelp`. It's important to note that the array index comes before the member name.
Additionally, using a pointer to a structure allows us to dynamically reference and call functions. For example, `pfuncmd = cmdArray[0].pFunc;` assigns the address of the first function to the pointer `pfuncmd`.
This approach significantly improves program scalability. Adding a new function only requires updating the structure array, without modifying the core logic of the program. This not only enhances flexibility but also reduces the likelihood of errors.
To further illustrate this, we provide a sample code snippet demonstrating the implementation of a console menu using a structure array. The code includes the definition of the `CmdEntry` structure, the initialization of the function table, and the logic for handling user input.
As an exercise, you are asked to sort an array of `employeeArray` structures based on specific criteria. The sorting should be done in the following order:
1. Sort by `id` in ascending order.
2. If `id` values are equal, sort by `weight`, `age`, and `height` in ascending order.
3. If all previous fields are equal, sort by `bloodType` in the order A → B → O → AB, and if still equal, sort by `id`.
Use the `bubbleSort()` algorithm to complete this task and output the sorted results. This exercise reinforces your understanding of how to work with structured data and custom sorting logic in C programming.
Indoor Led Display,Indoor digital signage,LED Display for Indoor Use,Digital signage LED Screen,Indoor Full HD LED Display,Indoor LED Display Panel
Shenzhen Xinfei Century Technology Co., Ltd. , https://www.rgbdancing.com