括弧匹配检验(check)

 1 /*题目:括弧匹配检验
 2 检验给定表达式中括弧是否正确匹配 (两种括弧“( ) ”“[]" ,正确输出OK,错误则输出wrong。 
 3 2016年8月8日07:24:58
 4 作者:冰樱梦 
 5 
 6 */
 7 # include <iostream>
 8 # include <cstring>
 9 # include <cstdio>
10 using namespace std;
11 char s[300];
12 int top=0, i;
13 bool panduan(char s[300])
14 {
15     for(i=0;i<strlen(s);i++)
16     {
17         if(s[i]=='('||s[i]=='[') top++;      //此处有逻辑或 “|| ” 
18         if(s[i]==')'||s[i]==']')             
19         {
20             if(top>0) top--;
21             else return 0;
22         }
23     }
24     if(top != 0) return 0;
25     else return 1;
26 }
27 int main()
28 {
29     scanf("%s",s);
30     if(panduan(s)) cout<<"OK";
31     else cout<<"Wrong";
32     return 0;
33 }

 

posted on 2016-08-08 07:30  冰樱梦  阅读(856)  评论(0编辑  收藏  举报

导航