Cod sursa(job #3141539)

Utilizator daristyleBejan Darius-Ramon daristyle Data 14 iulie 2023 14:02:23
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <fstream>

using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");

const int STRLEN_MAX = 1e3;
const int SIGMA = 26;

bool value[SIGMA]{};
char s[STRLEN_MAX + 1];
int sIndex;

void RewriteBooleanExpression(char s[]) {
	int i = 0, j = 0;
	while(s[j])
		if(s[j] == 'A' && s[j + 1] == 'N'){
			s[i++] = '&';
			j += 3;
		}else if(s[j] == 'O' && s[j + 1] == 'R'){
			s[i++] = '|';
			j += 2;
		}else if(s[j] == 'N' && s[j + 1] == 'O'){
			s[i++] = '!';
			j += 3;
		}else if(isspace(s[j]))
			++j;
		else
			s[i++] = s[j++];
	s[i] = '\0';
}

bool OR();

bool factor() {
	bool ret = false;
	if(isupper(s[sIndex])){
		if(!isupper(s[sIndex + 1]))
			ret = value[s[sIndex] - 'A'];
		else if(s[sIndex] == 'T')
			ret = true;

		do
			++sIndex;
		while(isupper(s[sIndex]));
	}else if(s[sIndex] == '!'){
		++sIndex;
		ret = !factor();
	}else if(s[sIndex] == '('){
		++sIndex;
		ret = OR();
		++sIndex;
	}

	return ret;
}

bool AND() {
	bool ret = factor();
	bool val;
	while(s[sIndex] == '&'){
		++sIndex;
		val = factor();
		ret = (ret && val);
	}

	return ret;
}

bool OR() {
	bool ret = AND();
	bool val;
	while(s[sIndex] == '|'){
		++sIndex;
		val = AND();
		ret = (ret || val);
	}

	return ret;
}


int main() {
	fin.getline(s, STRLEN_MAX);
	RewriteBooleanExpression(s);

	int n;
	fin >> n;
	fin.get();
	for(int i = 0; i < n; ++i){
		char ch;
		ch = fin.get();
		ch -= 'A';
		value[ch] = !value[ch];

		sIndex = 0;
		fout.put(OR() + '0');
	}
	fout.put('\n');

	fin.close();
	fout.close();
	return 0;
}