Cod sursa(job #195375)

Utilizator Matei14Popa-Matei Mihai Matei14 Data 18 iunie 2008 08:45:46
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 1005
int n,k,m;
char s[N],t[N];
int v[N];
inline int shas(char ch){
	return (ch>='A' && ch<='Z');
}
void crisis(){
	int k=0,x=strlen(s);
	m=0;
	while (k<x)
		if (shas(s[k])){
			if (!shas(s[k+1]))
				t[++m]=s[k++];
			else{
				switch(s[k]){
					case 'A':t[++m]='&';k+=3;break;
					case 'F':t[++m]='0';k+=5;break;
					case 'N':t[++m]='!';k+=3;break;
					case 'O':t[++m]='|';k+=2;break;
					case 'T':t[++m]='1';k+=4;break;
				}
			}
		}
		else
			if (s[k]=='(' || s[k]==')')
				t[++m]=s[k++];
		else ++k;
}
int term();
int fact();
int solv(){
    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=solv();
			k++;
		}
	else
		if (shas(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);
    crisis();
    scanf("%d\n",&n);
    memset(v,0,sizeof(v));
    while (n--){
		scanf("%c",&ch);
		v[ch]=1-v[ch];
		k=1;
		printf("%d",solv()>0?1:0);
	}
	fclose(stdin);
	fclose(stdout);
    return 0;
}