Cod sursa(job #3294210)

Utilizator EricDimiericdc EricDimi Data 19 aprilie 2025 13:25:04
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.45 kb
#include <bits/stdc++.h>

using namespace std;

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

const int MAX_LEN = 1'000;
const int SIGMA = 26;

char s[MAX_LEN + 5], t[MAX_LEN + 5];
int F[SIGMA + 5];
int i, k, n;
char c;

int expresieOr();
int expresieAnd();
int expresieXor();
int factor();
/// XOR este cel mai prioritar, apoi AND si apoi OR

int main() {
    f.getline(s, MAX_LEN);
    
    for (i = 0; s[i] != 0; i++) {
        if (s[i] == ' ') /// ignoram spatiile in t[]
            continue;
        if (s[i] == '(' || s[i] == ')') { /// paranteza
            t[k++] = s[i];
            continue;
        }
        if (s[i] == 'T' && s[i + 1] == 'R') { /// TRUE
            t[k++] = '1';
            i += 3;
            continue;
        }
        if (s[i] == 'F' && s[i + 1] == 'A') { /// FALSE
            t[k++] = '0';
            i += 4;
            continue;
        }
        if (s[i] == 'O' && s[i + 1] == 'R') { /// OR
            t[k++] = '|';
            i++;
            continue;
        }
        if (s[i] == 'A' && s[i + 1] == 'N') { /// AND
            t[k++] = '&';
            i += 2;
            continue;
        }
        if (s[i] == 'N' && s[i + 1] == 'O') { /// NOT
            t[k++] = '1';
            t[k++] = '^';
            i += 2;
            continue;
        }
        t[k++] = s[i]; /// litera
    }
    
    f >> n;
    cout << n;
    for (; n > 0; n--) {
        f >> c;
        F[c - 'A'] ^= 1;
        i = 0;
        g << expresieOr();
    }
    
    f.close();
    g.close();
    return 0;
}

int expresieOr() {
    int r = expresieAnd();
    while (t[i] == '|') {
        i++;
        r |= expresieAnd();
    }
    return r;
}

int expresieAnd() {
    int r = expresieXor();
    while (t[i] == '&') {
        i++;
        r &= expresieXor();
    }
    return r;
}

int expresieXor() {
    int r = factor();
    while (t[i] == '^') {
        i++;
        r ^= factor();
    }
    return r;
}

int factor() {
    /// Un factor poate fi: (expresieOr), constanta(0, 1), sau variabila(A, B, C, etc.).
    int r;
    if (t[i] == '(') {
        i++;
        r = expresieOr();
        i++;
    }
    else {
        if (t[i] == '0') {
            i++;
            return 0;
        }
        else 
        if (t[i] == '1') {
            i++;
            return 1;
        }
        return F[t[i++] - 'A']; /// valoarea variabilei
    }
    return r;
}