Pagini recente » Cod sursa (job #1095436) | Cod sursa (job #586910) | Cod sursa (job #3001128) | Cod sursa (job #57243) | Cod sursa (job #2696470)
#include <fstream>
#include <string>
#define DIM 1005
#define DN 105
using namespace std;
ifstream fi("bool.in");
ofstream fo("bool.out");
bool val[DN];
char expr[DIM], *p=expr;
bool f1();
bool f2();
bool f3();
bool f4();
int main()
{
fi.getline(expr, DIM);
int n;
fi >> n;
char c;
while (n--) {
fi >> c;
val[c-'A'] = !val[c-'A'];
p=expr;
fo << f1();
}
return 0;
}
bool f1() //OR
{
bool ans = f2();
while (*(p+3) != 0 && *p == ' ' && *(p+1) == 'O' && *(p+2) == 'R' && *(p+3) == ' ') {
p += 4;
ans |= f2();
}
return ans;
}
bool f2() //AND
{
bool ans = f3();
while (*(p+4)!=0 && *p == ' ' && *(p+1) == 'A' && *(p+2) == 'N' && *(p+3) == 'D' && *(p+4) == ' ') {
p += 5;
ans &= f3();
}
return ans;
}
bool f3() //NOT
{
bool ans;
if (*(p+3)!=0 && *p == 'N' && *(p+1) == 'O' && *(p+2) == 'T' && *(p+3) == ' ') {
p += 4;
ans = !f3();
}
else {
ans = f4();
}
return ans;
}
bool f4() //VALOARE (paranteza, litera, TRUE, FALSE)
{
bool ans;
if (*p == '(') {
p++;
ans = f1();
p++;
}
else if (*p == 'T' && *(p+1) == 'R' && *(p+2) == 'U' && *(p+3) == 'E') {
ans = 1;
p += 4;
}
else if (*p == 'F' && *(p+1) == 'A' && *(p+2) == 'L' && *(p+3) == 'S' && *(p+4) == 'E') {
ans = 0;
p += 5;
}
else {
ans = val[*p-'A'];
p++;
}
return ans;
}