栈实现括号匹配

来源:互联网 发布:网络文明传播志愿评论 编辑:程序博客网 时间:2024/05/17 21:44
//[]()(())([<(]))#include<iostream>#include<string>#include<stack>using namespace std;int march(char str[]){stack<char> kh;char ch,temp;int i,len;len=strlen(str);kh.empty();for(i=0;i<len;i++){ch=str[i];switch(ch){case '(':kh.push(ch);break;case')':if(kh.empty()||kh.top()!='('){cout<<"Not Match!"<<endl;break;}else{temp=kh.top();kh.pop();break;}case '[':kh.push(ch);break;case']':if(kh.empty()||kh.top()!='['){cout<<"Not Match!"<<endl;break;}else{temp=kh.top();kh.pop();break;}case '{':kh.push(ch);break;case'}':if(kh.empty()||kh.top()!='{'){cout<<"Not Match!"<<endl;break;}else{temp=kh.top();kh.pop();break;}}}if(kh.empty()){cout<<"Match!"<<endl;}else{cout<<"Not Match!"<<endl;}return 0;}int main(){char temp[20];cin>>temp;march(temp); system("pause");return 0;}



0 0
原创粉丝点击