Pagini recente » Cod sursa (job #1577012) | Cod sursa (job #2118812) | Cod sursa (job #7722) | Cod sursa (job #499224) | Cod sursa (job #2098884)
#include <iostream>
#include <fstream>
#include <map>
#include <cstring>
#define AND "AND"
#define OR "OR"
#define NOT "NOT"
#define TRUE "TRUE"
#define FALSE "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 changeLen;
fin >> changeLen;
char changes[105];
fin >> changes;
for (int iter = 0; iter < changeLen; iter++) {
char change = changes[iter];
if (changeMap.find(change) != changeMap.end())
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();
}
fin.close();
fout.close();
return 0;
}
void createMap() {
char *startP;
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() {
int sol = term();
while (strncmp(expIt, AND, strlen(AND)) == 0 or strncmp(expIt, OR, strlen(OR)) == 0) {
if (strncmp(expIt, AND, strlen(AND)) == 0) {
expIt += strlen(AND);
if (sol == term())
sol = 1;
else
sol = 0;
} else if (strncmp(expIt, OR, strlen(OR)) == 0) {
if (sol or term())
sol = 1;
else
sol = 0;
}
}
return sol;
}
int term() {
int tmp = factor();
while (strncmp(expIt, NOT, strlen(NOT)) == 0) {
expIt += strlen(NOT);
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, TRUE, strlen(TRUE)) == 0) {
tmp = 1;
expIt += strlen(TRUE);
} else if (strncmp(expIt, FALSE, strlen(FALSE)) == 0) {
tmp = 0;
expIt += strlen(FALSE);
}
}
}
return tmp;
}