C 判断字符串 括号是否匹配,```c
/.按“3.2.2 括号匹配的检验”中的思路,写一个函数“bool check(char* line)”
//检查输入的字符串line中的括号是否匹配,不匹配则check函数返回false,否则返回true
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACKLENGTH 20
char stack[STACKLENGTH];
int top = STACKLENGTH;
//判断栈是否为空
int Empty() {
if (top == STACKLENGTH)
return 1;
return 0;
}
void Push(char ch)
{
top–;
stack[top] = ch;
}
char Pop()
{
char ch = stack[top];
top++;
return ch;
}
/括号匹配的检验/
bool Check(char* line)
{
while (*line != ‘\0’)
{
char chLeft = ‘\0’;
char chRight = ‘\0’;
char ch = *line;
switch (ch)
{
case ‘(’:
case ‘[’: