Cod sursa(job #2071982)

Utilizator alexsandulescuSandulescu Alexandru alexsandulescu Data 21 noiembrie 2017 11:15:48
Problema Bool Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.82 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("bool.in");
ofstream g("bool.out");

int N, i, n;
bool a[255];
char str[1006];

bool eval_or();
bool eval_and();
bool eval_num();

bool eval_or() {
    bool res = eval_and();
    while(str[i] == 'O' && str[i + 1] == 'R' && i < n) {
        i += 2;
        if(str[i] == ' ') i++;
        bool s = eval_and();
        res = res or s;
    }
    return res;
}
bool eval_and() {
    bool res = eval_num();
    while(str[i] == 'A' && str[i + 1] == 'N' && str[i + 2] == 'D' && i < n) {
        i += 3;
        if(str[i] == ' ') i++;
        bool s = eval_num();
        res = res and s;
    }

    return res;
}
bool eval_num() {
    if(str[i] == '(' && i < n)
        i++, eval_or(), i++;
    else {
        bool ok = false;
        if(str[i] == 'T' && str[i + 1] == 'R' && str[i + 2] == 'U' && str[i + 3] == 'E' && i < n) {
            i += 4;
            if(str[i] == ' ') i++;
            return true;
        }
        if(str[i] == 'F' && str[i + 1] == 'A' && str[i + 2] == 'L' && str[i + 3] == 'S' && str[i + 4] == 'E' && i < n) {
            i += 5;
            if(str[i] == ' ') i++;
            return false;
        }
        if(str[i] == 'N' && str[i + 1] == 'O' && str[i + 2] == 'T' && i < n) {
            i += 3;
            if(str[i] == ' ') i++;
            ok = true;
        }
        if(isalpha(str[i]) && i < n) {
            bool x = a[str[i]];
            i += 1;
            if(str[i] == ' ') i++;
            if(!ok) return x;
            else    return !x;
        }
    }
}
int main()
{
    f.getline(str, 1003);
    n = strlen(str);
    f >> N;
    f.get();
    for(int x = 0; x < N; x++) {
        char c;
        i = 0;
        f.get(c);
        a[c] = !a[c];
        g << eval_or();
    }
    return 0;
}