Pagini recente » Cod sursa (job #3205980) | Cod sursa (job #942502)
Cod sursa(job #942502)
#include <fstream>
#include <iostream>
using namespace std;
ifstream fi("bool.in");
ofstream fo("bool.out");
string s;
bool a[30];
int p,n,i;
char ch;
bool eval();
bool termen();
bool factor();
int v(char ch){ return ((int)ch-65);}
bool eval(){
int r=termen(); //cout << " int_e r=" << r << ' ' << "p=" << p << endl;
if ((s[p]=='O')&&(s[p+1]=='R')) p+=3,r|=factor();
return r;
}
bool termen(){
bool r=factor(); //cout << " in_t r=" << r << ' ' << "p=" << p << endl;
if ((s[p]=='A')&&(s[p+1]=='N')) p+=4,r&=factor();
return r;
}
bool factor(){
bool r,aux=1;
if ((s[p]=='N')&&(s[p+1]=='O')) aux=0,p+=4;
if ((s[p]=='T')&&(s[p+1]=='R')) r=1,p+=5; else
if ((s[p]=='F')&&(s[p+1]=='A')) r=0,p+=5; else
if (s[p]=='(') {++p; r=eval(); ++p;} else p+=2,r=aux?a[v(s[p])]:!a[v(s[p])];
return r;
}
int main(){
// A-65 Z-90
getline(fi,s);
fi >> n;
while (n--){
fi >> ch;
a[v(ch)]=!a[v(ch)];
//for (i=0; i<26; i++) cout << a[i] << ' '; cout << endl;
//cout << s << endl;
p=0;
fo << eval();
}
return 0;
}