Pagini recente » Borderou de evaluare (job #2943803) | Cod sursa (job #1362703)
#include <cstdio>
using namespace std;
const int MAXSIR = 1005;
const int MAXLITERE = 'Z' - 'A' + 2;
char sir[MAXSIR];
bool variabila[MAXLITERE];
void citire()
{
freopen("bool.in","r",stdin);
freopen("bool.out","w",stdout);
gets(sir);
}
int i = 0;
char termen[10];
int l_termen;
void det_termen()
{
l_termen = 0;
while('A' <= sir[i] && sir[i] <= 'Z')
{
termen[++l_termen] = sir[i];
++i;
}
}
const int AND = 1;
const int OR = 2;
void adaugare_termen(bool val, bool &rasp, bool &inceput, int idoperator, bool &nu)
{
if (nu)
val = !val;
if (!inceput)
{
inceput = true;
rasp = val;
}
else
{
if (idoperator == AND)
rasp = rasp && val;
if (idoperator == OR)
rasp = rasp || val;
}
nu = false;
}
bool expresie()
{
bool rasp;
bool inceput = false;
bool nu = false;
int idoperator = 0;
while(sir[i] != 0 && sir[i] != ')')
{
if ('A' <= sir[i] && sir[i] <= 'Z')
{
det_termen();
if (l_termen == 1)
adaugare_termen(variabila[termen[1] - 'A'],rasp,inceput,idoperator, nu);
if (l_termen == 2 && termen[1] == 'O' && termen[2] == 'R')
idoperator = OR;
if (l_termen == 3 && termen[1] == 'A' && termen[2] == 'N' && termen[3] == 'D')
idoperator = AND;
if (l_termen == 3 && termen[1] == 'N' && termen[2] == 'O' && termen[3] == 'T')
nu = true;
if (l_termen == 4 && termen[1] == 'T' && termen[2] == 'R' && termen[3] == 'U' && termen[4] == 'E')
adaugare_termen(true,rasp,inceput,idoperator,nu);
if (l_termen == 5 && termen[1] == 'F' && termen[2] == 'A' && termen[3] == 'L' && termen[4] == 'S' && termen[5] == 'E')
adaugare_termen(false,rasp,inceput,idoperator,nu);
}
if (sir[i] == '(')
{
++i;
adaugare_termen(expresie(),rasp,inceput,idoperator,nu);
}
if (sir[i] == ' ')
++i;
}
++i;
return rasp;
}
int main()
{
int n;
char c;
citire();
scanf("%d\n",&n);
for (int test = 1;test <= n;++test)
{
scanf("%c",&c);
variabila[c - 'A'] = !variabila[c - 'A'];
i = 0;
printf("%d",expresie()?1:0);
}
return 0;
}