函数题:括号匹配
要求实现函数,借助如下自定义栈SqStack判断一个中、小括符[、]、(、)组成的字符串中的括弧是否匹配,是则返回true,否则返回false。例如,[[()]]、([[()]])、(()[[]])是匹配的,而(((、()]、 ([(]))是不匹配的。
typedef char ElemType; // 为char取别名ElemType
struct SqStack{
ElemType *base; // 顺序栈的首地址,动态数组的首地址
int top; // 栈顶指针,栈非空时,为栈顶元素的下标(从0开始)
void Init( ); // 初始化栈
void Clear(); // 清空栈
ElemType GetTop(); // 取栈顶元素
void Push(ElemType e); // 入栈
void Pop(); // 出栈
bool Empty(); // 判断空栈
};
### 函数接口定义:
c++
bool matching(string exp);
其中参数 exp是一个仅包含中、小括符[、]、(、)的字符串。
### 裁判测试程序样例:
c++
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
while(cin>>s){
if (matching(s)) cout<<"yes\n";
else cout<<"no\n";
}
return 0;
}
### 输入样例:
in
()[]
[()]
[(()]]
[(])
### 输出样例:
out
yes
yes
no
no
答案:若无答案欢迎评论
typedef char ElemType; // 为char取别名ElemType
struct SqStack{
ElemType *base; // 顺序栈的首地址,动态数组的首地址
int top; // 栈顶指针,栈非空时,为栈顶元素的下标(从0开始)
void Init( ); // 初始化栈
void Clear(); // 清空栈
ElemType GetTop(); // 取栈顶元素
void Push(ElemType e); // 入栈
void Pop(); // 出栈
bool Empty(); // 判断空栈
};
### 函数接口定义:
c++
bool matching(string exp);
其中参数 exp是一个仅包含中、小括符[、]、(、)的字符串。
### 裁判测试程序样例:
c++
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
while(cin>>s){
if (matching(s)) cout<<"yes\n";
else cout<<"no\n";
}
return 0;
}
### 输入样例:
in
()[]
[()]
[(()]]
[(])
### 输出样例:
out
yes
yes
no
no
答案:若无答案欢迎评论