Cod sursa(job #194660)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 12 iunie 2008 15:02:20
Problema Bool Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.8 kb
var a : array['A'..'Z'] of boolean;
    t : ansistring;
    n,i,k,m : integer;
    c : char;
    f,g : text;
    sh:boolean;
procedure scoate(op,r : string);
var poz: integer;
begin
  poz:=pos(op,t);
    while poz<>0 do
    begin
      delete(t,poz,length(op));
      insert(r,t,poz);
      poz:=pos(op,t);
    end;
end;

function term : boolean; forward;
function fact : boolean; forward;

function expr : boolean;
var aux,w : boolean;
begin
  aux:=term;
  while (k<=m)and(t[k]='o')do
  begin
    inc(k);
    w:=term;
    aux:=aux or w;
  end;
  expr:=aux;
end;

function term:boolean;
var aux,w : boolean;
begin
  aux:=fact;
  while (k<=m)and(t[k]='a')do
  begin
    inc(k);
    w:=fact;
    aux:=aux and w;
  end;
  term:=aux;
end;

function fact : boolean;
var aux,w : boolean;
begin
  if (t[k]='1') or (t[k]='0')then
  begin
    if t[k]='0' then aux:=false
    else aux:=true;
    inc(k);
  end
  else
    if t[k]='(' then
    begin
      inc(k);
      aux:=expr;
      inc(k);
    end
    else
      if t[k] in ['A'..'Z']then
      begin
        aux:=a[t[k]];
        inc(k);
      end
      else
        if t[k]='n' then
        begin
          inc(k);
          w:=fact();
          aux:=not w;
        end;
  fact:=aux;
end;

begin
  assign(f,'bool.in');reset(f);
  assign(g,'bool.out');rewrite(g);
  readln(f,t);
  scoate('AND','a');
  scoate('OR','o');
  scoate('NOT','n');
  scoate(' ','');
  scoate('nn','');
  scoate('TRUE','1');
  scoate('FALSE','0');
  readln(f,n); m:=length(t);
  writeln(t);
  for c:='A' to 'Z' do a[c]:=false;
    for i:=1 to n do
    begin
      read(f,c);
      a[c]:=not a[c] ;
      k:=1;
      sh:=expr;
     if sh then write(g,'1')
     else write(g,'0');
      writeln(k);
    end;
  close(g);
end.