Cod sursa(job #986750)

Utilizator stefanfStefan Fulger stefanf Data 19 august 2013 14:49:50
Problema Bool Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 1.58 kb
#include<stdio.h>
#include<string.h>

char str[1000];
int vars[30];
int ind, len;

int factor();
int term();

int expression() {
    int e = factor();
    while(ind < len && (str[ind] == ' ' || !strncmp(str+ind, "OR", 2))) {
        switch(str[ind]) {
            case ' ': ind++;
                      break;
            case 'O': ind += 3;
                      e |= factor();
                      break;
        }
    }

    return e;
}

int factor() {
    int f = term();
    while(ind < len && (str[ind] == ' ' || !strncmp(str + ind, "AND", 3))) {
        switch(str[ind]) {
            case ' ': ind++;
                      break;
            case 'A': ind += 4;
                      f &= term();
                      break;
        }
    }

    return f;
}

int term() {
    if(str[ind] == '(') {
        ind++;
        int t = expression();
        ind++;
        return t;
    }

    if (!strncmp(str + ind, "NOT", 3)) {
        ind += 4;
        int t = term();
        return t ^ 1;
    }

    if (!strncmp(str + ind, "FALSE", 5)) {
        ind+=5;
        return 0;
    }

    if (!strncmp(str + ind, "TRUE", 4)) {
        ind+=4;
        return 1;
    }

    char c = str[ind];
    ind++;

    return vars[c - 'A'];
}

int main() {
    int n, i;
    char difs[105];
    freopen("bool.in", "r", stdin);
    //freopen("bool.out", "w", stdout);
    
    scanf("%[^\n]", str);
    len = strlen(str);
    scanf("%d", &n);
    scanf("%s", difs);
    for(i = 0; i < n; i++) {
        vars[difs[i] - 'A'] ^= 1;
        int e = expression();
        printf("%d", e);
        ind = 0;
    }

    return 0;
}