Cod sursa(job #988348)

Utilizator FlameingoAiordachioaei Marius Flameingo Data 22 august 2013 17:31:53
Problema Bool Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <cstdio>
#include <bitset>
#include <iostream>
#include <cstring>
using namespace std;

const int SMAX = 1003, NMAX = 103;

bitset <26> val;
char S[SMAX], C[NMAX], *p;

bool _or ();
bool _and ();
bool _not ();
inline bool sub_expression ();

int main () {

    freopen ("bool.in", "r", stdin);
    freopen ("bool.out", "w", stdout);
    int N, i;
    cin.get (S + 1, SMAX);
    S[strlen (S + 1) + 1] = ' ';
    scanf ("%d%s", &N, C + 1);
    for (i = 1; i <= N; ++i) {
        val.flip (C[i] - 'A');
        p = S + 1;
        C[i] = _or () + '0';
    }
    printf ("%s", C + 1);

}

bool _or () {

    bool value = _and ();
    if (*p == 'O' && p[1] == 'R') {
        p += 3;
        value = value || _and ();
    }
    return value;

}

bool _and () {

    bool value = _not ();
    if (*p == 'A' && p[1] == 'N') {
        p += 4;
        value = value && _not ();
    }
    return value;

}

bool _not () {

    bool value;
    if (*p == 'N' && p[1] == 'O') {
        p += 4;
        value = sub_expression ();
        return !value;
    }
    return sub_expression ();

}

inline bool sub_expression () {

    bool value;
    if (*p == '(') {
        ++p;
        value = _or ();
        ++p;
        return value;
    }
    if (*p == 'T' && p[1] == 'R') {
        p += 5;
        return 1;
    }
    if (*p == 'F' && p[1] == 'A') {
        p += 6;
        return 0;
    }
    if (*p <= 'Z' && *p >= 'A' && p[1] == ' ') {
        value = val[*p - 'A'];
        p += 2;
        return value;
    }

}