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

ECNU-2574 Principles of Compiler

时间:2014-09-11 23:47:02      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   art   div   

题意:

给出编译规则,求是否满足条件

    A:= ‘(‘ B‘)‘|‘x‘.
    B:=AC.
    C:={‘+‘A}.

其中{}表示里面的内容可以出现0次或者多次

注意点见代码注释

bubuko.com,布布扣
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 const int maxn = 205;
 5 
 6 int pos;
 7 int len;
 8 char a[ maxn ];
 9 
10 bool solveA();
11 bool solveB();
12 bool solveC();
13 
14 bool solveA(){
15     //printf("A:%c\n",a[pos]);
16     //if( pos>=len ) return false;
17     if( a[pos]==( ){
18         pos++;
19         if( solveB() ){
20             if( a[pos]==) ){
21                 pos++;/*该字符后面可能还有字符*/
22                 return true;
23             }
24             else{
25                 return false;
26             }
27         }
28         else 
29             return false;
30     }
31     else
32         if( a[pos]==x ){
33             pos++;
34             return true;
35         }
36     else{
37         pos++;/*同理,该字符后面可能还有字符*/
38         return false;
39     }
40 }
41 
42 bool solveB(){
43     //printf("B:%c\n",a[pos]);
44     if( solveA() ){
45         return solveC();
46     }
47     else return false;
48 }
49 
50 
51 bool solveC(){
52     if( pos>=len ) return false;
53     while( pos<len && a[pos]==+ ){
54         pos++;
55         solveA();
56     }
57     return true;
58 }
59 
60 
61 int main(){
62     int T;
63     scanf("%d",&T);
64     while( T-- ){
65         memset( a,\0,sizeof( a ) );
66         scanf("%s",a);
67         pos = 0;
68         len = strlen( a );
69         bool f = solveA();
70         if( pos<len ) {printf("Bad\n");continue;}
71         /*防止出现前半部分符合条件,后半部分不合的用例*/
72         if( f ) printf("Good\n");
73         else printf("Bad\n");
74     }
75     return 0;
76 }
View Code

 

ECNU-2574 Principles of Compiler

标签:style   blog   http   color   io   os   ar   art   div   

原文地址:http://www.cnblogs.com/justforgl/p/3967442.html

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