括号匹配性检测C语言实现

#include <stdio.h>

#define SIMPLE_KUOHAO "(()1231qeqw)(@#$)"

#define COMPLEX_KUOHAO "{(()[asd])}{{{{(((())))}}}}"

int main(int argc, const char * argv[])

{

    /*问题描述:

     假设一个算术表达式中可以包含三种括号:圆括号"("

     ")",方括号"[""]"和花括号"{""}",且这三种括号可按任意的

     次序嵌套使用(如:…[…{…}…[…]…]…[…]…(…)…)。编写判别给定表达

     式中所含括号是否正确配对出现的算法(已知表达式已存入数据元素

     为字符的顺序表中) 

     

     思路分析:

     检验括号是否匹配的方法可以用期待的急迫程度这个概念来描述。

     

       例如,考虑下列括号序列:

     

       [ ( [ ] [ ] ) ]

     

       1 2 3 4 5 6 7 8

     

       当计算机接受了第一个括号后,它期待着与其匹配的第八个括号的出现,然而等来的却是第二个括号,此时第一个括号“[”只能暂时靠边,而迫切等待与第二个括号相匹配的、第七个括号“)”的出现,类似地,因等来的是第三个括号“[”,其期待匹配的程度较第二个括号更急迫,则第二个括号也只能靠边,让位于第三个括号,显然第二个括号的期待急迫性高于第一个括号;在接受了第四个括号之后,第三个括号的期待得到满足,消解之后,第二个括号的期待匹配就成为当前最急迫的任务了,……依此类推。

     

       很显然,这样的一个处理过程和栈的特点非常吻合,因此,这个问题可以用栈来解决。

     

       解决思路:

     

       1.在算法中设置一个栈,每次读入一个括号;

     

       2.若是右括号,则或者使置于栈顶的最急迫的期待得以消解,此时将栈顶的左括号弹出;或者是不合法的情况,此时将右括号压入;

     

       3.若是左括号,则作为一个新的更急迫的期待压入栈中,自然使原有的在栈中的所有未消解的期待的急迫性都降低一级;

     

       4.在算法的开始和结束时,栈应该为空。*/

    

    //这里用的是C来做的,用结构体模拟了堆栈,当最后匹配完成后括号数组不为空(若用堆栈则匹配完成后堆栈应该为空),只是top下标为0

    //注意类似这种的 {[}] 也会匹配失败   实际中也失败

    //简单地括号匹配性检测(只有一种括号)

    int jianceSimpleKuohao(char *str);//声明检测函数   返回1匹配成功  返回0匹配失败

    char str[] = SIMPLE_KUOHAO;//要检测的字符串

    int i = jianceSimpleKuohao(str);

    if (i == 1) {

        printf("匹配成功 ");

    }

    else

    {

        printf("匹配失败 ");

    }

    //简单地括号匹配性检测(有多种括号出现)

    int jianceComplexKuohao(char *str);

    char str2[] = COMPLEX_KUOHAO;

    int j = jianceComplexKuohao(str2);

    if (j == 1) {

        printf("匹配成功 ");

    }

    else

    {

        printf("匹配失败 ");

    }

    return 0;

}

int jianceSimpleKuohao(char *str)

{

    struct Stack {

        char kuohao[100];//用于存放左括号

        unsignedint top;//指示char kuohao[1]的下标

    }stack;

    

    stack.top = 0;

    while (*str != '')//循环遍历字符串数组

    {

        if (*str == '(')

        {

            stack.kuohao[stack.top] = *str;

            stack.top++;

            //将出现的左括号 放入stack.kuohao[stack.top],并且下标+1   用堆栈来说就是每新出现一个左括号就将其压入栈 堆栈的top指针+1 

        }

        else if (*str == ')')

        {

            if (stack.top > 0 && stack.kuohao[stack.top-1] == '(')

            //stack.top > 0 防止右括号出现再第一位

            //stack.kuohao[stack.top-1] == '('防止右括号出现在左括号的前边

            {

                stack.top--;

                //如出现一个右括号stack.kuohao[]数组的下标-1 其实就是将离新出现的右括号最近的左括号退出栈

            }

            else

            {

                return 0;

            }

        }

        str++;

    }

    printf("%d",stack.top);

    return 1;

}

int jianceComplexKuohao(char *str)

{

    struct SQLIST{

        char elem[100];

        unsigned int top;

    } SqList;  // 顺序表

    SqList.top = 0;

    while (*str != '') {

        if (*str == '(' || *str == '[' || *str == '{') {

            SqList.elem[SqList.top] = *str;

            SqList.top++;

        }

        else if (*str == ')' || *str == ']' || *str == '}')

        {

            if (*str == ')') {

                if (SqList.top > 0 && (SqList.elem[SqList.top - 1] == '(')) {

                    SqList.top--;

                }

                else

                {

                    return 0;

                }

            }

            if (*str == ']') {

                if (SqList.top > 0 && (SqList.elem[SqList.top - 1] == '[')) {

                    SqList.top--;

                }

                else

                {

                    return 0;

                }

            }

            if (*str == '}') {

                if (SqList.top > 0 && (SqList.elem[SqList.top - 1] == '{')) {

                    SqList.top--;

                }

                else

                {

                    return 0;

                }

            }

        }

        str++;

    }

    printf("%d",SqList.top);

    return 1;

}

原文地址:https://www.cnblogs.com/chenhaosuibi/p/3440338.html