首页 > 其他 > 详细

栈-括号的匹配

时间:2020-11-07 19:58:47      阅读:30      评论:0      收藏:0      [点我收藏+]

// 判断输入的括号格式是否正确(只限于小括号和中括号)

【输入】

  [[([()])]]

【输出】

  YES


#include<iostream>
#include<cstring>
using namespace std;
char b[101];
int top=0;

void push(char x){
  if(top<101){
    top++;
    b[top]=x;
    return;
  }
  cout<<"进栈失败"<<endl;

}


void pop(){
  if(top>0){
    top--;
    return;
  }
  cout<<"出栈失败"<<endl;
}


int main(){
  char a[101];
  cin.getline(a,101);
  for(int i=0;i<strlen(a);i++){
    if(a[i]==‘(‘||a[i]==‘[‘) push(a[i]);
    else if(b[top]==‘(‘&&a[i]==‘)‘) pop();
    else if(b[top]==‘[‘&&a[i]==‘]‘) pop();
    else cout<<"NO"<<endl;
  }
  if(top==0)cout<<"YES"<<endl;
  return 0;
}

栈-括号的匹配

原文:https://www.cnblogs.com/dks0313/p/13941791.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!