Cod sursa(job #2789537)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 27 octombrie 2021 17:08:39
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <stdio.h>
#define MAX 2048

char s[ MAX ];
int a[ 30 ];
int poz;

void spatii() {
    while( s[ poz ] == ' ' )
        ++poz;
}

int exp();

int var() {
    if( s[ poz ] == 'T' && s[ poz + 1 ] == 'R' && s[ poz + 2 ] == 'U' && s[ poz + 3 ] == 'E' ) {
        poz += 4;
        spatii();
        return 1;
    } else if( s[ poz ] == 'F' && s[ poz + 1 ] == 'A' && s[ poz + 2 ] == 'L' && s[ poz + 3 ] == 'S' && s[ poz + 4 ] == 'E' ) {
        poz += 5;
        spatii();
        return 0;
    } else return a[ s[ poz++ ] - 'A' ];
}

void calcul_not( int& rez ) {
    int neg = 0;
    while( s[ poz ] == 'N' && s[ poz + 1 ] == 'O' && s[ poz + 2 ] == 'T' ) {
        neg ^= 1;
        poz += 3;
        spatii();
    }

    if( s[ poz ] == '(' ) {
        ++poz;
        rez = exp();
        ++poz;
        spatii();
    } else rez = var();

    rez ^= neg;
}

int no() {
    int rez;

    calcul_not( rez );
    spatii();

    while( s[ poz ] == 'A' && s[ poz + 1 ] == 'N' && s[ poz + 2 ] == 'D' ) {
        poz += 3;
        spatii();
        int val = 0;
        calcul_not( val );

        rez &= val;

        spatii();
    }

    return rez;
}


int exp() {
    int rez = no();

    spatii();
    while( s[ poz ] == 'O' && s[ poz + 1 ] == 'R' ) {
        poz += 2;
        spatii();
        rez |= no();
        spatii();
    }

    return rez;
}

int main()
{
    FILE *fin = fopen( "bool.in", "r" );
    FILE *fout = fopen( "bool.out", "w" );

    fgets( s, MAX, fin );

    int q;
    fscanf( fin, "%d\n", &q );
    while( q-- ) {
        char ch;
        fscanf( fin, "%c", &ch );
        a[ ch - 'A' ] ^= 1;

        poz = 0;
        fprintf( fout, "%d", exp() );
    }

    fclose( fin );
    fclose( fout );
    return 0;
}