Cod sursa(job #84426)

Utilizator floringh06Florin Ghesu floringh06 Data 14 septembrie 2007 23:45:10
Problema Bool Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
using namespace std;
#include<fstream>

char pol[1005];
int np=0,cnt,v[30];

int is_alpha(char x)
{
return (x>='A'&&x<='Z');
}

int grad(char x)
{
if(x=='(') return 1;
if(x=='|') return 2;
if(x=='&') return 3;
return 4;
}

int eval()
{
if(pol[cnt]=='1'||pol[cnt]=='0')
  return (pol[cnt]-'0');
if(is_alpha(pol[cnt]))
  return v[pol[cnt]-'A'+1];

if(pol[cnt]=='&')
   {
   int a,b;
   cnt--;
   a=eval();
   cnt--;
   b=eval();
   return (a&b);
   }
if(pol[cnt]=='|')
   {
   int a,b;
   cnt--;
   a=eval();
   cnt--;
   b=eval();
   return (a|b);
   }
cnt--;
return (eval()^1);
}

int main()
{
ifstream fin("bool.in");
ofstream fout("bool.out");

char s[1005],oper[1005],x;
int no=0,i,n;
fin.get(s,1005);
fin.get();
oper[0]='(';
for(i=0;s[i];i++)
 if(s[i]!=' ')
 {
 if(s[i]=='(')
   oper[++no]='(';
 else
 if(s[i]==')')
    {
    while(oper[no]!='(')
       pol[++np]=oper[no--];
    no--;
    }
 else
    {
    if(is_alpha(s[i+1]))
       {
       if(s[i]=='N'||s[i]=='A'||s[i]=='O')
	 {
	 if(s[i]=='N')
	    x='!',i+=2;
	 else
	 if(s[i]=='A')
	    x='&',i+=2;
	 else
	    x='|',i++;

	 while(grad(oper[no])>grad(x))
	     pol[++np]=oper[no--];
	 oper[++no]=x;
	 }
       else
	 {
	 if(s[i]=='T')
	    pol[++np]='1',i+=3;
	 else
	    pol[++np]='0',i+=4;
	 }
       }
    else
       pol[++np]=s[i];
    }
 }
while(no)
  pol[++np]=oper[no--];

for(i=1;i<=26;i++) v[i]=0;
fin>>n;
fin.get();
fin.get(s,1005);
for(i=0;i<n;i++)
  {
  v[s[i]-'A'+1]^=1;
  cnt=np;
  fout<<eval();
  }

fin.close();
fout.close();
return 0;
}