Cod sursa(job #1110242)

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

using namespace std;

const int Lmax = 3000;
const int Sigma = 256;

char expr[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 ( expr[ point ] == 'N' && expr[ point + 1 ] == 'O ' )
    {
        point += 3;
        res = !eval_not();

        return res;
    }

    if ( expr[ point ] == 'T' && expr[ point + 1 ] == 'R' )
    {
        point += 4;
        res = 1;

        return res;
    }

    if ( expr[ point ] == 'F' && expr[ point + 1] == 'A' )
    {
        point += 5;
        res = 0;

        return res;
    }

    return val[ expr[ point++ ] ];
}

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

    while ( expr[ point ] == 'A' && expr[ point + 1] == 'N' )
    {
        point += 4;
        t &= eval_not();
    }

    return t;
}

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

    while ( expr[ point ] == 'O' && expr[ point ] == 'R' )
    {
        point += 2;
        t |= eval_and();
    }

    return t;
}

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

    char c;

    gets( expr + 1 );

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

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

        val[ c ] ^= 1;

        point = 1;

        cout << eval_or();
    }

    return 0;
}