Pagini recente » Cod sursa (job #557078) | Cod sursa (job #1386808) | Cod sursa (job #1938178) | Cod sursa (job #2022902) | Cod sursa (job #2833233)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
char s[1001];
int p=0;
bool EvalTermen();
bool EvalFactor();
bool EvalExpresie()
{
bool vala=EvalTermen();
while(s[p]=='|')
{
p++;
bool valb=EvalTermen();
vala|=valb;
}
return vala;
}
bool EvalTermen()
{
bool vala=EvalFactor();
while(s[p]=='&')
{
p++;
bool valb=EvalFactor();
vala&=valb;
}
return vala;
}
bool EvalFactor()
{
bool semn=0;
while(s[p]=='!')
{
semn=!semn;
p++;
}
if(s[p]=='(')
{
p++;
bool val=EvalExpresie();
p++;
if(semn==1)
return !val;
else
return val;
}
bool val=0;
if(s[p]=='0')
val=0;
if(s[p]=='1')
val=1;
p++;
if(semn==1)
return !val;
else
return val;
}
int main()
{
in.getline(s,1001);
int lungime=strlen(s);
int i=0;
while(i<lungime)
{
if(s[i]==' ')
{
for(int j=i+1;j<=lungime;j++)
s[j-1]=s[j];
lungime--;
}
else
i++;
}
char aux[1001];
char s1[]="AND";
char s2[]="&";
char *pp=strstr(s,s1);
while(pp!=NULL)
{
strcpy(aux,pp+strlen(s1));
strcpy(pp,s2);
strcat(s,aux);
pp=strstr(pp+strlen(s2),s1);
}
char s3[]="OR";
char s4[]="|";
char *p1=strstr(s,s3);
while(p1!=NULL)
{
strcpy(aux,p1+strlen(s3));
strcpy(p1,s4);
strcat(s,aux);
p1=strstr(p1+strlen(s4),s3);
}
char s5[]="NOT";
char s6[]="!";
char *p2=strstr(s,s5);
while(p2!=NULL)
{
strcpy(aux,p2+strlen(s5));
strcpy(p2,s6);
strcat(s,aux);
p2=strstr(p2+strlen(s6),s5);
}
char s7[]="TRUE";
char s8[]="1";
char *p3=strstr(s,s7);
while(p3!=NULL)
{
strcpy(aux,p3+strlen(s7));
strcpy(p3,s8);
strcat(s,aux);
p3=strstr(p3+strlen(s8),s7);
}
char s9[]="FALSE";
char s10[]="0";
char *p4=strstr(s,s9);
while(p4!=NULL)
{
strcpy(aux,p4+strlen(s9));
strcpy(p4,s10);
strcat(s,aux);
p4=strstr(p4+strlen(s10),s9);
}
int modif;
in>>modif;
int f[100];
for(int i=0;i<=strlen(s);i++)
{
if(s[i]>='A' && s[i]<='Z')
{
f[s[i]-'A']=i;
s[i]='0';
}
//cout<<s[i];
}
for(int i=1;i<=modif;i++)
{
char ch;
in>>ch;
int indice=ch-'A';
if(s[f[indice]]=='0')
s[f[indice]]='1';
else
s[f[indice]]='0';
out<<EvalExpresie();
p=0;
}
return 0;
}