Cod sursa(job #1122516)

Utilizator thewildnathNathan Wildenberg thewildnath Data 25 februarie 2014 18:38:18
Problema Bool Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.7 kb
#include<stdio.h>
#include<string.h>

#define LMAX 1002

int i,lung;
char sir[LMAX];
int val[30];

// Semne: OR 0,NOT 1, AND 2



bool calc();

inline void ignore()
{
    while(sir[i]==' ')
        ++i;
}

inline int get_num()
{
    ignore();

    switch(sir[i])
    {
        case '(':{++i;return calc();}
        default  :{++i;return val[sir[i-1]-'A'];}
    }
}
inline int get_type()
{
    ignore();

    switch(sir[i])
    {
        case 'O':{i+=2;return 0;}
        case 'N':{i+=3;return 1;}
        case 'A':{i+=3;return 2;}
    }
}

bool calc()
{
    bool a,b,c;
    int s1,s2,aux;
    a=b=c=s1=s2=0;

    c=get_num();

    for(;i<lung;++i)
    {
        if(s2==0){
            switch(s1)
            {
                case 0:{a=a|b   ;break;}
                case 1:{a=a&(1-b);break;}
                case 2:{a=a&b   ;break;}
            }
            b=c;
            s1=s2;
        }else if(s2==1){
            b=b&(1-c);
        }else if(s2==2){
            switch(s1)
            {
                case   1:{  a=a&(1-b);
                            b=c;
                            s1=s2;
                            break;
                }default :{b=b&c;break;}
            }
        }

        ignore();

        if(sir[i]==')')
        {
            s2=0;
            c=1;
            ++i;
            break;
        }

        s2=get_type();
        ignore();

        if(sir[i]=='N'&&sir[i+1]=='O')
        {
            aux=get_type();
            c=get_num();
            c=1-c;
        }
        else
            c=get_num();


    }


                                            // FINISH
    if(s2==0){
        switch(s1)
        {
            case 0:{a=a|b   ;break;}
            case 1:{a=a&(1-b);break;}
            case 2:{a=a&b   ;break;}
        }
        b=c;
        s1=s2;
    }else if(s2==1){
        b=b&(1-c);
    }else if(s2==2){
        switch(s1)
        {
            case   1:{  a=a&(1-b);
                        b=c;
                        s1=s2;
                        break;
            }default :{b=b&c;break;}
        }
    }

    switch(s1)
    {
        case 0:{a=a|b;break;}
        case 1:{a=a&(1-b);break;}
        case 2:{a=a&b;break;}
    }

    return a;
}

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

    gets(sir);
    lung=strlen(sir);
    scanf("%d\n",&n);

    while(scanf("%c",&c))
    {

        if(c<'A'||c>'Z')
            break;

        val[c-'A']=1-val[c-'A'];
        i=0;

        rez=calc();

        printf("%d",rez);
    }

    printf("\n");

    return 0;
}