Cod sursa(job #49752)

Utilizator fogabFodor Gabor fogab Data 6 aprilie 2007 12:51:12
Problema Bool Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.41 kb
var f,f2:text;
    a:array[1..1100] of char;
    b:array['A'..'Z'] of boolean;
    c:char;
    i,n,j:word;
    ok:boolean;

function go(var i:word):boolean;
var k,k2:boolean;
    op,ops:byte;
begin
k:=false;
op:=2;
ops:=0;
while (a[i]<>#0) and (a[i]<>')') do
  begin
  if a[i]=' ' then inc(i) else
  if a[i]='(' then begin
                   inc(i);
                   k2:=go(i);
                   if ops=1 then if op=1 then k:=k and not(k2)
                                  else k:=k or not(k2)
                     else if op=1 then k:=k and k2
                                  else k:=k or k2;
                   ops:=0;
                   end
     else if (a[i]='T') and (a[i+1]='R') then
             begin
             if ops=1 then if op=1 then k:=false
                      else if op=2 then k:=true;
             i:=i+4;
             ops:=0;
             end
     else if (a[i]='F') and (a[i+1]='A') then
             begin
             if ops=1 then if op=2 then k:=true
                      else if op=1 then k:=false;
             i:=i+5;
             ops:=0;
             end
     else if (a[i]='O') and (a[i+1]='R') then begin
                                            op:=2;
                                            i:=i+2;
                                            end
     else if (a[i]='A') and (a[i+1]='N') then begin
                                            op:=1;
                                            i:=i+3;
                                            end
     else if (a[i]='N') and (a[i+1]='O') then begin
                                            ops:=(ops+1) mod 2;
                                            i:=i+3;
                                            end
     else begin
            if ops=1 then if op=1 then k:=k and not(b[a[i]])
                                  else k:=k or not(b[a[i]])
                     else if op=1 then k:=k and b[a[i]]
                                  else k:=k or b[a[i]];
             i:=i+1;
             ops:=0;
           end
  end;
go:=k;
end;

begin
assign(f,'bool.in');
reset(f);
while not(eoln(f)) do
  begin
  inc(i);
  read(f,a[i]);
  end;
readln(f);
assign(f2,'bool.out');
rewrite(f2);
readln(f,n);
a[i+1]:=#0;
for i:=1 to n do
  begin
  read(f,c);
  b[c]:=not(b[c]);
  j:=1;
  ok:=go(j);
  if ok then write(f2,1)
        else write(f2,0);
  end;
close(f);
close(f2);
end.