Cod sursa(job #2140328)

Utilizator robx12lnLinca Robert robx12ln Data 23 februarie 2018 11:36:33
Problema Bool Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
int F[200], L, M, pos;
char s[1005], Sol[105];

int expresie();
int termen();
int factor();

int expresie(){
    int R = termen();
    while( pos < L && s[pos] == ' ' || (s[pos] == 'O' && s[pos + 1] == 'R') ){
        if( s[pos] == ' ' )
            pos++;
        if(s[pos] == 'O' && s[pos + 1] == 'R'){
            pos += 2;
            R |= termen();
        }
    }
    return R;
}
int termen(){
    int R = factor();
    while( pos < L && s[pos] == ' ' || (s[pos] == 'A' && s[pos + 1] == 'N') ){
        if( s[pos] == ' ' )
            pos++;
        if(s[pos] == 'A' && s[pos + 1] == 'N'){
            pos += 3;
            R &= factor();
        }
    }
    return R;
}
int factor(){
    int R = 0, ok = 0;
    while( pos < L && s[pos] == ' ' )
        pos++;
    if( s[pos] == 'N' && s[pos + 1] == 'O' ){
        pos += 3;
        ok = 1;
    }
    while( pos < L && s[pos] == ' ' )
        pos++;
    if( s[pos] == '(' ){
        pos++;
        R = expresie();
        pos++;
    }else{
        if( s[pos] == 'T' && s[pos + 1] == 'R' )
            R = 1, pos += 4;
        else
            if( s[pos] == 'F' && s[pos + 1] == 'A' )
                R = 0, pos += 5;
            else
                R = F[ s[pos] - 'A' ], pos++;
    }
    if( ok == 1 )
        R = !R;
    return R;
}
int main(){
    fin.getline( s, 1005 );
    L = strlen( s );
    fin >> M;
    for( int i = 0; i < M; i++ ){
        char x; fin >> x;
        F[ x - 'A' ] = !F[ x - 'A' ];
        pos = 0;
        int val = expresie();
        Sol[i] = ( val == 1 ) ? '1' : '0';
    }
    fout << Sol;
    return 0;
}