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

mac2022-06-30  17

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

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; }

转载于:https://www.cnblogs.com/CCCrunner/p/6444594.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)