Cod sursa(job #482418)

Utilizator danalex97Dan H Alexandru danalex97 Data 3 septembrie 2010 15:35:45
Problema Bool Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.01 kb
type sir=array[1..1001] of char;
var f,g:text;
    nr:array['A'..'Z'] of integer;
    v:sir;
    p,n,l,i,x:integer;
    c,u:char;
function exp(var p:integer):byte;forward;
function va(var p:integer):byte;
begin
     if v[p]='(' then
     begin
          inc(p);
          va:=exp(p);
          inc(p);
     end
     else
     if (v[p]+v[p+1]+v[p+2]='NOT')and(p+2<l) then
                         begin
                              p:=p+3;
                              va:=1-va(p);
                         end else
      if (v[p] in ['A'..'Z'])and(p<=l) then
          begin
               va:=nr[v[p]]; inc(p); end
                else
           if (v[p] in ['0','1'])and(p<=l) then
           begin
               va:=ord(v[p])-ord('0');      inc(p); end;
end;
function shi(var p:integer):byte;
var r:byte;
begin
     r:=va(p);
     while (v[p]+v[p+1]+v[p+2]='AND')and(p+2<l) do
     begin
          p:=p+3;
          r:=r and va(p);
     end;
     shi:=r;
end;
function exp(var p:integer):byte;
var r:byte;
begin
     r:=shi(p);
     while (v[p]+v[p+1]='OR')and(p+1<l) do
     begin
          p:=p+2;
          r:=r or shi(p);
     end;
     exp:=r;
end;
BEGIN
     assign(f,'bool.in'); reset(f);
     assign(g,'bool.out'); rewrite(g);
     while not eoln(f) do
     begin
          read(f,c);
          if c<>' 'then
          begin
               inc(l);
               v[l]:=c;
          end;
     end;
     while pos('TRUE',v)<>0 do
     begin
          x:=pos('TRUE',v);
          v[x]:='1';
          for i:=x+1 to l-3 do v[i]:=v[i+3];
          l:=l-3;
     end;
     while pos('FALSE',v)<>0 do
     begin
          x:=pos('FALSE',v);
          v[x]:='0';
          for i:=x+1 to l-4 do v[i]:=v[i+4];
          l:=l-4;
     end;
     for c:='A' to 'Z' do nr[c]:=0;
     readln(f,n);
     for i:=1 to n do
     begin
          read(f,u);
          nr[u]:=1-nr[u];
          p:=1;
          write(g,exp(p));
     end;
     writeln(g);
     close(g);
end.