Pagini recente » Cod sursa (job #1474818) | Cod sursa (job #2642201) | Cod sursa (job #2530232) | Cod sursa (job #966944) | Cod sursa (job #2869629)
#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(ok)
a = (!a);
if(ok1)
b = (!b);
if(operat == '&'){
if((a & b))
return '1';
else
return '0';
}
else{
if((a | b))
return '1';
else
return '0';
}
}
char special(char ch, bool ok)
{
int a;
if(isalpha(ch))
a = f[ch - 'A'];
else
a = ch - '0';
if(ok)
a = (!a);
if(a)
return '1';
else
return '0';
}
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();
if(op.empty() or op.top() == '('){
num.push(special(ch, ok));
break;
}
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((char)(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();
if(op.empty()){
num.push(special(ch, ok));
break;
}
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((char)(applyop(ch, ch1, ok, ok1, operat)));
}
return (num.top() - '0');
}
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;
}