Cod sursa(job #3231872)

Utilizator PescarusTanislav Luca Andrei Pescarus Data 27 mai 2024 22:44:46
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
char s[1005], t[1005];
int fr[105];
int n, i, k, q;
int expresieor();
int expresieand();
int expresiexor();
int factor();

int expresieor(){
    int r = expresieand();
    while(t[i] == '|'){
        i++;
        r = (r | expresieand());
    }
    return r;
}
int expresieand(){
    int r = expresiexor();
    while(t[i] == '&'){
        i++;
        r = (r & expresiexor());
    }
    return r;
}
int expresiexor(){
    int r = factor();
    while(t[i] == '^'){
        i++;
        r = (r ^ factor());
    }
    return r;
}
int factor(){
    int r;
    if(t[i] == '('){
        i++;
        r = expresieor();
        i++;
    }else{
        if(t[i] == '1'){
            i++;
            return 1;            
        }
        else if(t[i] == '0'){
            i++;
            return 0;
        }
        r = fr[t[i++] - 'A'];
    }   
    return r;
}
int main(){
    f.getline(s, 1005);
    for(int i = 0; i < strlen(s); i++){
        if(s[i] == ' ')continue;
        if(s[i] == 'T' && s[i + 1] == 'R'){
            t[k++] = '1';
            i+=3;
            continue;
        }
        if(s[i] == 'A' && s[i + 1] == 'N'){
            t[k++] = '&';
            i+=2;
            continue;
        }
        if(s[i] == 'F' && s[i + 1] == 'A'){
            t[k++] = '0';
            i+=4;
            continue;
        }
        if(s[i] == 'N' && s[i + 1] == 'O'){
            t[k++] = '1';
            t[k++] = '^';
            i += 2;
            continue;
        }
        if(s[i] == 'O' && s[i + 1] == 'R'){
            t[k++] = '|';
            i++;
            continue;
        }
        t[k++] = s[i];
    }
    f >> q;
    while(q--){
        char c;
        f >> c;
        fr[c - 'A'] = 1 - fr[c - 'A'];
        i = 0;
        int sum = expresieor();
        g << sum;
    }
}