#include <iostream>
#include <stack> // stack
#include <cstdlib>
#include <string.h> //strlen
using namespace std;
bool isRes(char * str){
stack<char> str_s;
int str_len = strlen(str);
for (int y=0;y<str_len;y++){
char x = str[y];
if (x!=']' && x!='[' && x!='(' && x!=')' )
continue;
if (x=='[' || x=='('){
str_s.push(x);
}else{
if(str_s.empty()){
cout <<"右右右多duo"<<endl;
return false;
}
char z = str_s.top();
if ((z=='[' && x==']') || (z=='(' && x==')'))
str_s.pop();
else
return false;
}
}
if (!str_s.empty()){
cout<<"zuo左左duo"<<endl;
return false;
}else{
return true;
}
}
int main() {
char r[10];
cin>> r;
if (isRes(r)){
cout<<"yes"<<endl;
}else
{
cout<<"no"<<endl;
}
return 0;
}
C++ 栈stack之括号匹配
最新推荐文章于 2024-07-26 15:21:24 发布
本文介绍了一种使用C++中的堆栈数据结构来检查字符串中括号是否正确匹配的方法。通过遍历字符串,利用堆栈跟踪左括号,确保每个左括号都有相应的右括号与之对应,从而验证括号的平衡性。
324

被折叠的 条评论
为什么被折叠?



