Cod sursa(job #1497531)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 6 octombrie 2015 22:04:53
Problema Bool Scor 60
Compilator c Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <stdio.h>
#include <stdlib.h>
#define MAXN 1000
#define MAXC 26
char v[MAXN+10];
int vf[MAXC],poz,n;
inline int numar(){
    int nr;
    if(v[poz]=='T'&&v[poz+1]=='R'){
        nr=1;
        poz+=4;
    }
    if(v[poz]=='F'&&v[poz+1]=='A'){
        nr=0;
        poz+=5;
    }
    if(v[poz+1]<'A'||v[poz+1]>'Z'){
        nr=vf[v[poz]-'A'];
        poz++;
    }
    return nr;
}
int fact(){
    int nr;
    if(v[poz]>='A'&&v[poz]<='Z'&&(v[poz]!='N'||v[poz+1]!='O')){
           nr=numar();
           blanc();
    }
    else
        if(v[poz]=='('){
            poz++;
            blanc();
            nr=E();
            poz++;
            blanc();
        }
        else
            if(v[poz]=='N'&&v[poz+1]=='O'){
                poz+=3;
                blanc();
                nr=!fact();
            }
    return nr;
}
inline int term(){
    int nr,x;
    nr=fact();
    while(v[poz]=='A'&&v[poz+1]=='N'){
        poz+=3;
        blanc();
        x=fact();
        nr=(nr&&x);
    }
    return nr;
}
inline int E(){
    int nr,x;
    nr=term();
    while(v[poz]=='O'&&v[poz+1]=='R'){
        poz+=2;
        blanc();
        x=term();
        nr=(nr||x);
    }
    return nr;
}
inline void blanc(){
    while(poz<n&&v[poz]==' ')
        poz++;
}
int main(){
    FILE*fi,*fout;
    int t,i;
    char a;
    fi=fopen("bool.in" ,"r");
    fout=fopen("bool.out" ,"w");
    a=fgetc(fi);
    n=0;
    while(a!='\n'){
        v[n++]=a;
        a=fgetc(fi);
    }
    a=fgetc(fi);
    t=0;
    while(a>='0'&&a<='9'){
        t=t*10+a-'0';
        a=fgetc(fi);
    }
    a=fgetc(fi);
    for(i=0;i<t;i++){
        poz=0;
        blanc();
        vf[a-'A']=!vf[a-'A'];
        fprintf(fout,"%d" ,E());
        a=fgetc(fi);
    }
    fclose(fi);
    fclose(fout);
    return 0;
}