#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
int main(){
<span style="white-space:pre"> </span>int n=0;
<span style="white-space:pre"> </span>cin >> n;
<span style="white-space:pre"> </span>getchar();
<span style="white-space:pre"> </span>char str[256];
<span style="white-space:pre"> </span>while (n--){
<span style="white-space:pre"> </span>gets_s(str);
<span style="white-space:pre"> </span>int len = strlen(str);
<span style="white-space:pre"> </span>stack <char> s;
<span style="white-space:pre"> </span>char ch;
<span style="white-space:pre"> </span>bool ok = true;
<span style="white-space:pre"> </span>for (int i = 0; i < len;i++){
<span style="white-space:pre"> </span>ch = str[i];
<span style="white-space:pre"> </span>switch (ch)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>case'(':
<span style="white-space:pre"> </span>case'[':<span style="white-space:pre"> </span>s.push(ch); break;
<span style="white-space:pre"> </span>case')':
<span style="white-space:pre"> </span>case']':<span style="white-space:pre"> </span>if (s.empty()) ok = false;
<span style="white-space:pre"> </span>else {
<span style="white-space:pre"> </span>if (s.top() == '('&&ch != ')' || s.top() == '['&&ch != ']')
<span style="white-space:pre"> </span>ok = false;
<span style="white-space:pre"> </span>s.pop();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (s.empty()&&ok)<span style="white-space:pre"> </span>cout << "Yes" << endl;
<span style="white-space:pre"> </span>else cout << "No" << endl;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return 0;
}uva673 表达式stack
最新推荐文章于 2020-03-05 19:45:26 发布
本文介绍了一个使用 C++ 实现的括号匹配验证程序。该程序能够读取包含括号的字符串,并利用栈数据结构来检查这些括号是否正确配对。通过对每一对括号进行匹配检查,程序可以判断输入字符串中的括号是否符合语法规则。
1809

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



