Cod sursa(job #1738934)

Utilizator cristina_borzaCristina Borza cristina_borza Data 8 august 2016 10:13:14
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <fstream>
#include <cstring>

using namespace std;

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


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

bool solve();
bool term();
bool fact();
bool getnr();

int main() {
    f.getline(s , 1000);
    dim = strlen(s);

    f >> n;
    for(int i = 0; i < n; ++i) {
        f >> ch;
        c[ch - 65] = !c[ch - 65];
        p = 0;
        g << solve();
    }
}

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

    return f;
}

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

    return f;
}

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

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

        while(s[p] == ' ' && p < dim) {
            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 < dim) {
                p++;
            }
        }
    }

    if(aux == 0)
        return true;

    if(aux == 1)
        return false;

    return f;
}

bool getnr() {
    return c[s[p] - 65];
}