進步主義 2023-01-03 19:46 采纳率: 33.3%
浏览 49

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

C语言数据结构栈的括号匹配问题
我感觉我的代码没有问题,发现不能输出正确结果,调试发现x变量值不变
#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;
    }
    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%

感觉代码没什么问题

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2023-01-03 20:54
    关注

    改动处见注释,供参考:

    #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;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 1月3日

悬赏问题

  • ¥15 网络分析设施点无法识别
  • ¥15 状态图的并发态问题咨询
  • ¥15 PFC3D,plot
  • ¥15 VAE模型编程报错无法解决
  • ¥100 基于SVM的信息粒化时序回归预测,有偿求解!
  • ¥15 物体组批优化问题-数学建模求解答
  • ¥15 微信原生小程序tabBar编译报错
  • ¥350 麦克风声源定位坐标不准
  • ¥15 apifox与swagger使用
  • ¥15 egg异步请求返回404的问题