Cod sursa(job #3192792)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 13 ianuarie 2024 11:16:33
Problema Bool Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <fstream>
#include <cstring>
#include <ctype.h>
#include <map>
using namespace std;

ifstream f("bool.in");
ofstream g("bool.out");

char s[1005];
char* p;
int pos, n;
char ch;
map < char, bool > val;

/// recursivitate indirecta - declaram functiile inainte
bool op_or(); /// se ocupa de OR
bool op_and();/// se ocupa de AND
bool term(); /// se ocupa de (), termeni, not, true, false

void format() {
	p = strstr(s, "TRUE");
	while (p != NULL) {
		strcpy(p + 1, p + 4);
		(*p) = '1';
		p = strstr(p, "TRUE");
	}
	p = strstr(s, "FALSE");
	while (p != NULL) {
		strcpy(p + 1, p + 5);
		(*p) = '0';
		p = strstr(p, "FALSE");
	}
	p = strstr(s, "AND");
	while (p != NULL) {
		strcpy(p + 1, p + 3);
		(*p) = '&';
		p = strstr(p, "AND");
	}
	p = strstr(s, "OR");
	while (p != NULL) {
		strcpy(p + 1, p + 2);
		(*p) = '|';
		p = strstr(p, "OR");
	}
	p = strstr(s, "NOT");
	while (p != NULL) {
		strcpy(p + 1, p + 3);
		(*p) = '!';
		p = strstr(p, "NOT");
	}
	p = strstr(s, " ");
	while (p != NULL) {
		strcpy(p, p + 1);
		p = strstr(p, " ");
	}
}

bool op_or() {
	bool res = op_and();
	while (s[pos] == '|') {
		pos++;
		bool t = op_and();
		res = res || t;
	}
	return res;
}

bool op_and() {
	bool res = term();
	while (s[pos] == '&') {
		pos++;
		bool t = term();
		res = res && t;
	}
	return res;
}

bool term() {
	bool res = 1;
	if (s[pos] == '(') {
		pos++;
		bool t = op_or();
		res = t;
		pos++;
	}
	else if (s[pos] == '!') {
		pos++;
		bool t = term();
		res = !t;
	}
	else if (s[pos] == '1') {
		pos++;
		res = 1;
	}
	else if (s[pos] == '0') {
		pos++;
		res = 0;
	}
	else {
		res = val[s[pos]];
		pos++;
	}
	return res;
}

int main() {
	f.getline(s, 1005);
	for (char x = 'A'; x <= 'Z'; x++) val[x] = 0;
	format();
	//g << s << '\n';
	f >> n;
	for (int i = 1; i <= n; i++) {
		f >> ch;
		val[ch] = !val[ch];
		pos = 0;
		g << op_or();
	}
	return 0;
}