Cod sursa(job #2150177)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 3 martie 2018 12:28:29
Problema Bool Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <bits/stdc++.h>

using namespace std;

FILE *fi, *fout;

const int MAXN = 1010;

char str[MAXN + 1];
bool ok[256];
int pos, n;

bool OR();

inline void skip() {
    while(pos <= n && str[pos] == ' ')
        pos++;
}

bool get() {
    skip();
    bool ans;
    if(str[pos] == '(') {
        pos++;
        ans = OR();
        skip();
        pos++;
    }
    else if(str[pos] == 'T' && str[pos + 1] == 'R') {
        pos += 4;
        ans = 1;
    }
    else if(str[pos] == 'F' && str[pos + 1] == 'A') {
        pos += 5;
        ans = 0;
    }
    else if(str[pos] == 'N' && str[pos + 1] == 'O') {
        pos += 3;
        skip();
        ans = get();
        skip();
    }
    else {
        ans = ok[str[pos]];
        pos++;
    }
    skip();
    return ans;
}

bool AND() {
    skip();
    bool ans = get();
    skip();
    while(pos <= n && str[pos] == 'A' && str[pos + 1] == 'N') {
        pos += 3;
        ans &= get();
        skip();
    }
    return ans;
}

bool OR() {
    skip();
    bool ans = AND();
    skip();
    while(pos <= n && str[pos] == 'O' && str[pos + 1] == 'R') {
        pos += 2;
        ans |= AND();
        skip();
    }
    return ans;
}

int main() {
    int q;
    fi = fopen("bool.in" ,"r");
    fout = fopen("bool.out" ,"w");
    fgets(str + 1, MAXN, fi);
    n = strlen(str + 1);
    fscanf(fi,"%d " ,&q);
    while(q > 0) {
        q--;
        char ch = fgetc(fi);
        ok[ch] ^= 1;
        pos = 1;
        fprintf(fout,"%d" ,OR());
    }
    fclose(fi);
    fclose(fout);
    return 0;
}