Cod sursa(job #1738511)

Utilizator cristina_borzaCristina Borza cristina_borza Data 6 august 2016 20:50:21
Problema Bool Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream f ("bool.in");
ofstream g ("bool.out");

char s[2005] , ch;
int n , c[50] , p;

int solve();
int getnr();
int term();
int fact();

int main() {
    f.getline(s , 1005);
    f >> n;

    for (int i = 1; i <= n; i++) {
        f >> ch;
        c[ch - 'A'] = !c[ch - 'A'];

        p = 0;
        g << solve();
    }

    return 0;
}

int solve() {
    int f = term();
    while (p < strlen(s) && s[p] == 'O' && s[p + 1] == 'R') {
        p = p + 3;
        f = (term() || f);
    }
    return f;
}

int term() {
    int f = fact();
    while (p < strlen(s) && s[p] == 'A' && s[p + 1] == 'N' && s[p + 2] == 'D') {
        p += 4;
        f = fact() && f;
    }

    return f;
}

int fact() {
    int f , aux = -1;

    if (s[p] == '(') {
        ++p;
        f = solve();
        ++p;

        while (s[p] == ' ' && p < strlen(s)) {
            p++;
        }
    }

    else {
        if (s[p] == 'N' && s[p + 1] == 'O' && s[p + 2] == 'T') {
            p += 4;
            f = !fact();
        }

        else {
            if (s[p] == 'T' && s[p + 1] == 'R' && s[p + 2] == 'U' && s[p + 3]=='E') {
                aux = 0;
                p +=  3;
            }

            if (s[p] == 'F' && s[p + 1] == 'A' && s[p + 2] == 'L' && s[p + 3] == 'S' && s[p + 4]=='E') {
                aux = 1;
                p += 4;
            }

            f = getnr();
            ++p;

            while(s[p] == ' ' && p < strlen(s)) {
                p++;
            }
        }
    }

    if(aux == 0)
        return 0;

    if(aux == 1)
        return 1;

    return f;
}

int getnr() {
    return c[s[p] - 'A'];
}