Boolean Expressions POJ 2106 【递归】

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. 

Input

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. 

Output

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. 

Sample Input

( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))

Sample Output

Expression 1: F
Expression 2: V
Expression 3: V

#include <bits/stdc++.h>
using namespace std;
//typedef long long ll;
using ll = long long;
const ll inf = 4e18+10;
const int mod = 1000000007;
const int mx = 200010; //check the limits, dummy
typedef pair<int, int> pa;
const double PI = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
#define swa(a,b) a^=b^=a^=b
#define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--)
#define clr(a) memset(a, 0, sizeof(a))
#define lowbit(x) ((x)&(x-1))
#define mkp make_pair
void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
ll  m, n,y,z,k,sum=0,ans=0,t,num;
int vis[200][2], mp[10][10];
bool orex ();
bool andex();
bool notex();
char pick();
char get();
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int I = 1;
    while (pick()!=-1)
    {
        bool B = orex();
        char c;
        if (B)c = 'V';
        else c = 'F';
        cout << "Expression " << I << ':' << c << endl;
        I++;
    }    
    return 0;
}
char pick() {
    char c = cin.peek();
    while (c==' '||c=='\n')
    {
        cin.get();
        c = cin.peek();
    }
    return c;
}
char get() {
    char c = cin.get();
    while (c==' '||c=='\n')
    {
        c = cin.get();
    }
    return c;
}
bool orex() {
    bool result = andex(), more = 1;
    while (more)
    {
        char c = pick();
        if (c == '|') {
            get();
            bool re = andex();
            result |= re;
        }
        else more = 0;
    }
    return result;
}
bool andex() {
    bool result = notex(), more = 1;
    while (more)
    {
        char c = pick();
        if (c == '&') {
            get();
            bool re = notex();
            result &= re;
        }
        else more = 0;
    }
    return result;
}
bool notex() {
    bool result;
    char c = pick();
    if (c == '!') {
        get();
        result = notex();
        return !result;
    }
    else if (c == '(') {
        get();
        result = orex();
        get();
        return result;
    }
    else if (c == 'F') {
        get();
        return 0;
    }
    else {
        get();
        return 1;
    }
}
原文地址:https://www.cnblogs.com/xxxsans/p/12718690.html