Cod sursa(job #2705542)

Utilizator ptlsebiptl sebi ptlsebi Data 12 februarie 2021 19:17:57
Problema Bool Scor 40
Compilator c-64 Status done
Runda Arhiva de probleme Marime 2.99 kb
#include <stdio.h>
#include <stdint.h>

void read_uint32_t(FILE *__restrict stream, uint32_t *__restrict nr) {
    uint8_t ch;
    *nr = 0;
    while ((ch = fgetc(stream)) && ('0' <= ch && ch <= '9')) {
        *nr *= 10;
        *nr += ch - '0';
    }
}

void readline(FILE *__restrict stream, uint8_t *str, uint32_t *strl) {
    uint8_t ch;
    *strl = 0;
    while ((ch = fgetc(stream)) &&
           (('A' <= ch && ch <= 'Z') || '(' == ch || ')' == ch || ' ' == ch)) {
        str[*strl] = ch;
        ++*strl;
    }
    str[*strl] = '\0';
}

uint8_t line[1001];
uint32_t linel;

uint8_t vars[27];

void tog_bit(uint8_t bit) {
    vars[bit - 'A'] ^= 1;
}

uint32_t pos;

#define cpos line[pos]
#define npos line[pos+1]


enum cop_e {
    VAR, AND, OR, NOT, LP, RP, TRUE, FALSE, UN
};

uint8_t g_cop() {
    if (cpos == 'A' && npos == 'N') {
        return AND;
    } else if (cpos == 'O' && npos == 'R') {
        return OR;
    } else if (cpos == 'N' && npos == 'O') {
        return NOT;
    } else if (cpos == '(') {
        return LP;
    } else if (cpos == ')') {
        return RP;
    } else if (cpos == 'T' && npos == 'R') {
        return TRUE;
    } else if (cpos == 'F' && npos == 'A') {
        return FALSE;
    } else if ('A' <= cpos && cpos <= 'Z') {
        return VAR;
    }
    return UN;
}

void g_n() {
    uint8_t v = g_cop();
    if (v == AND || v == NOT) {
        pos += 3;
    } else if (v == OR) {
        pos += 2;
    } else if (v == LP || v == RP || v == VAR) {
        pos += 1;
    } else if (v == TRUE || v == FALSE) {
        pos += 4;
    }
    while (cpos == ' ' && pos < linel) {
        ++pos;
    }
}

uint8_t get_var() {
    if (g_cop() == NOT) {
        g_n();
        return !get_var();
    } else {
        if (g_cop() == TRUE || g_cop() == FALSE) {
            return g_cop() == TRUE;
        } else {
            return vars[cpos - 'A'];
        }
    }
}

uint8_t eval();

uint8_t ands();

uint8_t ands() {
    uint8_t v;
    uint8_t neg = 0;
    while (g_cop() == NOT && pos < linel) {
        g_n();
        neg ^= 1;
    }

    if (g_cop() == LP) {
        g_n();
        v = eval();
        if (g_cop() != RP) {
            return 8;
        }
        g_n();
    } else {
        v = get_var();
        g_n();
    }

    v ^= neg;

    while (g_cop() == AND && pos < linel) {
        g_n();
        v &= ands();
    }

    return v;
}

uint8_t eval() {
    uint8_t v = ands();
    while (g_cop() == OR && pos < linel) {
        g_n();
        v |= eval();
    }
    return v;
}

int main() {
    uint32_t q;
    {
        FILE *__restrict in = fopen("bool.in", "r");

        readline(in, line, &linel);
        read_uint32_t(in, &q);
        {
            FILE *__restrict out = fopen("bool.out", "w");
            int32_t i;
            uint8_t ch;
            for (i = 0; i < q; ++i) {
                ch = fgetc(in);
                tog_bit(ch);
                pos = 0;
                fprintf(out , "%u", eval());
            }
            fclose(out);
        }

        fclose(in);
    }
    return 0;
}