Cod sursa(job #2209096)

Utilizator OctavianVasileVasileOctavian OctavianVasile Data 1 iunie 2018 18:30:47
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("bool.in");
ofstream fout ("bool.out");
bool f[28];
bool Expresie(char* &s);
void skip_spaces(char* &s){
    while (*s == ' ') s ++;
}
bool Termen(char* &s) {
    bool answer;
    skip_spaces(s);
    if (s[0] == 'F' && s[1] == 'A'
        && s[2] == 'L' && s[3] == 'S'
        && s[4] == 'E') {
        s += 5;
        answer = false;
    }
    else if (s[0] == 'T' && s[1] == 'R'
        && s[2] == 'U' && s[3] == 'E') {
        s += 4;
        answer = true;
    }
    else if (s[0] == '('){
        s++;
        answer =  Expresie (s);
        s++;
        skip_spaces(s);
    }
    else if (isalpha (s[0])) {
        answer = f[s[0] - 'A'];
        s++;
    }
    return answer;
}
bool Not (char* &s){
    bool nu = 0;
    skip_spaces (s);
    while  (*s == 'N' && *(s + 1) == 'O' && *(s + 2) == 'T'){
        s += 3;
        nu = 1 - nu;
        skip_spaces (s);
    }
    bool val;
    if (nu == 0)
      val = Termen(s);
    else
      val = !Termen(s);
    return val;
}
bool ands (char* &s){
    bool val = Not(s);
    skip_spaces(s);
    while (s[0] == 'A' && s[1] == 'N' && s[2] == 'D') {
        s += 3;
        skip_spaces (s);
        val &= Not (s);
        skip_spaces (s);
    }
    return val;
}
bool Expresie (char* &s){
    bool val = ands(s);
    skip_spaces (s);
    while (s[0] == 'O' && s[1] == 'R') {
        s += 2;
        skip_spaces (s);
        val |= ands (s);
        skip_spaces (s);
    }
    return val;
}
int main (void){
    int n;
    string s;
    char ch;
    while (fin.get(ch)) {
        if (ch == '\n')
            break;
        s += ch;
    }
    s += '\0';
    fin >> n;
    for (int i = 1; i <= n; i ++){
        fin >> ch;
        ch -= 'A';
        f[ch] = !f[ch];
        char *sir = &s[0];
        if (Expresie(sir)) fout << '1';
        else fout << '0';
    }
    return 0;
}