括号匹配
2016-04-10 12:33
295 查看
#include <iostream> #include <stack> #include <cstring> using namespace std; #define MaxSize 80 stack<char> s; void Judge(char ch[]) { int i; char ch2; if(strlen(ch)%2!=0) {cout<<"false"<<endl;return ;} for(i=0;i<strlen(ch);i++) { switch(ch[i]) { case '[': case '(': case '{':{s.push(ch[i]);break;} case ']': case ')': case '}': { if(!s.empty()){ ch2=s.top(); if((ch2=='('&&ch[i]==')')||(ch2=='['&&ch[i]==']')||(ch2=='{'&&ch[i]=='}')) {s.pop();break;} else cout<<"false"<<endl;return ; } else {cout<<"false"<<endl;return ;} } } } if(s.empty()) {cout<<"true"<<endl;return ;} else {cout<<"false"<<endl;return ;} } int main() { char ch[MaxSize]; while(cin>>ch) { Judge(ch); while(!s.empty()) s.pop(); } return 0; }
相关文章推荐
- Cocos2dx——戏如人生(1)
- JavaScript学习总结(一)
- Windows程序设计基础(1)
- Java NIO-Lock.unlock
- 高等代数中行列式的计算总结
- 利用STL栈将中缀表达式转换成后缀表达式输出
- H5新增表单属性
- Java编程思想——吸血鬼数字问题
- C# 单例模式Lazy<T>实现版本
- iOS 用StackView的第三方FDStackView,有时会出现crash,'NSInternalInconsistencyException', reason: 'Multiplier is
- 【GoogleCodeJam2016C】【java 二进制枚举爆搜】Coin Jam n位01数转变为n进制
- NKOI 2202 字符串乘方
- javascript学习笔记
- 【GoogleCodeJam2016B】【贪心】Revenge of the Pancakes -+改变并翻转 最小操作次数使得字符串变为全+
- [python之数据分析] 基础篇1- Numpy,Scipy,Matplotlib 快速入门攻略
- 利用STL计算后缀表达式
- hadoop yarn配置的一点小问题
- 【GoogleCodeJam2016A】【暴力】Counting Sheep x的倍数从小向大增加直到出现0~9所有数的最小倍增终点
- WHOJ 1609 - Han Move【细心】
- CSS文字两端对齐