Pagini recente » Cod sursa (job #1840029) | Cod sursa (job #2412467) | Cod sursa (job #2654872) | Cod sursa (job #2185203) | Cod sursa (job #1064653)
#include<fstream>
#include<cstring>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
int n,i,ap[256];
char c,*p,aux[1005],s[1005];
void prel()
{
int j;
p=strstr(s," AND");
while (p)
{
strcpy(aux,p+5);
p[0]='&', p[1]=0;
strcat(s,aux);
p=strstr(s," AND");
}
p=strstr(s," OR");
while (p)
{
strcpy(aux,p+4);
p[0]='|', p[1]=0;
strcat(s,aux);
p=strstr(s," OR");
}
p=strstr(s,"NOT");
while (p)
{
strcpy(aux,p+4);
p[0]='1', p[1]='^', p[2]=0;
strcat(s,aux);
p=strstr(s,"NOT");
}
p=strstr(s,"TRUE");
while (p)
{
strcpy(aux,p+4);
p[0]='1', p[1]=0;
strcat(s,aux);
p=strstr(s,"TRUE");
}
p=strstr(s,"FALSE");
while (p)
{
strcpy(aux,p+5);
p[0]='0', p[1]=0;
strcat(s,aux);
p=strstr(s,"FALSE");
}
}
int expresie();
int opor();
int opand();
int opxor();
int main()
{
int j;
f.getline(s,1005);
f>>n;
prel(); f.get();
for (j=0;j<n;++j)
{
f.get(c);
i=0, ap[c]=1-ap[c];
g<<expresie();
}
g<<'\n';
return 0;
}
int expresie()
{
int r=opor();
while (s[i]=='|')
{
++i;
r|=opor();
}
return r;
}
int opor()
{
int r=opand();
while (s[i]=='&')
{
++i;
r&=opand();
}
return r;
}
int opand()
{
int r=opxor();
while (s[i]=='^')
{
++i;
r^=opxor();
}
return r;
}
int opxor()
{
int r=0;
if (s[i]=='(')
{
++i;
r=expresie();
++i;
}
else
{
if (s[i]=='0' || s[i]=='1')
{
r=s[i]-'0';
++i;
}
else if ('A'<=s[i] && s[i]<='Z')
{
r=ap[s[i]];
++i;
}
}
return r;
}