Cod sursa(job #2791924)

Utilizator andrei_C1Andrei Chertes andrei_C1 Data 31 octombrie 2021 13:55:41
Problema Bool Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.82 kb
#include <stdio.h>
#include <string>

FILE *fin, *fout;

const int sigma = 26;
const int DIM = 1000;

int N;
int pos;
std :: string s(DIM, 0);
int val[sigma];

int eval();

int NOT() {
    int ret = 0;

    while(s[pos] == 'N' && s[pos + 1] == 'O' && s[pos + 2] == 'T') {
        ret = !ret;
        pos += 4;
    }

    return ret;
}

int fact() {
    int ret, sign;

    sign = NOT();

    if(isupper(s[pos])) {
        if(s[pos] == 'T' && s[pos + 1] == 'R' && s[pos + 2] == 'U' && s[pos + 3] == 'E') {
            ret = 1;
            pos += 3;
        } else if(s[pos] == 'F' && s[pos + 1] == 'A' && s[pos + 2] == 'L' && s[pos + 3] == 'S' && s[pos + 4] == 'E') {
            ret = 0;
            pos += 4;
        } else {
            ret = val[s[pos] - 'A'];
        }
        pos++;
    } else {
        pos++;
        ret = eval();
        pos++;
    }

    if(sign == 0) {
        return ret;
    } else {
        return !ret;
    }
}

int calcul(int a, int b, std :: string op) {
    if(op == "AND") {
        return a & b;
    } else if(op == "OR") {
        return a | b;
    }
}

int eval() {
    int ret = fact();

    while(s[pos] == ' ') {
        pos++;
        std :: string op;
        while(s[pos] != ' ') {
            op.push_back(s[pos]);
            pos++;
        }
        pos++;
        ret = calcul(ret, fact(), op);
    }

    return ret;
}

int main() {
    fin = fopen("bool.in", "r");
    fout = fopen("bool.out", "w");

    fgets(const_cast<char*>(s.data()), DIM, fin);
    s = s.data();

    fscanf(fin, "%d\n", &N);
    for(int i = 1; i <= N; i++) {
        char lit = fgetc(fin);
        val[lit - 'A'] = !val[lit - 'A'];
        pos = 0;
        fprintf(fout, "%d", eval());
    }

    fclose(fin);
    fclose(fout);
    return 0;
}