Pagini recente » Cod sursa (job #1285905) | Cod sursa (job #737399) | Cod sursa (job #3240651) | Cod sursa (job #3032547) | Cod sursa (job #3231872)
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
char s[1005], t[1005];
int fr[105];
int n, i, k, q;
int expresieor();
int expresieand();
int expresiexor();
int factor();
int expresieor(){
int r = expresieand();
while(t[i] == '|'){
i++;
r = (r | expresieand());
}
return r;
}
int expresieand(){
int r = expresiexor();
while(t[i] == '&'){
i++;
r = (r & expresiexor());
}
return r;
}
int expresiexor(){
int r = factor();
while(t[i] == '^'){
i++;
r = (r ^ factor());
}
return r;
}
int factor(){
int r;
if(t[i] == '('){
i++;
r = expresieor();
i++;
}else{
if(t[i] == '1'){
i++;
return 1;
}
else if(t[i] == '0'){
i++;
return 0;
}
r = fr[t[i++] - 'A'];
}
return r;
}
int main(){
f.getline(s, 1005);
for(int i = 0; i < strlen(s); i++){
if(s[i] == ' ')continue;
if(s[i] == 'T' && s[i + 1] == 'R'){
t[k++] = '1';
i+=3;
continue;
}
if(s[i] == 'A' && s[i + 1] == 'N'){
t[k++] = '&';
i+=2;
continue;
}
if(s[i] == 'F' && s[i + 1] == 'A'){
t[k++] = '0';
i+=4;
continue;
}
if(s[i] == 'N' && s[i + 1] == 'O'){
t[k++] = '1';
t[k++] = '^';
i += 2;
continue;
}
if(s[i] == 'O' && s[i + 1] == 'R'){
t[k++] = '|';
i++;
continue;
}
t[k++] = s[i];
}
f >> q;
while(q--){
char c;
f >> c;
fr[c - 'A'] = 1 - fr[c - 'A'];
i = 0;
int sum = expresieor();
g << sum;
}
}