Xyyyq 2020-05-08 21:33 采纳率: 0%
浏览 320
已采纳

括号匹配,,实在找不出问题了,求助大佬!!

#include
#include

#define MAXSIZE 1001
typedef struct{
char *base;
char *top;
int stacksize;
}SqStack;
char InitStack(SqStack S)
{
S.base=(char *)malloc(MAXSIZE*sizeof(char));
S.stacksize=MAXSIZE;
S.top=S.base;
return 1;
}
char StackEmpty(SqStack S)
{
InitStack(S);
if(S.top-S.base==0)return 0;
else return 1;
}

char GetTop(SqStack S)
{
InitStack(S);
if(S.top-S.base==0)return 0;
return *--S.top;

}
char Push(SqStack *S,char e)
{
InitStack(*S);
*S->top++=e;

}
char Pop(SqStack *S)
{
InitStack(*S);
if(S->top-S->base==0)return 0;
--S->top;

}

int main()
{SqStack S;
InitStack(S);
int flag=1;
char s[MAXSIZE];
gets(s);

int i=0;
int a=0,b=0;
while(s[i]!='#'&&flag)
{

switch(s[i])
{
case '(':
case '[':
a=a+1,Push(&S,s[i]);
break;
case ')':
b=b+1;
if(StackEmpty(S)&&GetTop(S)=='(')Pop(&S);
else flag=0;
break;
case ']':b=b+1;
if(StackEmpty(S)&&GetTop(S)=='[')Pop(&S);
else flag=0;
break;
} i++;
}
printf("%d %d\n",a,b);
if(!StackEmpty(S)&&flag)printf("YES\n");
else printf("NO\n");
return 0;
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-08 23:00
    关注

    如果问题得到解决,请点下采纳

    #include <stdio.h>
    #include <stdlib.h>
    #define MAXSIZE 1001
    typedef struct{
        char *base;
        char *top;
        int stacksize;
    }SqStack;
    char InitStack(SqStack *S)
    {
        S->base=(char *)malloc(MAXSIZE*sizeof(char));
        S->stacksize=MAXSIZE;
        S->top=S->base;
        return 1;
    }
    int StackEmpty(SqStack S)
    {
        //InitStack(S);
        if(S.top-S.base==0)return 0;
        else return 1;
    }
    char GetTop(SqStack S)
    {
        //InitStack(S);
        if(S.top-S.base==0)return 0;
        return *(--S.top);
    }
    void Push(SqStack *S,char e)
    {
        //InitStack(*S);
        *S->top++=e;
    }
    char Pop(SqStack *S)
    {
        //InitStack(*S);
        if(S->top-S->base==0) return 0;
        --S->top; return 1;
    }
    int main()
    {
        SqStack S;
        InitStack(&S);
        int flag=1;
        char s[MAXSIZE];
        gets(s);
        int i=0;
        int a=0,b=0;
        while(s[i]!='#'&&flag)
        {
            switch(s[i])
            {
            case '(':
            case '[':
                a=a+1,Push(&S,s[i]);
                break;
            case ')':
                b=b+1;
                if(StackEmpty(S)&&GetTop(S)=='(')Pop(&S);
                else flag=0;
                break;
            case ']':b=b+1;
                if(StackEmpty(S)&&GetTop(S)=='[')Pop(&S);
                else flag=0;
                break;
            } 
            i++;
        }
        printf("%d %d\n",a,b);
        if(!StackEmpty(S)&&flag)printf("YES\n");
        else printf("NO\n");
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换