Cod sursa(job #245648)

Utilizator savimSerban Andrei Stan savim Data 18 ianuarie 2009 14:06:21
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <stdio.h>
#include <string.h>

#define MAX_L 1010

char s[MAX_L], str[MAX_L], *p;
int n, i, m, k, poz;

bool val[30];
bool rez;

bool termen();
bool factor();

bool expr() {
	bool rez = termen();
	
	while (*p == 'O' && *(p + 1) == 'R') {
		p += 3;
		rez = rez | termen();
	}
	
	return rez;
}

bool termen() {
	bool rez = factor();
	
	while (*p == 'A' && *(p + 1) == 'N') {
		p += 4;
		rez = rez & factor();
	}
	
	return rez;
}

bool factor() {
	bool rez;
	
	if (*p == '(') {
		//trec de paranteza
		p++;
		rez = expr();
		p ++;
	}
	else
		if (*p == 'N' && *(p + 1) == 'O') {
			//am not	
			p += 4;
			rez = ! factor();
		}
		else 
		 	if (*p == 'T' && *(p + 1) == 'R') {
				//am true		
				rez = 1;
				p += 5;
			}
			else 
				if (*p == 'F' && *(p + 1) == 'A') {
					//am false
					rez = 0;
					p += 6;
				}
				else {
					//am variabila
					rez = val[*p - 'A'];
					p += 2;
				}
	
	return rez;
}	

int main() {

	freopen("bool.in", "r", stdin);
	freopen("bool.out", "w", stdout);
	
	fgets(s, MAX_L, stdin);
	
	//am grija sa am exact 1 spatiu intre operanzi
	i = -1; n = strlen(s); m = -1;
	while (i < n) {
		i++;
		str[++m] = s[i];
		while (s[i] == s[i + 1] && s[i + 1] == ' ' && i < n) i++;
	}
	str[++m] = '\0';

	strcpy(s, str);
	
	//rezolv expresiile
	scanf("%d", &n);
	scanf("%s", str);
	for (i = 0; i < n; i++) {
		val[str[i] - 'A'] = 1 - val[str[i] - 'A'];
		p = s;
		rez = expr();
		printf("%d", rez);
	}
	printf("\n");
	
	return 0;
}