Pagini recente » Cod sursa (job #1116748) | Cod sursa (job #1337386) | Cod sursa (job #1265963) | Cod sursa (job #2913876) | Cod sursa (job #2674178)
#include <iostream>
#include <fstream>
#include <stack>
#include <cstring>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
char s[1001], raws[1001], c;
bool a[30];
int lenght;
stack <bool> val;
stack <char> op;
void read()
{
fin.getline(raws, 1001);
lenght=strlen(raws);
}
void convert()
{
int k=0;
for(int i=0; i<lenght; i++)
{
if(raws[i]=='A' && raws[i+1]=='N' && raws[i+2]=='D')
{
s[k]='*';
k++;
i+=2;
continue;
}
if(raws[i]=='O' && raws[i+1]=='R')
{
s[k]='+';
k++;
i++;
continue;
}
if(raws[i]=='N' && raws[i+1]=='O' && raws[i+2]=='T')
{
s[k]='-';
k++;
i+=2;
continue;
}
if(raws[i]=='T' && raws[i+1]=='R' && raws[i+2]=='U' && raws[i+3]=='E')
{
s[k]='1';
k++;
i+=3;
continue;
}
if(raws[i]=='F' && raws[i+1]=='A' && raws[i+2]=='L' && raws[i+3]=='S' && raws[i+4]=='E')
{
s[k]='0';
k++;
i+=4;
continue;
}
if(raws[i]!=' ')
{
s[k]=raws[i];
k++;
}
}
}
void change_variable(char c)
{
int variable_id=c-'A';
if(a[variable_id]==0)
a[variable_id]=1;
else
a[variable_id]=0;
}
int check(char x)
{
if(x=='+')
return 1;
if(x=='*')
return 2;
if(x=='-')
return 3;
if(x=='(')
return -1;
}
int priority()
{
return check(c)-check(op.top());
}
void solve()
{
char o=op.top();
if(o=='(')
return;
bool t1=val.top();
op.pop();
val.pop();
if(o=='-')
{
val.push(!t1);
return;
}
bool t2=val.top();
val.pop();
if(o=='+')
{
if(t1==1 || t2==1)
val.push(1);
else
val.push(0);
return;
}
if(t1==1 && t2==1)
val.push(1);
else
val.push(0);
}
bool e()
{
for(int i=0; i<lenght; i++)
{
c=s[i];
if(c=='(')
{
op.push('(');
continue;
}
if(c>='A' && c<='Z')
{
val.push(a[c-'A']);
continue;
}
if(c=='0' || c=='1')
{
val.push(c-'0');
continue;
}
if(op.empty())
{
op.push(c);
continue;
}
if(c==')')
{
while(op.top()!='(')
solve();
op.pop();
continue;
}
if(priority()<0)
{
while(priority()<0)
solve();
continue;
}
op.push(c);
}
while(!op.empty())
solve();
return val.top();
}
int main()
{
int n;
char switched;
read();
convert();
lenght=strlen(s);
fin>>n;
for(int i=0; i<n; i++)
{
fin>>switched;
change_variable(switched);
//cout<<s<<'\n';
fout<<e();
}
return 0;
}