Cod sursa(job #2165839)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 13 martie 2018 13:57:23
Problema Bool Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fi("bool.in");
ofstream fo("bool.out");


bool values[256];

string str, fliper;
int ptr, n;


static bool expression();
static bool priority_1();
static bool num();

static void advance() {
	while (str[ptr] == ' ')
		ptr+= 1; }

static bool expression() {
	bool ant = priority_1();

	advance();
	while (str[ptr] == 'O' && str[ptr + 1] == 'R') {
		ptr+= 3; 
		ant|= priority_1(); }
	ptr+= 1;

	return ant; }

static bool priority_1() {
	bool ant = num();

	advance();
	while (str[ptr] == 'A' && str[ptr + 1] == 'N') {
		ptr+= 3;
		ant&= num(); }

	return ant; }

static bool num() {
	bool ant, flip = false;

	advance();
	if (str[ptr] == '(') {
		ptr+= 1;
		return expression(); }

	while (str[ptr] == 'N' && str[ptr + 1] == 'O') {
		flip^= 1;
		ptr+= 3; }

	if (str[ptr] == 'F' && str[ptr + 1] == 'A') {
		ptr+= 5;
		return false ^ flip; }
	if (str[ptr] == 'T' && str[ptr + 1] == 'R') {
		ptr+= 4;
		return true ^ flip; }

	return values[str[ptr]] ^ flip; }

int main() {
	getline(fi, str);
	fi >> n >> fliper;

	for (auto ch: fliper) {
		values[ch]^= 1;
		ptr = 0;
		fo << int(expression()); }
	fo << endl;

	return 0; }