Cod sursa(job #1276735)

Utilizator LegionHagiu Stefan Legion Data 26 noiembrie 2014 19:46:31
Problema Bool Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
bool litere[40];
int n;
string s;
bool rez(int& i);
bool datermen(int& i)
{
	bool tot;
	bool invers = false;
	if (s[i] == 'N'&&s[i + 1] == 'O')
	{
		i += 4;
		invers = true;
	}
	if (s[i] == '(')
	{
		i++;
		tot = rez(i);
	}
	if (i+1<n&&(s[i + 1] == ' ' || s[i + 1] == ')'))
	{
		tot = litere[s[i] - 65];
		i++;
	}
	if (i+1<n&&s[i] == 'T'&&s[i + 1] == 'R')
	{
		i += 4;
		tot = true;
	}
	if (i+1<n&&s[i] == 'F'&&s[i + 1] == 'A')
	{
		i += 5;
		tot = false;
	}
	if (invers)
	{
		return !tot;
	}
	else
	{
		return tot;
	}
}
bool termen(int& i)
{
	bool tot = 1;
	tot = datermen(i);
	if (i<n&&s[i] == ' '){ i++; }
	while (i<n&&(s[i] != 'O' || (i+1<n&&s[i + 1] != 'R')) && s[i] != ')')
	{
		if (s[i] == 'A'&&s[i + 1] == 'N')
		{
			i += 4;
			if (tot == 1)
				tot = datermen(i);
			else
				datermen(i);
			if (i<n&&s[i] == ' '){ i++; }
		}
	}
	return tot;
}
bool expresie(int& i)
{
	bool tot = 0;
	tot = termen(i);
	while (i < n && s[i] != ')')
	{
		if (s[i] == 'O'&&s[i + 1] == 'R')
		{
			i += 3;
			if (tot == 0)
				tot = termen(i);
			else
				termen(i);
		}
	}
	i++;
	return tot;
}
bool rez(int& i)
{
	bool tot;
	tot = expresie(i);
	return tot;
}
int main()
{
	ifstream in("bool.in");
	ofstream out("bool.out");
	int f, i, j;
	char c;
	getline(in, s);
	n = s.size();
	in >> f;
	for (i = 1; i <= f; i++)
	{
		j = 0;
		in >> c;
		litere[c - 65] = !litere[c - 65];
		out << rez(j);
	}
}