Bracket matching (c language)

⭐ my website: www.mengyingjie.com ⭐

1

Write a program to check whether the string paired brackets, and can not cross appears.
Input:
a string, may contain inside "()" "{}", "[]" brackets three kinds, "#" End
Output:
Success: appears in parentheses represent properly nested pairs and
failed: Unused brackets correctly character.

2 analysis

With a stack, you can solve the problem, the left bracket character must be top of the stack and a stack of right parenthesis character matches.
Introduction stack: the stack is a special linear table, only one end of the linear operating table.
Characteristics stack: last in, first out (LIFO)
Since the data structure is learning, then stack operations are defined and I have written, in order to consolidate the concept

Code 3

#include<stdio.h>
#include <malloc.h>

#define STACK_INT_SIZE 100
#define STACKINCREMENT 10
#define bool int            //自定义bool变量
#define SElemType char

typedef struct {
    SElemType *base;        //栈基地址
    SElemType *top;         //栈顶地址
    int stacksize;
} SqStack;

//------基本操作的算法描述------
//构建一个空栈
bool InitStack(SqStack *S) {
    S->base = (SElemType *) malloc(STACK_INT_SIZE * sizeof(SElemType)); //开辟新的空间
    if (!S->base) return 0;     //开辟失败返回0
    S->top = S->base;
    S->stacksize = STACK_INT_SIZE;
    return 1;
}

//若栈不为空,返回栈顶元素,并返回true 否则返回 false
bool GetTop(SqStack S) {
    if (S.top == S.base) return 0;
    return *(S.top - 1);
}

//插入元素 为新的栈顶元素
bool Push(SqStack *S, SElemType e) {
    if (S->top - S->base >= S->stacksize) //如果栈满  需要增加空间
    {
        S->base = (SElemType *) realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType));
        if (!S->base) return 0;
        S->top = S->base + S->stacksize;
        S->stacksize += STACKINCREMENT;
    }
    *(S->top++) = e;
    return 1;
}

//若栈不为空,则删除栈顶元素,用e返回其值,返回true, 否则返回false
bool Pop(SqStack *S, SElemType *e) {
    if (S->top == S->base) return 0;
    *e = *(--S->top);
    return 1;
}

//检查括号字符在字符集中的位置
int CheckChar(char c, char OP[]) {
    int i;
    for (i = 0; i < 3; i++)
        if (c == OP[i])return i;
    return 999;
}

int main() {
    SqStack OPTR;
    InitStack(&OPTR);
    Push(&OPTR, '#');
    printf("输入括号以“#”结尾\n");
    char c;
    c = getchar();
    int m = 1;          //判断最终是否完全匹配   完全匹配  值为1,否则为0
    char OP1[] = {'[', '(', '{'};       //前置括号字符集
    char OP2[] = {']', ')', '}'};       //后置括号字符集
    while (c != '#') {
        if (CheckChar(c, OP1) < 3) {
            Push(&OPTR, c);
            c = getchar();
        } else {
            if (CheckChar(GetTop(OPTR), OP1) == CheckChar(c, OP2)) {
                //如果需要检验的两个符号在前置和后置括号集中的位置相同则表示配对成功
                //例如[和],[在前置括号集中的位置1,]在后置括号集中的位置1,所以匹配成功
                Pop(&OPTR, &c);
                c = getchar();
                continue;
            } else {
                m = 0;
                break;
            }
        }

    }
    if (GetTop(OPTR) != c)m = 0;
    if (m == 1)printf("\n括号完全匹配!");
    else printf("\n括号匹配失败!");
    return 0;
}

Encountered such problems, but still unresolved read the article,
comment or add QQ: 781378815

Guess you like

Origin www.cnblogs.com/mengyingjie/p/11595513.html