Pagini recente » Cod sursa (job #453769) | Cod sursa (job #100717) | Cod sursa (job #2069717) | Cod sursa (job #1185105) | Cod sursa (job #2869600)
#include <fstream>
#include <stack>
using namespace std;
ifstream cin("bool.in");
ofstream cout("bool.out");
string s;
string sir = "";
bool f[26];
char applyop(char ch, char ch1, bool ok, bool ok1, char operat)
{
int a, b;
if(isalpha(ch))
a = f[ch - 'A'];
else
a = ch - '0';
if(isalpha(ch1))
b = f[ch1 - 'A'];
else
b = ch1 - '0';
if(isalpha(ch) and ok)
a = (!a);
if(isalpha(ch1) and ok1)
b = (!b);
if(operat == '&')
return ((char)(a & b));
else
return ((char)(a | b));
}
bool evaluare()
{
int i, j;
stack <char> op;
stack <char> num;
for(i = 0; i < sir.size(); i++){
if(isalpha(sir[i]) or isdigit(sir[i]))
num.push(sir[i]);
else if(sir[i] == '(')
op.push('(');
else if(sir[i] == ')'){
while(!op.empty() and op.top() != '('){
int ok = false, ok1 = false;
if(!op.empty() and op.top() == '!'){
while(!op.empty() and op.top() == '!'){
ok ^= 1;
op.pop();
}
}
char ch = num.top();
num.pop();
char ch1 = num.top();
num.pop();
char operat = op.top();
op.pop();
if(!op.empty() and op.top() == '!'){
while(!op.empty() and op.top() == '!'){
ok1 ^= 1;
op.pop();
}
}
num.push(applyop(ch, ch1, ok, ok1, operat));
}
op.pop();
}
else
op.push(sir[i]);
}
while(!op.empty() and op.top() != '('){
int ok = false, ok1 = false;
if(!op.empty() and op.top() == '!'){
while(!op.empty() and op.top() == '!'){
ok ^= 1;
op.pop();
}
}
char ch = num.top();
num.pop();
char ch1 = num.top();
num.pop();
char operat = op.top();
op.pop();
if(!op.empty() and op.top() == '!'){
while(!op.empty() and op.top() == '!'){
ok1 ^= 1;
op.pop();
}
}
num.push(applyop(ch, ch1, ok, ok1, operat));
}
return num.top();
}
int main()
{
int len, i, j, n;
char ch;
i = 0;
cin >> s;
cin.get(ch);
for(j = 0; j < s.size(); j++){
if(s[j] == '(' or s[j] ==')')
sir += s[j];
else if(isalpha(s[j])){
string nousir = "";
while(isalpha(s[j]) and j < s.size()){
nousir += s[j];
j++;
}
if(nousir == "AND")
sir += '&';
else if(nousir == "OR")
sir += '|';
else if(nousir == "NOT")
sir += '!';
else if(nousir == "TRUE")
sir += '1';
else if(nousir == "FALSE")
sir += '0';
else if(nousir.size() == 1)
sir += nousir;
j--;
}
}
while(ch != '\n'){
cin >> s;
for(j = 0; j < s.size(); j++){
if(s[j] == '(' or s[j] ==')')
sir += s[j];
else if(isalpha(s[j])){
string nousir = "";
while(isalpha(s[j]) and j < s.size()){
nousir += s[j];
j++;
}
if(nousir == "AND")
sir += '&';
else if(nousir == "OR")
sir += '|';
else if(nousir == "NOT")
sir += '!';
else if(nousir == "TRUE")
sir += '1';
else if(nousir == "FALSE")
sir += '0';
else if(nousir.size() == 1)
sir += nousir;
j--;
}
}
cin.get(ch);
}
string schimbari;
cin >> n;
cin >> schimbari;
for(i = 0; i < n; i++){
f[schimbari[i] - 'A'] ^= 1;
cout << evaluare();
}
//cout << sir;
return 0;
}