[PTA][C语言][数据结构]判断表达式中括号是否匹配

设算术表达式中有圆括号、方括号、花括号,设计一个算法,判断表达式中的各种括号是否配对。

说明:

  1. 本题仅判断括号是否配对,对于表达式其它问题不做检测。
  2. 括号只考虑配对问题,不考虑括号间的嵌套准则。例如:(a+(b-[c+d])*{e/f}) 中的括号是匹配的;(a+{b-c)} 中的括号不匹配。

函数接口定义:

int IsBracketMatch(char *str);//判断str中括号是否匹配。

其中 str是用户传入的参数,其值为带判断表达式。 若括号匹配则返回1,否则返回0。

栈的定义如下:

#define Stack_Size 50
typedef char ElemType;
typedef struct
{      ElemType  data[Stack_Size]; 
       int  top; 
}SeqStack;

//栈的基本操作函数定义
SeqStack* InitStack();  //栈初始化
int IsEmpty(SeqStack *S); //栈判空
int IsFull(SeqStack *S);  //栈判满
int Push(SeqStack * S, ElemType x);  //  入栈
int Pop(SeqStack * S, ElemType *x);  //  出栈
int GetTop(SeqStack *S, ElemType *x); // 取栈顶元素

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

#define Stack_Size 50
typedef char ElemType;
typedef struct
{      ElemType  data[Stack_Size]; 
       int  top; 
}SeqStack;

//栈的基本操作函数定义
SeqStack* InitStack();  //栈初始化
int IsEmpty(SeqStack *S); //栈判空
int IsFull(SeqStack *S);  //栈判满
int Push(SeqStack * S, ElemType x);  //  入栈
int Pop(SeqStack * S, ElemType *x);  //  出栈
int GetTop(SeqStack *S, ElemType *x); // 取栈顶元素


int IsBracketMatch(char *str);//判断str中括号是否匹配。

main()
{
    char s[20];
    scanf("%s",s);
    if( IsBracketMatch(s))
        printf("Match!\n"); 
    else
        printf("Not Match!\n");
}


SeqStack* InitStack()
{
    SeqStack *s;
    s=(SeqStack *)malloc(sizeof(SeqStack));
    s->top=-1;
    return s;
}
int IsEmpty(SeqStack *S)     
{
      return(S->top==-1?TRUE:FALSE);
}
int IsFull(SeqStack *S)
{
   return(S->top== Stack_Size-1?TRUE:FALSE);
}
int Push(SeqStack * S, ElemType x)
{
     if(S->top== Stack_Size-1)  
         return(FALSE); 
     S->top++;
     S->data[S->top]=x;
     return(TRUE);
}
int Pop(SeqStack * S, ElemType *x)
{     if(S->top==-1)     
             return(FALSE);
      *x= S->data[S->top];
      S->top--;    
      return(TRUE);
}
int GetTop(SeqStack *S, ElemType *x)
{  
      if(S->top==-1)
            return(FALSE);
      *x = S->data[S->top];
      return(TRUE);
}



/* 请在这里填写答案 */

输入样例1:

在这里给出一组输入。例如:

(a+(b-[c*d]-{e/f}))
//结尾无空行

输出样例1:

在这里给出相应的输出。例如:

Match!

输入样例2:

在这里给出一组输入。例如:

(a+[b-(c*d]-{e/f}))
//结尾无空行

输出样例2:

在这里给出相应的输出。例如:

Not Match!

Answer:

int IsBracketMatch(char *str) {
    SeqStack *stack = InitStack();
    ElemType x = 'c';
    for (int i = 0; i < strlen(str); ++i) {
        if ((str[i] == '(' || str[i] == '[' || str[i] == '{') && !IsFull(stack)) {
            Push(stack, str[i]);
        } else if (str[i] == ')' || str[i] == ']' || str[i] == '}') {
            switch (str[i]) {
                case ')':
                    if (GetTop(stack, &x) && x == '(') {
                        Pop(stack, &x);
                    } else {
                        return 0;
                    }
                    break;
                case ']':
                    if (GetTop(stack, &x) && x == '[') {
                        Pop(stack, &x);
                    } else {
                        return 0;
                    }
                    break;
                case '}':
                    if (GetTop(stack, &x) && x == '{') {
                        Pop(stack, &x);
                    } else {
                        return 0;
                    }
                    break;
                default :
                    break;
            }
        }
    }
    if (IsEmpty(stack)) {
        return 1;
    }
    return 0;
}
  • 5
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值