Cod sursa(job #1110262)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 17 februarie 2014 21:50:59
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

const int Lmax = 1000;
const int Sigma = 256;

char expr[Lmax + 1], buf[Lmax + 1];
int val[Sigma + 1];

int N, point;

int eval_or();
int eval_and();
int eval_not();

int eval_not()
{
    int res = 0;

    if ( expr[ point ] == '(' )
    {
        point++;
        res = eval_or();
        point++;

        return res;
    }

    if ( strncmp( expr + point, "NOT", 3 ) == 0 )
    {
        point += 3;
        res = !eval_not();

        return res;
    }

    if ( strncmp( expr + point, "TRUE", 4 ) == 0 )
    {
        point += 4;
        res = 1;

        return res;
    }

    if ( strncmp( expr + point, "FALSE", 5 ) == 0 )
    {
        point += 5;
        res = 0;

        return res;
    }

    return val[ static_cast<int>( expr[ point++ ] ) ];
}

int eval_and()
{
    int t = eval_not();

    while ( strncmp( expr + point, "AND", 3 ) == 0 )
    {
        point += 3;
        t &= eval_not();
    }

    return t;
}

int eval_or()
{
    int t = eval_and();

    while ( strncmp( expr + point, "OR", 2 ) == 0 )
    {
        point += 2;
        t |= eval_and();
    }

    return t;
}

int main()
{
    freopen("bool.in", "r", stdin);
    freopen("bool.out", "w", stdout);

    char c;
    int n = 0;
    int i = 0;

    fgets( buf, Lmax, stdin );

    while ( buf[i] != '\n' )
    {
        if ( buf[i] != ' ' )
                expr[ n++ ] = buf[i];

        i++;
    }

    scanf("%d\n", &N);

    for ( int i = 0; i < N; ++i )
    {
        scanf("%c", &c);

        val[ static_cast<int>( c ) ] ^= 1;

        point = 0;

        cout << eval_or();
    }

    return 0;
}