Cod sursa(job #3129996)

Utilizator dobreraduDobre Radu Fabian dobreradu Data 16 mai 2023 16:23:57
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.11 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream fin ("bool.in");
ofstream fout("bool.out");

const int DIM = 1001;
char s[DIM], t[DIM];
int i = 0, k = 0, n;
bool f[DIM];

int expresieOr();
int expresieAnd();
int expresieXor();
int factor();

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

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

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

int factor() {
    int r;
    if (t[i] == '(') {
        i++;
        r = expresieOr();
        i++;
    } else {
        if (t[i] == '0') {
            i++;
            return 0;
        }
        if (t[i] == '1') {
            i++;
            return 1;
        }
        return f[t[i++]];
    }
    return r;
}

int main() {
    fin.getline(s, DIM);

    for (int index = 0; s[index] != '\0'; index++) {
        if (s[index] == ' ')
            continue;
        if (s[index] == 'T' && s[index + 1] == 'R') {
            t[k++] = '1';
            index += 3;
            continue;
        }
        if (s[index] == 'F' && s[index + 1] == 'A') {
            t[k++] = '0';
            index += 4;
            continue;
        }
        if (s[index] == 'O' && s[index + 1] == 'R') {
            t[k++] = '|';
            index++;
            continue;
        }
        if (s[index] == 'A' && s[index + 1] == 'N') {
            t[k++] = '&';
            index += 2;
            continue;
        }
        if (s[index] == 'N' && s[index + 1] == 'O') {
            t[k++] = '1', t[k++] = '^';
            index += 2;
            continue;
        }
        t[k++] = s[index];
    }
    t[k] = '\0';

    fin >> n;
    for (int j = 1; j <= n; j++) {
        char c;
        fin >> c;
        f[c] = !f[c];
        i = 0;
        fout << expresieOr();
    }

    return 0;
}