Cod sursa(job #2140353)

Utilizator robx12lnLinca Robert robx12ln Data 23 februarie 2018 12:21:59
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
bool F[1000];
int L, M, pos;
string s;

bool expresie();
bool termen();
bool factor();

bool expresie(){
    bool R = termen();
    while( pos < L && s[pos] == '|' ){
        pos++;
        R |= termen();
    }
    return R;
}
bool termen(){
    bool R = factor();
    while( pos < L && s[pos] == '&' ){
        pos++;
        R &= factor();
    }
    return R;
}

bool factor(){
    bool R , ok = false;
    while( pos < L && s[pos] == '!' )
        ok = !ok, pos++;
    if( s[pos] == '(' ){
        pos++;
        R = expresie();
        pos++;
    }else{
        R = F[ s[pos] - 'A' ];
        pos++;
    }
    if( ok == true )
        R = !R;
    return R;
}

int main(){
    getline( fin, s );
    while( s.find( "TRUE" ) != string::npos )
        s.replace( s.find( "TRUE" ), 4, "t" );
    while( s.find( "FALSE" ) != string::npos )
        s.replace( s.find( "FALSE" ), 5, "f" );
    while( s.find( "AND" ) != string::npos )
        s.replace( s.find( "AND" ), 3, "&" );
    while( s.find( "OR" ) != string::npos )
        s.replace( s.find( "OR" ), 2, "|" );
    while( s.find( "NOT" ) != string::npos )
        s.replace( s.find( "NOT" ), 3, "!" );
    while( s.find( " " ) != string::npos )
        s.replace( s.find( " " ), 1, "" );
    F[ 't' - 'A' ] = true;
    L = (int)s.length();
    fin >> M;
    for( int i = 0; i < M; i++ ){
        char x; fin >> x;
        F[ x - 'A' ] = !F[ x - 'A' ];
        pos = 0;
        fout << expresie();
    }
    return 0;
}