This section describes how to use the stack to detect pairs of symbols in a program and implement the symbol detection function similar to the compiler, using the chain stack. I. Problem proposal and solution 1. assume that the following program is available: [cpp] # include <stdio. h> # include <stdlib. h> int main () {int a [5] [5]; int (* p) [5]; p = a [0]; printf ("% d ", & a [3] [3]-& p [3] [3]);} <> [] {} "" in this program are all paired, if it does not appear in pairs, my program will report an error during compilation. 2. The main purpose of the program we are about to write is to check whether all pairs of symbols in the program match. 3. solution: assume that the program we want to detect is a program above, we need to scan every character. When the program encounters letters, numbers, or unpaired symbols, it will be ignored directly, when the program encounters the left symbol of a pair of symbols, we press the left symbol into the stack. When the right parenthesis is encountered during the scanning process, we will pop up the top element of the stack for matching. If the matching program continues scanning, if the matching fails, an error will be reported. If all matches are successful, the stack is empty and all characters cannot be scanned. If no matching is successful, the matching fails or the stack is not empty. 4. the algorithm framework of a program is as follows: 2. Implementation of a specific program 1. the specific implementation of the program mainly uses the data structure of the chain stack. At the same time, the chain stack is implemented by reusing the one-way linked list. These are explained in this blog post after clicking the open link. Therefore, the main part of Algorithm Implementation is implemented in the main function, that is, the so-called upper-layer function. 2. First, you can call other functions in the main function to run the entire program. [Cpp] int main () {char * a = "# include <stdio. h> # include <stdlib. h> int main () {int a [5] [5]; int (* p) [5]; p = a [0]; printf (\ "% d \", & a [3] [3]-& p [3] [3]) ;}"; scan (a); return 0 ;} it defines a string array and transmits the first address of the string array to scan. Here, it is said that the string array may not be exact. I have never figured out the character and string problems, now. 3. scan function (1) scan function receives the pointer [cpp] int scan (char * a) of the string array (2) Call the function of the chain stack to create the stack, the returned result is a stack pointer (because the stack pointer will be used as long as stack operations are involved) [cpp] LinkStack * stack = LinkStack_Create (); (3) as described in the program algorithm, we need to constantly scan all the characters until the end of the entire string. The End mark of the string is '\ 0'. Here we use the while loop to implement [cpp] while (a [I]! = '\ 0') the first address of the passed string array. We can use this first address to define a character array and then perform operations. (4) First, determine whether the elements in the array are the left symbol of the matching symbol. If it is the left symbol, press the left symbol into the stack. [Cpp] if (left (a [I]) = 1) {LinkStack_Push (stack, (void *) (a + I ));} when we press the left symbol into the stack, we press the address of the array element into the stack, which is also a manifestation of the reusability of the chain stack. (5) Next, determine whether the elements in the array are the right symbol of the matching symbol. If it is the right symbol, the top element of the stack will pop up and be compared with the corresponding right symbol. If the top element of the stack is null or the comparison fails, an error is returned. [Cpp] if (right (a [I]) = 1) {char * bijiao = (char *) LinkStack_Pop (stack); if (bijiao = NULL) |! Match (* bijiao, a [I]) {printf ("% c \ n", a [I]); ret = 0; break ;}} (6) Finally, the condition for successful detection is that the stack top is empty and the last Terminator has been detected. [Cpp] if (LinkStack_Top (stack) = NULL & a [I] = '\ 0') {printf ("compiled successfully \ n");} (7) when the program is executed, there are several other subfunctions. 1) check whether the function is a left symbol. 2) check whether the function is a right symbol. 3) Compare the function. 3. Specific code. for detailed code of the chain stack used by the program implementation, see click open link and click open link. Here, the implementation part of the main function is stuck. As it is a cainiao, some code may be poorly written. Sorry. 2. code for implementing the main function: [cpp] # include <stdio. h> # include <stdlib. h> # include "1.h "/********************************* **************************************** * ****** Function Name: left * parameter: array element passed in by char a * return value: If the int type is the left symbol, 1 is returned, not 0 * is returned: determines whether the input character is a character ******************************** **************************************** * *****/int left (char) {int ret = 0; switch (a) {case '<': ret = 1; break; case '(': ret = 1; break; case '[': ret = 1; break; case '{': ret = 1; break; case '\ '': ret = 1; break; case '\ "': ret = 1; break; default: ret = 0; break;} return ret ;} /*************************************** **************************************** * Function Name: right * parameter: array element passed in by char a * return value: If the int type is the right symbol, 1 is returned instead of 0 *. function: determines whether the passed character is a character ******************************** **************************************** ****** */Int right (char a) {int ret = 0; switch (a) {case '>': case ')': case ']': case '}': case '\ '': case' \" ': ret = 1; break; default: ret = 0; break;} return ret ;} /*************************************** **************************************** * Function Name: right * parameter: array element passed in by char a * return value: If the int type is the right symbol, 1 is returned instead of 0 *. function: determines whether the passed character is a character ******************************** ************************************* * ********/Int match (char bijiao, char a) {int ret = 0; int I = 0; switch (bijiao) {case '<': ret = (a = '>'); break; case '(': ret = (a = '); break; case '[': ret = (a = ']'); break; case '{': ret = (a = '}'); break; case '\ '': ret = (a =' \''); break; case '\ "': ret = (a = '\"'); break; default: ret = 0; break;} return ret ;} /*************************************** ******************* * ******************** Function name: scan * parameter: the address of the character array element transmitted by char * a * return value: If the int type is the right symbol, 1 is returned, not 0 * is returned: ********************************* **************************************** * *****/int scan (char *) {LinkStack * stack = LinkStack_Create (); int I = 0; int ret = 0; while (a [I]! = '\ 0') {if (left (a [I]) = 1) {LinkStack_Push (stack, (void *) (a + I ));} if (right (a [I]) = 1) {char * bijiao = (char *) LinkStack_Pop (stack); if (bijiao = NULL) |! Match (* bijiao, a [I]) {printf ("% c \ n", a [I]); ret = 0; break ;}} I ++ ;} if (LinkStack_Top (stack) = NULL & a [I] = '\ 0') {printf ("compiled successfully \ n ");} else {printf ("error \ n");} LinkStack_Destroy (stack); return ret;} int main () {char * a = "# include <stdio. h> # include <stdlib. h> int main () {int a [5] [5]; int (* p) [5]; p = a [0]; printf (\ "% d \", & a [3] [3]-& p [3] [3]) ;}"; scan (a); return 0 ;}