Cod sursa(job #2600867)

Utilizator nicolaefilatNicolae Filat nicolaefilat Data 13 aprilie 2020 13:31:59
Problema Bool Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <iostream>
#include <fstream>

using namespace std;

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

/// NOT > AND > OR
string s;
int n,i;
bool v[30];

int myor();
int myand();
int mynot();
int expresie();

int myor(){
    int raspuns = myand();
    while(i + 1 < n && s[i] == 'O' && s[i + 1] == 'R'){
        i += 2;
        int altceva = myand();
        raspuns = raspuns or altceva;
    }
    return raspuns;
}
int myand(){
    int raspuns = mynot();
    while(i + 2 < n && s[i] == 'A' && s[i + 1] == 'N' && s[i + 2] == 'D'){
        i += 3;
        int altceva = mynot();
        raspuns = raspuns & altceva;
    }
    return raspuns;
}
int mynot(){
    int raspuns = expresie();
    while(i + 2 < n && s[i] == 'N' && s[i + 1] == 'O' && s[i + 2] == 'T'){
        i += 3;
        int altceva = expresie();
        raspuns = !altceva;
    }
    return raspuns;
}

int expresie(){
    int raspuns = 0;
    while(s[i] == ' ')
        i++;
    if(i < n){
        if(s[i] == '('){
            i++;
            raspuns = myor();
            i++;
        }else{
            if(isalpha(s[i])){
                if((i == 0 && !isalpha(s[i + 1])) || (i == n - 1 && !isalpha(s[i - 1]))
                   || (!isalpha(s[i - 1]) && !isalpha(s[i + 1]))){
                        char c = s[i];
                        raspuns = v[c - 'A'];
                   }
            }
        }
    }
    return raspuns;
}

int main()
{
    getline(in,s);
    n = s.size();
    int chestii;
    in>>chestii;
    /// B or A
    for(int i = 1; i <= chestii; i++){
        char caracter;
        in>>caracter;
        if(!v[caracter - 'A'])
            v[caracter - 'A'] = 1;
        else
            v[caracter - 'A'] = 0;
        out<<myor();
    }

    return 0;
}