Cod sursa(job #189046)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 11 mai 2008 18:07:32
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.82 kb
#include <cstdio>
#include <cstring>
int n,k,m;
char s[1002],t[1002];
int v[200];
int elit(char ch){
     return (ch>='A' && ch<='Z');
     }
void naturalizeaza(){ //A AND (B OR (C AND !D) AND FALSE) => A&(B|(C&!D)&0)    
 int k=0;
 m=0;
 while (k<strlen(s))
       if (elit(s[k])){
         if (!elit(s[k+1])) t[++m]=s[k++];
         else{
           switch(s[k]){
            case 'A':t[++m]='&';k+=3;break;//AND
            case 'F':t[++m]='0';k+=5;break;//FALSE
            case 'N':t[++m]='!';k+=3;break;//NOT
            case 'O':t[++m]='|';k+=2;break;//OR
            case 'T':t[++m]='1';k+=4;break;//TRUE
                       }
             }
                     }
          else       
       if (s[k]=='(' || s[k]==')')
          t[++m]=s[k++];
           else ++k;
}
int term();
int fact();
int eval(){
    int aux=term();
    while (t[k]=='|' && k<=m) {
          k++;
          aux|=term();}
    return aux;
    }
int term(){
     int aux=fact();
     while (t[k]=='&' && k<=m){
           k++; 
           aux&=fact();}
     return aux;
     }
int fact(){
     int aux;
     if (t[k]=='1' || t[k]=='0'){
        if (t[k]=='0') aux=0;
                  else aux=1;
        k++;}
     else if (t[k]=='('){
          k++;
          aux=eval();
          k++;}
     else if (elit(t[k])){
          aux=v[t[k]];
          k++;}
     else if (t[k]=='!'){
          k++;
          aux=!fact();}
     return aux;
     } 
int main(){
    char ch;
    freopen("bool.in","r",stdin);
    freopen("bool.out","w",stdout);
    gets(s);
    naturalizeaza();
    scanf("%d\n",&n);
    memset(v,0,sizeof(v));
    while (n--){
          scanf("%c",&ch);
          v[ch]=1-v[ch];
          k=1;
          printf("%d",eval()>0?1:0);
          }
    return 0;
}