Cod sursa(job #3375231)

Utilizator rapidu36Victor Manz rapidu36 Data 11 noiembrie 2026 10:13:07
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <fstream>
#include <cstring>

using namespace std;

const int NS = 1000;
const int NOP = 100;
const int NL = 26;

char s[NS+1];
char op[NOP+1];
bool valori[NL];
int p;

void transforma();
bool expresie();
bool termen();
bool factor();

int main() {
    ifstream fin("bool.in");
    ofstream fout("bool.out");
    fin.getline(s, NS + 1);
    transforma();
    int nop;
    fin >> nop >> op;
    for (int i = 0; i < nop; i++) {
        int poz_alfa = (op[i] - 'A');
        valori[poz_alfa] = (!valori[poz_alfa]);
        p = 0;
        fout << expresie();
    }
    fout << "\n";
    fin.close();
    fout.close();
    return 0;
}

void transforma() {
    int ramase = 0, i = 0;
    while (s[i] != '\0') {
        if (s[i] == 'F' && s[i+1] == 'A') {
            s[ramase++] = '0';
            i += 5;
        } else if (s[i] == 'T' && s[i+1] == 'R') {
            s[ramase++] = '1';
            i += 4;
        } else if (s[i] == 'A' && s[i+1] == 'N') {
            s[ramase++] = '&';
            i += 3;
        } else if (s[i] == 'N' && s[i+1] == 'O') {
            s[ramase++] = '!';
            i += 3;
        } else if (s[i] == 'O' && s[i+1] == 'R') {
            s[ramase++] = '|';
            i += 2;
        } else if (s[i] != ' ') {
            s[ramase++] = s[i++];
        } else { // s[i] == ' '
            i++;
        }
    }
    s[ramase] = '\0';
}

bool expresie() {
    bool rez = termen();
    while (s[p] == '|') {
        p++;
        rez = (termen() || rez);
    }
    return rez;
}

bool termen() {
    bool rez = factor();
    while (s[p] == '&') {
        p++;
        rez = (factor() && rez);
    }
    return rez;
}

bool factor() {
    bool semn = true;
    while (s[p] == '!') {
        semn = (!semn);
        p++;
    }
    bool rez;
    if (s[p] == '(') {
        p++;
        rez = expresie();
        p++;
        if (!semn) {
            rez = (!rez);
        }
        return rez;
    }
    if (s[p] == '0') {
        rez = false;
    } else if (s[p] == '1') {
        rez = true;
    } else { // s[p] = litera (deci variabila)
        int poz_alfa = (s[p] - 'A');
        rez = valori[poz_alfa];
    }
    p++;
    if (!semn) {
        rez = (!rez);
    }
    return rez;
}