攒一袋星星24 2022-03-08 10:17 采纳率: 50%
浏览 36
已结题

括号匹配,匹配输出1,不匹配输出0,代码一直输出0

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef char datatype;
typedef struct{
int top;
datatype a[MAXSIZE];
}sequence_stack ;
void init(sequence_stack *st){
st->top=0;
}
void push(sequence_stack *st,datatype x){
if(st->top==MAXSIZE){
printf("\nThe sequence stack is full!\n");exit(1);
return 1;
}
st->a[st->top]=x;
st->top++;
return 0;
}
void pop(sequence_stack *st){
if(st->top==0){
printf("This sequence stack is empty!\n");exit(1);
return 1;
}
st->top--;
return 0;
}
int empty(sequence_stack st){
return(st.top? 0:1);//1表示空,0表示非空
}
datatype read(sequence_stack st){
if(empty(st)){
printf("\n栈是空的!");exit(1);
}
return st.a[st.top-1];
}
void input(sequence_stack *st){
init(st);
datatype x;
printf("请输入一组字符串,并以#为结束符\n");
scanf("%c",&x);
while(!'#'){
st->a[st->top++]=x;
scanf("%c",&x);
}
}

int match_kuohao(char c[]){
int i=0;
sequence_stack st;
init(&st);
while(c[i]!='#'){
switch(c[i]){
case'{':
case'[':
case'(':push(&st,c[i]);break;
case'}':if(!empty(st)&&read(st)=='{'){
pop(&st);break;}
else return 0;
case']':if(!empty(st)&&read(st)=='['){
pop(&st);break;}
else return 0;
case')':if(!empty(st)&&read(st)=='('){
pop(&st);break;}
else return 0;
}
i++;
}
return(empty(st));
}
int main(int argc, char *argv[]) {
sequence_stack st;
datatype x;
init(&st);
input(&st);
int m= 7;
m=match_kuohao(&st);//空为1,1表示匹配,0表示非空,0为不匹配
printf("%d",m);
return 0;
}

  • 写回答

2条回答 默认 最新

  • 关注

    push函数返回值是void类型,return 1改成return,其它函数类似。
    input函数中不需要再调用init();input函数入队就可以了,不需要读取字符串,读取字符串放在main函数中。
    main函数中match_kuohao的输入参数也是错误的。
    代码修改如下:

    #include <stdio.h>
    #include <stdlib.h>
    #define MAXSIZE 100
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    typedef char datatype;
    typedef struct{
        int top;
        datatype a[MAXSIZE];
    }sequence_stack ;
    void init(sequence_stack *st){
        st->top=0;
    }
    void push(sequence_stack *st,datatype x)
    {
        if(st->top==MAXSIZE){
            printf("\nThe sequence stack is full!\n");exit(1);
            return ;
        }
        st->a[st->top]=x;
        st->top++;
        return ;
    }
    void pop(sequence_stack *st)
    {
        if(st->top==0){
            printf("This sequence stack is empty!\n");exit(1);
            return ;
        }
        st->top--;
        return ;
    }
    int empty(sequence_stack st){
        return(st.top? 0:1);//1表示空,0表示非空
    }
    datatype read(sequence_stack st){
        if(empty(st)){
            printf("\n栈是空的!");exit(1);
            return 0x0;
        }
        return st.a[st.top-1];
    }
    void input(char st[]){
        //init(st);
        datatype x;
        int i=0;
        printf("请输入一组字符串,并以#为结束符\n");
        scanf("%c",&x);
        while(x!='#'){
            st[i] = x;
            i++;
            //st->a[st->top++]=x;
            scanf("%c",&x);
        }
        st[i] =0;
    }
    
    int match_kuohao(char c[])
    {
        int i=0;
        sequence_stack st;
        init(&st);
        while(c[i])
        {
            switch(c[i])
            {
            case'{':
            case'[':
            case'(':
                push(&st,c[i]);
                break;
            case'}':
                if(!empty(st)&&read(st)=='{')
                {
                    pop(&st);
                    break;
                }
                else return 0;
            case']':
                if(!empty(st)&&read(st)=='['){
                    pop(&st);break;}
                else return 0;
            case')':
                if(!empty(st)&&read(st)=='('){
                    pop(&st);break;}
                else return 0;
            }
            i++;
        }
        return(empty(st));
    }
    int main(int argc, char *argv[]) {
        //sequence_stack st;
        //datatype x;
        char buf[1000];
        //init(&st);
        input(buf);    //input(&st);
        int m= 7;
        m=match_kuohao(/*&st*/ buf);//空为11表示匹配,0表示非空,0为不匹配
        printf("%d",m);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月18日
  • 已采纳回答 3月10日
  • 创建了问题 3月8日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改