Cod sursa(job #1738268)

Utilizator cristina_borzaCristina Borza cristina_borza Data 6 august 2016 12:07:51
Problema Bool Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <fstream>
#include <cstring>

using namespace std;

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

char s[1005] , ch;
int n , c[50] , i;

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

int main() {
    f.getline(s + 1 , 1005 , '\n');
    f >> n;

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

        pos = 1;
        g << solve(pos);
    }
    return 0;
}

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

    return t1;
}

int term(int &p) {
    int f1 = fact(p);
    while(p <= strlen(s + 1) - 2 && s[p] == 'A' && s[p + 1] == 'N') {
        p += 4;
        int f2 = fact(p);

        f1 = (f1 && f2);
    }

    return f1;
}


int fact(int &p) {
    int rez , aux = -1;
    if(s[p] == '(') {
        ++p;
        rez = solve(p);
        ++p;

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

    else {
        if (s[p] == 'N' && s[p + 1] == 'O') {
            p += 4;
            return !(solve(p));
        }

        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;
            }


            rez = getnr(p);
        }
    }

    if (aux != -1) {
        return aux;
    }

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

    return rez;
}

int getnr(int &p) {
    int sol = c[s[p] - 'A'];
    p += 2;

    return sol;
}