Cod sursa(job #589809)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 13 mai 2011 20:49:24
Problema Bool Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.48 kb
type    sir=set of 'A'..'Z';
var     a:array[1..1000] of string;
        m:array[1..100] of boolean;
        c:char;
        v:sir;
        n,i,j,poz:longint;
        f,g:text;

function eval(left, right:longint):boolean;
var i,pp,p1,p2:longint;
begin
  pp:=0; p1:=0; p2:=0;
  for i:=left to right do
  if a[i]='(' then inc(pp) else
  if a[i]=')' then dec(pp) else
  if pp=0 then
    begin
      if a[i]='OR' then p1:=i;
      if a[i]='AND'  then p2:=i;
    end;
  if p1>0 then eval:=eval(left,p1-1) or eval(p1+1,right) else
  if p2>0 then eval:=eval(left,p2-1) and eval(p2+1,right) else
  if a[left]='(' then eval:=eval(left+1,right-1) else
  if a[left]='NOT' then eval:=not eval(left+1,right) else
  if a[left]='TRUE' then eval:=true else
  if a[left]='FALSE' then eval:=false else
  eval:=m[ord(a[left][1])-64];
end;

begin
  assign(f,'bool.in');
  assign(g,'bool.out');
  reset(f);
  rewrite(g);
  poz:=1;
  while not eoln(f) do
    begin
      read(f,c);
      if c=' ' then inc(poz) else
      if c='(' then begin a[poz]:=c; inc(poz); end else
      if c=')' then begin inc(poz); a[poz]:=c; end
      else a[poz]:=a[poz]+c;
    end;
  for i:=1 to poz do
    begin
      if length(a[i])=1 then c:=a[i][1];
      if c in v then
        m[ord(c)-64]:=false;
    end;
  readln(f,n);
  for i:=1 to n do
    begin
      read(f,c);
      m[ord(c)-64]:=not m[ord(c)-64];
      if eval(1,poz) then write(g,'1') else write(g,'0');
    end;
  close(g);
end.