Pagini recente » Cod sursa (job #3228496) | Cod sursa (job #3272475) | Cod sursa (job #2266256) | Cod sursa (job #863811) | Cod sursa (job #2520162)
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
string str;
char chr;
bool init, flag;
bool var[30];
bool nec[1000];
bool both[1000];
int nrnr, poz;
void atrb(bool* val, bool* neg) {
if (*neg == true) {
init = !init;
}
*val = init;
*neg = false;
}
bool dec(bool* ininec, bool* iniboth, int cntn, int cntb) {
for (int index = 0; index < cntn; index++, (ininec)++) {
nec[index] = *ininec;
}
for (int index = 0; index < cntb; index++, (iniboth)++) {
both[index] = *iniboth;
}
for (int index = cntb - 1; index >= 0; index--) {
if (both[index] == true) {
nec[index] = nec[index] && nec[index + 1];
nec[index + 1] = false;
}
}
flag = false;
for (int index = 0; index < cntn; index++) {
if (nec[index] == true) {
flag = true;
break;
}
}
return flag;
}
bool decide(string str) {
int cnt = 0;
int brak = 0;
bool neg = false;
bool nec[1000];
int cntn = 0;
bool both[1000];
int cntb = 0;
string sti;
while ((unsigned int) cnt < str.size()) {
sti = str[cnt];
cnt++;
init = false;
if (sti == "(") {
sti = "";
brak = 1;
if (str[cnt] == '(') {
brak++;
}
if (str[cnt] == ')') {
brak--;
}
while (brak > 0) {
sti += str[cnt];
cnt++;
if (str[cnt] == '(') {
brak++;
}
if (str[cnt] == ')') {
brak--;
}
}
init = decide(sti);
atrb(&nec[cntn], &neg);
cntn++;
}
while (str[cnt] != ' ' && (unsigned)cnt < str.size()) {
sti += str[cnt];
cnt++;
}
if (sti.size() == 1) {
chr = sti[0];
init = var[chr - 'A'];
atrb(&nec[cntn], &neg);
cntn++;
}
if (sti == "TRUE") {
init = true;
atrb(&nec[cntn], &neg);
cntn++;
}
if (sti == "FALSE") {
init = false;
atrb(&nec[cntn], &neg);
cntn++;
}
if (sti == "NOT") {
neg = !neg;
}
if (sti == "AND") {
both[cntb] = true;
cntb++;
}
if (sti == "OR") {
both[cntb] = false;
cntb++;
}
cnt++;
}
return dec(&nec[0], &both[0], cntn, cntb);
}
int main()
{
fin.get(chr);
while (chr != '\n') {
str += chr;
fin.get(chr);
}
fin >> nrnr;
for (int index = 0; index < nrnr; index++) {
fin >> chr;
var[chr - 'A'] = !var[chr - 'A'];
if (decide(str)) {
fout << "1";
}
else {
fout << "0";
}
}
return 0;
}