0
点赞
收藏
分享

微信扫一扫

数据结构实验之栈四:括号匹配

杏花疏影1 2022-12-21 阅读 61


数据结构实验之栈四:括号匹配

Time Limit: 1000MS Memory Limit: 65536KB


Submit
Statistic
Problem Description

给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。


Input

输入数据有多组,处理到文件结束。
Output

如果匹配就输出“yes”,不匹配输出“no”
Example Input

sin(20+10){[}]
Example Output

yesno

#include <bits/stdc++.h>
using namespace std;
int Compare(char str[])
{
stack<char> S;
for(int i=0;str[i]!='\0';i++)
{
switch(str[i])
{
case '(':

case '[':

case '{': S.push(str[i]);break;

case ')': if(!S.empty() && S.top() == '(') S.pop(); else return 0;break;

case ']': if(!S.empty() && S.top() == '[') S.pop(); else return 0;break;

case '}': if(!S.empty() && S.top() == '{') S.pop(); else return 0;break;

}
}
if(S.empty())
return 1;
return 0;

}
int main()
{
char str[66];
while(gets(str))
{
Compare(str) ? cout << "yes" << endl:cout << "no" << endl;

}
return 0;
}


举报

相关推荐

0 条评论