Pagini recente » Cod sursa (job #1765978) | Cod sursa (job #2713074) | Cod sursa (job #1069593) | Cod sursa (job #1464370) | Cod sursa (job #2098689)
#include <iostream>
#include <fstream>
#include <map>
#include <cstring>
#define ANDD "AND"
#define ORR "OR"
#define NT "NOT"
#define TRU "TRUE"
#define FLS "FALSE"
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
char exp[1005];
char *expIt = exp;
map<char, int> changeMap;
int eval();
void createMap();
bool isLetter(char letter);
int term();
int factor();
int main() {
fin.getline(exp, 1000, '\r');
createMap();
int changes;
fin >> changes;
for (int iter = 0; iter < changes; iter++) {
char change;
fin >> change;
switch (exp[changeMap.at(change)]) {
case '0':
exp[changeMap.at(change)] = '1';
break;
case '1':
exp[changeMap.at(change)] = '0';
break;
default:
break;
}
expIt = exp;
fout << eval();
}
return 0;
}
void createMap() {
char *startP = expIt;
while (*expIt) {
startP = expIt;
while (isLetter(*expIt)) {
++expIt;
}
char copy = *expIt;
*expIt = '\0';
if (strlen(startP) == 1) {
changeMap[*startP] = startP - exp;
*startP = '0';
}
*expIt = copy;
++expIt;
}
}
bool isLetter(char letter) {
return letter >= 'A' and letter <= 'Z';
}
int eval() {
expIt = exp;
int sol = term();
while (strncmp(expIt, ANDD, strlen(ANDD)) == 0 or strncmp(expIt, ORR, strlen(ORR)) == 0) {
if (strncmp(expIt, ANDD, strlen(ANDD)) == 0) {
expIt += strlen(ANDD);
if (sol == term())
sol = 1;
else
sol = 0;
} else if (strncmp(expIt, ORR, strlen(ORR)) == 0) {
if (sol or term())
sol = 1;
else
sol = 0;
}
}
return sol;
}
int term() {
int tmp = factor();
while (strncmp(expIt, NT, strlen(NT)) == 0) {
tmp += strlen(NT);
tmp = !factor();
}
return tmp;
}
int factor() {
int tmp = 0;
if (*expIt == '(') {
++expIt;
tmp = eval();
++expIt;
} else {
if (*expIt == '0') {
tmp = 0;
expIt++;
} else if (*expIt == '1') {
tmp = 1;
expIt++;
} else {
if (strncmp(expIt, TRU, strlen(TRU)) == 0) {
tmp = 1;
expIt += strlen(TRU);
} else if (strncmp(expIt, FLS, strlen(FLS)) == 0) {
tmp = 0;
expIt += strlen(FLS);
}
}
}
return tmp;
}