PTA 6-3 括号匹配

下面程序可实现表达式中括号匹配检查,约定只有‘()’、'[]'、'{}'三种括号。请将以下程序补充完整。

函数接口定义:

在这里描述函数接口。例如:
Status push(Sqstack &S,SElemType x) //x入栈S
{
}
Status pop(Sqstack &S,SElemType &e)//从S栈出栈1次元素放入e
{
}
Status Compare(char s[]) //s为表达式
{
   Sqstack S;
   SElemType e;
   Status flag=TRUE;
   int i=0;
   iniStack(S);
   while(s[i]!='#' && flag==TRUE )
   {
       switch(s[i])
       {
         case '(':
         case '[':
         case '{':push(S,s[i]);break;
         case ')': if(pop(S,e)==ERROR || e!='(')//如果是(
                     flag=FALSE;break;
         case ']': if(_________________)//如果是[
                     flag=FALSE;break;
         case '}': if(_________________)//如果是{
                     flag=FALSE;break;
       }
       i++;
   }
   if(flag==TRUE && s[i]=='#' && S.top==S.base)
     return TRUE;
   else
     return FALSE;
}

裁判测试程序样例:

#include <stdio.h>
#include <malloc.h>
typedef int Status;
typedef char SElemType;
#define stack_INIT_SIZE     100
#define stackINCREMENT  10
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
 typedef struct {
       SElemType  *base;   //栈底指针
       SElemType  *top;     //栈顶指针
       int stacksize;       //栈空间
}Sqstack;

Status iniStack(Sqstack &S) //初始化栈
{
 //构造一个空栈S
    S.base=(SElemType*)malloc(stack_INIT_SIZE * sizeof(SElemType));
    if(!S.base)return(ERROR);
    S.top=S.base;
    S.stacksize=stack_INIT_SIZE;
    return OK;
  }//InitStack

/* 你的代码将被嵌入到这里 */

int main()
{
    char s[100];
    Status st;
  scanf("%s",s);
   st=Compare(s);
    if(st==TRUE)
        printf("检查结果:括号匹配!");
    else
        printf("检查结果:括号不匹配!");
    return 0;
}

裁判测试程序样例:

#include <stdio.h>
#include <malloc.h>
typedef int Status;
typedef char SElemType;
#define stack_INIT_SIZE     100
#define stackINCREMENT  10
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
 typedef struct {
       SElemType  *base;   //栈底指针
       SElemType  *top;     //栈顶指针
       int stacksize;       //栈空间
}Sqstack;

Status iniStack(Sqstack &S) //初始化栈
{
 //构造一个空栈S
    S.base=(SElemType*)malloc(stack_INIT_SIZE * sizeof(SElemType));
    if(!S.base)return(ERROR);
    S.top=S.base;
    S.stacksize=stack_INIT_SIZE;
    return OK;
  }//InitStack

/* 你的代码将被嵌入到这里 */

int main()
{
    char s[100];
    Status st;
  scanf("%s",s);
   st=Compare(s);
    if(st==TRUE)
        printf("检查结果:括号匹配!");
    else
        printf("检查结果:括号不匹配!");
    return 0;
}

输入样例:

{[4+2*(2+3)]-1}#

输出样例:

检查结果:括号匹配!

答案:

Status push(Sqstack &S,SElemType x) //x入栈S
{
    if(S.top-S.base==S.stacksize)
    {
        int num=S.top-S.base;
        S.base=(SElemType*)realloc((void*)(S.base),sizeof(SElemType)*(stackINCREMENT+stack_INIT_SIZE));
        S.top=S.base+num;
        S.stacksize=stackINCREMENT+stack_INIT_SIZE;
    }
    *(S.top)=x;
    S.top++;
    return 0;
}
Status pop(Sqstack &S,SElemType &e)//从S栈出栈1次元素放入e
{
    if(S.top==S.base)
    {
    return ERROR;
    }
    else
    {
        S.top--;
        e=*(S.top);
    }
    return TRUE;
}
Status Compare(char s[]) //s为表达式
{
   Sqstack S;
   SElemType e;
   Status flag=TRUE;
   int i=0;
   iniStack(S);
   while(s[i]!='#' && flag==TRUE )
   {
       switch(s[i])
       {
         case '(':
         case '[':
         case '{':
               push(S,s[i]);
               break;
         case ')':
               if(pop(S,e)==ERROR || e!='(')
                     flag=FALSE;
               break;
         case ']':
               if(pop(S,e)==ERROR || e!='[')
                     flag=FALSE;
               break;
         case '}': 
               if(pop(S,e)==ERROR || e!='{')
                     flag=FALSE;
               break;
       }
       i++;
   }
   if(flag==TRUE && s[i]=='#' && S.top==S.base)
     return TRUE;
   else
     return FALSE;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值