Cod sursa(job #572837)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 5 aprilie 2011 17:47:06
Problema Bool Scor 70
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.75 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,p3:longint;
begin
  pp:=0; p1:=0; p2:=0; p3:=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 p1=0 then if a[i]='NOT' then p1:=i;
      if a[i]='AND' then p2:=i;
      if a[i]='OR'  then p3:=i;
    end;
  if (p3=0)and(p2=0)and(p1>0) then eval:=not eval(p1+1,right) else
  if p3>p2 then
    begin
      if p1=p3+1 then
      eval:=eval(left,p3-1) or not eval(p1+1,right) else
      eval:=eval(left,p3-1) or eval(p3+1,right);
    end else
  if p2>0 then
    begin
      if p1=p2+1 then
      eval:=eval(left,p2-1) and not eval(p1+1,right) else
      eval:=eval(left,p2-1) and eval(p2+1,right);
    end else
  if a[left]='(' then eval:=eval(left+1,right-1) 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.