Cod sursa(job #360297)

Utilizator ProtomanAndrei Purice Protoman Data 30 octombrie 2009 21:16:44
Problema Bool Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <algorithm>
#include <stdio.h>
#include <locale>

#define MAX 1024
#define lvlMAX 2

using namespace std;

char oper[4][4] = {"|", "&", "", ""};
int mas, sch; 
char *expr;
int val[MAX];
char strExpr[MAX], strInit[MAX], strSch[MAX];

inline int calc(int x, int y, int oper)
{
	if (oper == '&')
		return x & y;
	if (oper == '|')
		return x | y;
}

inline int eval(int lvl)
{
	int tot = 0, ac = 0, not = 0;

	if (lvl == lvlMAX)
	{
		if (*expr == '!')
			not = 1, expr++;
		if (*expr == '(')
		{
			expr++;
			tot = eval(0);
			expr++;
		}
		else
		{
			tot = val[*expr];
			expr++;
		}

		tot ^= not;
	}
	else for (tot = eval(lvl + 1); strchr(oper[lvl], *expr) && *expr; tot = ac)
		ac = calc(tot, eval(lvl + 1), *expr++);

	return tot;
}

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

	fgets(strInit + 1, MAX, stdin);
	int n = strlen(strInit + 1);

	scanf("%d\n", &sch);

	fgets(strSch + 1, MAX, stdin);

	val['1'] = 1;
	for (int i = 1; i <= n; i++)
		if (strInit[i] == 'T' && strInit[i + 1] == 'R')
			strExpr[mas++] = '1', i += 3;
		else if (strInit[i] == 'F' && strInit[i + 1] == 'A')
			strExpr[mas++] = '0', i += 4;
		else if (strInit[i] == 'N' && strInit[i + 1] == 'O')
			strExpr[mas++] = '!', i += 2;
		else if (strInit[i] == 'A' && strInit[i + 1] == 'N')
			strExpr[mas++] = '&', i += 2;
		else if (strInit[i] == 'O' && strInit[i + 1] == 'R')
			strExpr[mas++] = '|', i += 1;
		else if (isalnum(strInit[i]) || strInit[i] == ')' || strInit[i] == '(')
			strExpr[mas++] = strInit[i];
	
	for (int i = 1; i <= sch; i++)
	{
		val[strSch[i]] ^= 1;

		expr = strExpr;
		printf("%d", eval(0));
	}

	fclose(stdin);
	fclose(stdout);
	return 0;
}