0804 OpenJ#1108 Boolean Expressions

来源:互联网 发布:java声明银行账户类锁 编辑:程序博客网 时间:2024/06/09 15:32
摘要:用递归方法解决递归定义的问题
原题目

     Boolean Expressions 
描述
The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next: 
Expression: ( V | V ) & F & ( F | V )

where V is for True, and F is for False. The expressions may include the following operators: ! for not , & for and, | for or , the use of parenthesis for operations grouping is also allowed. 

To perform the evaluation of an expression, it will be considered the priority of the operators, the not having the highest, and the or the lowest. The program must yield V or F , as the result for each expression in the input file. 
输入
The expressions are of a variable length, although will never exceed 100 symbols. Symbols may be separated by any number of spaces or no spaces at all, therefore, the total length of an expression, as a number of characters, is unknown. 

The number of expressions in the input file is variable and will never be greater than 20. Each expression is presented in a new line, as shown below. 
输出
For each test expression, print "Expression " followed by its sequence number, ": ", and the resulting value of the corresponding test expression. Separate the output for consecutive test expressions with a new line. 

Use the same format as that shown in the sample output shown below. 
样例输入
( V | V ) & F & ( F| V)!V | V & V & !F & (F | V ) & (!F | F | !V & V)(F&F|V|!V&!F&!(F|F&V))
样例输出
Expression 1: FExpression 2: VExpression 3: V
来源
México and Central America 2004
来源: http://cxsjsxmooc.openjudge.cn/2017t2summerw3/1/


题目理解:将表达式用递归定义的方式进行计算,以exp()为一个最小的计算项,带括号的也算是一个项。进行递归。在solve里面调用exp()直到表达式结束。

注意:直接处理输入操作不便,所以先进行了数据格式的清理。

日期:2017 8 4

附加

代码

1
2
#include <cstdio>
3
#include <iostream>
4
#include <algorithm>
5
#include <cstring>
6
using namespace std;t
7
char str[500];
8
bool ans;
9
int ptr=0;
10
char get_c(){
11
    return str[ptr++];
12
}
13
14
bool cnt(bool a,char op,bool b){
15
    if(op=='|') return a|b;
16
    if(op=='&') return a&b;
17
}
18
19
bool exp(){
20
    char op=get_c();
21
    bool ans;
22
    switch(op){
23
        case '(':{
24
            bool e;
25
            ans=exp();
26
            op=get_c();
27
            while(op!=')'){
28
                e=exp();
29
                ans=cnt(ans,op,e);
30
                op=get_c();
31
            }
32
            return ans;
33
        }
34
        case '!':{
35
            return !exp();
36
        }
37
        case 'F':return false;
38
        case 'V':return true;
39
        default:{
40
            printf("ERROR");
41
        }
42
    }
43
    
44
}
45
46
bool solve(){
47
    bool b,ans=exp();
48
    char op=get_c();
49
    while(op!='\0'){
50
        switch(op){
51
            case '|':{
52
                ans|=exp();
53
                break;
54
            }
55
            case '&':{
56
                ans&=exp();
57
                break;
58
            }
59
        }
60
        op=get_c();
61
    }
62
    return ans;
63
}
64
65
bool pre(){
66
    char tmp=getchar();
67
    int i=0;
68
    while(tmp!='\n'&&tmp!=EOF){
69
        if(tmp!=' ') str[i++]=tmp;
70
        tmp=getchar();
71
    }
72
    
73
    str[i]='\0';
74
    ptr=0;
75
    if(strlen(str)!=0&&tmp==EOF) return true;
76
    return tmp==EOF?false:true;
77
}
78
79
int main(){
80
    bool ret;int i=1;
81
    while(pre()){
82
        ret=solve();
83
        printf("Expression %d: %c\n",i++,ret?'V':'F');
84
    
85
    }
86
    return 0;
87
}

原创粉丝点击