码迷,mamicode.com
首页 > 其他好文 > 详细

括号匹配的检验

时间:2015-06-04 22:26:05      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
//判断括号是否能匹配,如果最后栈为空,则括号匹配,否则括号不匹配;
int main(){
	char ch;
	stack<char> s;
	int n;
	cin>>n;
	while(n--){
		cin>>ch;
		if(s.empty()||ch==‘[‘||ch==‘(‘){
		s.push(ch);
		}
		if(ch==‘]‘){
			if(s.top()==‘[‘)
				s.pop();
			else
				s.push(ch);
		}if(ch==‘)‘){
			if(s.top()==‘(‘)
				s.pop();
			else
				s.push(ch);
		}
	}
	if(s.empty()) cout<<"yes"<<endl;
	else cout<<"no"<<endl;
	system("pause");
	return 0;
}

  

括号匹配的检验

标签:

原文地址:http://www.cnblogs.com/wintersong/p/4553050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!