C language/c++ (data structure) bracket matching test example (stack and queue) (4/7)

Experimental purpose and requirements:

Familiar with using the stack to complete the check of whether the brackets match.

Experiment content:

Use the stack to complete the parenthesis matching check.

Purpose: To master the application of the last-in-first-out principle of the stack in solving practical problems.

Content: Input a set of brackets, construct a stack, use the stack to judge whether the input brackets match, and output three different results: match, multiple left brackets, and multiple right brackets.

For any number of non-negative decimal numbers entered, print out the octal number equivalent to its value.

The specific implementation process:

1. The sequence stack is expressed as an array, which is used to store the parentheses to be input;

2. Distinguish between different situations through different values ​​of the bottom pointer of the stack;

3. Connect the function.

4. Basic requirements: check the occurrence of parentheses; in-depth analysis: check the occurrence of parentheses, square brackets and curly braces at the same time.

Experimental steps and procedures:

#include<stdio.h>

#include<stdlib.h>

typedef struct

{

      int *base;

      int *top;

      int size;

}sqstack;

sqstack initstack()

{

      sqstack s;

      s.base=(int *)malloc(100*sizeof(int));

      s.top=s.base;

      return s;

}

sqstack push(sqstack s,int e)

{

      *(s.top)=e;

      s.top++;

      return s;

}

int gettop(sqstack s)

{

      if(s.base==s.top)

      {

            return 0;

      }

      return *(s.top-1);

}

sqstack pop(sqstack s)

{

      s.top--;

      return s;

}

void print(sqstack s)

{

      while(gettop(s)!=0)

      {

            printf("%d\n",gettop(s));

            s=pop(s);

      }

}

int main()

{

      sqstack s;

      char c=' ';

      s=initstack();

      printf("请输入括号,以#结束\n");

      while(c!='#')

      {

            scanf("%c",&c);

            switch (c)

            {

                  case '(':s=push(s,1);break;

                  case '[':s=push(s,2);break;

                  case '{':s=push(s,3);break;

                  case ')':

                       {

                             if(gettop(s)==0)

                             {

                                   printf("多了*)*\n");

                                   return 0;

                             }

                             else if(gettop(s)==1)

                             {

                                   s=pop(s);

                             }

                             else

                             {

                                   printf("括号不匹配!\n");

                                   return 0;

                             }

                            

                       };break;

                  case ']':

                       {

                             if(gettop(s)==0)

                             {

                                   printf("多了*]*\n");

                                   return 0;

                             }

                             else if(gettop(s)==2)

                             {

                                   s=pop(s);

                             }

                             else

                             {

                                   printf("括号不匹配!\n");

                                   return 0;

                             }

                            

                       };break;

                  case '}':

                       {

                             if(gettop(s)==0)

                             {

                                   printf("多了*}*\n");

                                   return 0;

                             }

                             else if(gettop(s)==3)

                             {

                                   s=pop(s);

                             }

                             else

                             {

                                   printf("括号不匹配!\n");

                                   return 0;

                             }

                            

                       };break;

                  default:break;

            }

      }

      if(s.top==s.base)

      {

            printf("括号匹配\n");

      }

      else

      {

            printf("多了左括号\n");

      }

      return 0;

 }

运行结果:

结果分析与讨论:

1. 我们可以利利用栈的特点即后进先出的特点来实现括号匹配的检验,存储括号字符的数组通过malloc实现动态分配长度,每读入一个括号,若是左括号,则直接进栈,等待相匹配的同类右括号;若读入的是右括号,且与当前栈顶左括号同类型,则二者匹配,将栈顶的左括号弹出,否则属于不合法情况。另外。如果输入序列已经读完,而栈中仍有等待匹配的左括号,或者读入一个右括号,而栈中已无等待匹配的同类型左括号,均属于不匹配的情况。

Guess you like

Origin blog.csdn.net/qq_59819866/article/details/131451069