Cod sursa(job #2832789)

Utilizator Titus_PirsanTitus-Teodor Pirsan Titus_Pirsan Data 14 ianuarie 2022 12:10:54
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.29 kb
#include <fstream>
#include <string>

using namespace std;

const string filename = "bool";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");

const int max_size = 92;
const int str_size = 1e3 + 5;

char str[str_size];
bool val[max_size];
int index;

bool factor();
bool and_operation();
bool or_operation();

bool factor() {
    bool n = false;
    bool n_opposite = false;

    while (str[index] == '!') {
        index++;
        n_opposite = !n_opposite;
    }

    if (str[index] == '(') {
        index++;
        n = or_operation();
    } else if (str[index] >= 'A' && str[index] <= 'Z') {
        n = val[str[index]];
    } else if (str[index] == 0 || str[index] == 1) {
        n = str[index];
    }
    index++;
    return n ^ n_opposite;
}

bool and_operation() {
    bool ans;
    ans = factor();
    while (str[index] == '&') {
        index++;
        ans = factor() & ans;
    }
    return ans;
}

bool or_operation() {
    bool ans;
    ans = and_operation();
    while (str[index] == '|') {
        index++;
        ans = and_operation() | ans;
    }
    return ans;
}

void build_exp() {}

int main() {
    int n;

    fin.getline(str, str_size);
    fin >> n;

    int i = 0;
    index = 0;
    while (str[index] != '\n') {
        if (str[index] == 'T' && str[index + 1] == 'R') {
            str[i++] = 1;
            index += 3;
        } else if (str[index] == 'F' && str[index + 1] == 'A') {
            str[i++] = 0;
            index += 4;
        } else if (str[index] == 'N' && str[index + 1] == 'O') {
            str[i++] = '!';
            index += 2;
        } else if (str[index] == 'A' && str[index + 1] == 'N') {
            str[i++] = '&';
            index += 2;
        } else if (str[index] == 'O' && str[index + 1] == 'R') {
            str[i++] = '|';
            index++;
        } else if (str[index] == '(' || str[index] == ')') {
            str[i++] = str[index];
        } else if (str[index] >= 'A' && str[index] <= 'Z') {
            str[i++] = str[index];
        }
        index++;
    }
    str[i] = '\n';

    for (int i = 0; i < n; i++) {
        char c;
        fin >> c;
        val[c] = !val[c];
        index = 0;
        fout << or_operation();
    }

    return 0;
}