C语言数据结构栈的括号匹配问题

C语言数据结构栈的括号匹配问题
我感觉我的代码没有问题,发现不能输出正确结果,调试发现x变量值不变
#include 
#define Maxsize 10

typedef struct Stack{
    char data[Maxsize];
    int top;
}SqStack;

bool InitStack(SqStack &S){
    S.top = 0;
    return true;
}

//bool DestroyStack(SqStack &S){
//    S.top = 0;
//    return true;
//}
//
//bool Push(SqStack &S,int x){
//    if(S.top == Maxsize){
//        printf("栈满错误\n");
//        return false;
//    }
//    S.data[++S.top] = x;
//    printf("压栈成功\n");
//    return true;
//}

bool Pop(SqStack &S,char &x){
    if(S.top == 0){
        printf("栈空错误\n");
        return false;
    }
    x = S.data[S.top];
    S.top--;
    return true;
}

//bool GetTop(SqStack S){
//    if(S.top == 0){
//        printf("栈空错误\n");
//        return false;
//    }
//    int x = S.data[S.top];
//    printf("栈顶值值:%d\n",x);
//    return true;
//}
//
//bool StackEmpty(SqStack S){
//    if(S.top == 0){
//        printf("栈为空");
//        return true;
//    }
//    printf("栈不为空");
//    return false;
//}

bool BracketCheck(SqStack &S,char str[],int length){
    char x = '(';
    int i;
    if(length > Maxsize){
        printf("数组过长");
        return false;
    }
    for(i = 0;i < length;i++){
        if(str[i] == '('||str[i] == '['||str[i] == '{'){//栈内 
            S.data[S.top] = str[i];
            S.top;
            continue;
        }
        Pop(S,x);
        if(str[i] == ')' && x != '('){
            printf("匹配失败");
            return false;
        }
        if(str[i] == ']' && x != '['){
            printf("匹配失败");
            return false;
        }
        if(str[i] == '}' && x != '{'){
            printf("匹配失败");
            return false;
        }
        if(S.top == 0){
            printf("匹配失败");
            return false;            
        }
        S.top--;
    }
    printf("匹配成功");
    return true;
}

int main(){
    SqStack S;
    char str[10] = {'(','[','{','}',']',')'};
    InitStack(S);
    BracketCheck(S,str,6);
//    Push(S,5);
//    Push(S,4);    
//    Push(S,3);    
//    Push(S,2);
//    Push(S,1);
//    Pop(S);
//    Pop(S);
//    Pop(S);
//    Pop(S);
//    Pop(S);
}

运行结果及详细报错内容

提示匹配失败

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%

感觉代码没什么问题

改动处见注释,供参考:

#include <stdio.h>
#define Maxsize 10
 
typedef struct Stack{
    char data[Maxsize];
    int top;
}SqStack;
 
bool InitStack(SqStack &S){
    S.top = 0;
    return true;
}
 
//bool DestroyStack(SqStack &S){
//    S.top = 0;
//    return true;
//}
//
//bool Push(SqStack &S,int x){
//    if(S.top == Maxsize){
//        printf("栈满错误\n");
//        return false;
//    }
//    S.data[++S.top] = x;
//    printf("压栈成功\n");
//    return true;
//}
 
bool Pop(SqStack &S,char &x){
    if(S.top == 0){
        printf("栈空错误\n");
        return false;
    }
    S.top--;    //修改
    x = S.data[S.top];
    return true;
}
 
//bool GetTop(SqStack S){
//    if(S.top == 0){
//        printf("栈空错误\n");
//        return false;
//    }
//    int x = S.data[S.top];
//    printf("栈顶值值:%d\n",x);
//    return true;
//}
//
//bool StackEmpty(SqStack S){
//    if(S.top == 0){
//        printf("栈为空");
//        return true;
//    }
//    printf("栈不为空");
//    return false;
//}
 
bool BracketCheck(SqStack &S,char str[],int length){
    char x = '(';
    int i;
    if(length > Maxsize){
        printf("数组过长");
        return false;
    }
    for(i = 0;i < length;i++){
        if(str[i] == '('||str[i] == '['||str[i] == '{'){//栈内 
            S.data[S.top] = str[i];
            S.top++;     //修改
            continue;
        }
        Pop(S,x);
        if(str[i] == ')' && x != '('){
            printf("匹配失败");
            return false;
        }
        if(str[i] == ']' && x != '['){
            printf("匹配失败");
            return false;
        }
        if(str[i] == '}' && x != '{'){
            printf("匹配失败");
            return false;
        }
        //if(S.top == 0){          修改
        //    printf("匹配失败");  修改
            //return false;        修改
        //}                        修改
        //S.top--;                 修改
    }
    printf("匹配成功");
    return true;
}
 
int main(){
    SqStack S;
    char str[10] = {'(','[','{','}',']',')'};
    InitStack(S);
    BracketCheck(S,str,6);
//    Push(S,5);
//    Push(S,4);    
//    Push(S,3);    
//    Push(S,2);
//    Push(S,1);
//    Pop(S);
//    Pop(S);
//    Pop(S);
//    Pop(S);
//    Pop(S);
    return 0;
}

修改了2处,top在pop里减了,循环中不用再减。
x = S.data[S.top - 1];里的top已经是++过后的top。
S.top < 0的判断也可以通过Pop的返回值替代。

bool Pop(SqStack &S, char &x)
{
    if (S.top == 0)
    {
        printf("栈空错误\n");
        return false;
    }
    x = S.data[S.top - 1];//
    S.top--;
    return true;
}


bool BracketCheck(SqStack &S, char str[], int length)
{
    char x = 0;
    int i;
    if (length > Maxsize)
    {
        printf("数组过长");
        return false;
    }
    for (i = 0; i < length; i++)
    {
        if (str[i] == '(' || str[i] == '[' || str[i] == '{')
        { // 栈内
            S.data[S.top] = str[i];
            S.top++; //
            continue;
        }
        Pop(S, x);
        if ((str[i] == ')' && x != '(') || (str[i] == ']' && x != '[') || (str[i] == '}' && x != '{') || S.top < 0) //
        {
            cout << x << " " << str[i] << endl; //
            printf("匹配失败");
            return false;
        }
        // S.top--;//
    }
    printf("匹配成功");
    return true;
}