利用栈实现的()。。匹配c++程序

yeah!加油!

 

代码
1 #include <iostream>
2 #include <stack>
3  using namespace std;
4  int main()
5 {
6 stack<char> a;
7 char input;
8 cout<<"Please input your chars:\n";
9 while(cin>>input&&(input=='('||')'||'['||']'))
10 {
11 switch(input){
12 case '(':
13 a.push(input);
14 break;
15 case '[':
16 a.push(input);
17 break;
18 case ')':
19 if(a.top()=='(')
20 a.pop();
21 break;
22 case ']':
23 if(a.top()=='[')
24 a.pop();
25 break;
26 default:
27 break;
28 }}
29 if(a.empty())
30 cout<<"Matching success!";
31 else
32 cout<<"Matching fail.";
33
34 return 0;
35 }

 

posted on 2010-10-20 21:13  decem  阅读(146)  评论(0)    收藏  举报

导航