Pagini recente » Cod sursa (job #144298) | Cod sursa (job #1852268) | Cod sursa (job #334487) | Cod sursa (job #1340747) | Cod sursa (job #3192733)
#include <fstream>
#include <cstring>
#include <ctype.h>
#include <map>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
char s[1005];
char* p;
int pos, n;
char ch;
map < char, bool > val;
/// recursivitate indirecta - declaram functiile inainte
bool op_or(); /// se ocupa de OR
bool op_and();/// se ocupa de AND
bool term(); /// se ocupa de (), termeni, not, true, false
void format() {
p = strstr(s, "TRUE");
while (p != NULL) {
strcpy(p + 1, p + 4);
(*p) = '1';
p = strstr(p, "TRUE");
}
p = strstr(s, "FALSE");
while (p != NULL) {
strcpy(p + 1, p + 5);
(*p) = '0';
p = strstr(p, "FALSE");
}
p = strstr(s, "AND");
while (p != NULL) {
strcpy(p + 1, p + 3);
(*p) = '&';
p = strstr(p, "AND");
}
p = strstr(s, "OR");
while (p != NULL) {
strcpy(p + 1, p + 2);
(*p) = '|';
p = strstr(p, "OR");
}
p = strstr(s, "NOT");
while (p != NULL) {
strcpy(p + 1, p + 3);
(*p) = '!';
p = strstr(p, "NOT");
}
p = strstr(s, " ");
while (p != NULL) {
strcpy(p, p + 1);
p = strstr(p, " ");
}
}
bool op_or() {
bool res = op_and();
while (s[pos] == '|') {
pos++;
bool t = op_and();
res |= t;
}
return res;
}
bool op_and() {
bool res = term();
while (s[pos] == '&') {
pos++;
bool t = term();
res &= t;
}
return res;
}
bool term() {
bool res;
if (s[pos] == '(') {
pos++;
bool t = op_or();
res = t;
pos++;
}
else if (s[pos] == '!') {
pos++;
bool t = term();
res = !t;
}
else if (s[pos] == '1') {
pos++;
res = 1;
}
else if (s[pos] == '0') {
pos++;
res = 0;
}
else {
res = val[s[pos]];
pos++;
}
return res;
}
int main() {
f.getline(s, 1000);
for (char x = 'A'; x <= 'Z'; x++) val[x] = 0;
format();
f >> n;
for (int i = 1; i <= n; i++) {
f >> ch;
val[ch] = !val[ch];
pos = 0;
g << op_or();
}
return 0;
}