当前位置: 首页 > article >正文

每日一练算法题(判断表达式中括号是否匹配)

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

说明:

(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!
int IsBracketMatch(char *str){
    SeqStack *stack=InitStack();
    int len=strlen(str);
    int i;
    for(i=0;i<len;i++){
        if(str[i]=='('||str[i]=='['||str[i]=='{'){
            Push(stack,str[i]);
        }else if(str[i]==')'||str[i]==']'||str[i]=='}'){
            char top;
            if(IsEmpty(stack)){
                return FALSE;
            }
            Pop(stack,&top);
            if((str[i]==')'&&top!='(')||(str[i]==']'&&top!='[')||(str[i]=='}'&&top!='{')){
                return FALSE;
            }
        }
    }
    return IsEmpty(stack)?TRUE:FALSE;
}

http://www.kler.cn/news/336417.html

相关文章:

  • QT学习笔记4.6(编译,运行,调试)
  • 【D3.js in Action 3 精译_027】3.4 让 D3 数据适应屏幕(下)—— D3 分段比例尺的用法
  • 852. 山脉数组的峰顶索引
  • 计算机网络自顶向下(2)----socket编程
  • 分享国产32位单片机的电机控制方案
  • GEE 土地分类:利用Landsat C02 TOA数据进行土地分类精度超95%(希腊雅典为例)并监测不同年份的绿地面积
  • Android高级控件
  • (2025)408考研:王道操作系统文件管理强化
  • Sublime Text 下载地址分享
  • 看480p、720p、1080p、2k、4k、视频一般需要多大带宽呢?
  • 信息学奥赛使用的编程IDE:Dev-C++ 安装指南
  • Linux之进程概念
  • 毕业设计 深度学习昆虫识别系统(源码+论文)
  • Kafka的基本概念整理
  • UE5蓝图实战:打造自定义摄像机视野控制
  • 排序算法总结(含链表)
  • 民峰:为投资者提供稳健的财富管理方案
  • 鸿蒙开发(NEXT/API 12)【二次向用户申请授权】程序访问控制
  • 秋招内推2025-招联金融
  • 基于opencv-C++dnn模块推理的yolov5 onnx模型