Cod sursa(job #47451)

Utilizator vanila0406Ionescu Victor vanila0406 Data 3 aprilie 2007 18:37:56
Problema Bool Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 8.75 kb
program expr;
var f,g:text;
        p,o:array[0..1001] of char;
        lo,lp,i:longint;
        a:array['A'..'\'] of longint;
        n:longint;
        c,car:char;



procedure iofile;
var i:longint;
        c:char;
        vl:longint;
begin
        assign(f,'bool.in');
        reset(f);
        assign(g,'bool.out');
        rewrite(g);
        for c:='A' to 'Z' do
                a[c]:=0;
                a['[']:=1;
                a['\']:=0;
end;



function prio(c:char):byte;
begin
        if c='-' then prio:=3 else
        if c='*' then prio:=2 else
        if c='+' then prio:=1 else
        prio:=0;
end;



procedure prel;
var c,c1:char;
        ok,go:boolean;
begin
        while not eoln(f) do
                begin
                        read(f,c);
                        go:=false;
                        if c<>' ' then
                             begin
                                   if c in ['A'..'Z'] then
                                        begin
                                                read(f,c1);
                                                ok:=false;
                                                if c1 in ['A'..'Z'] then
                                                        begin
                                                                ok:=true;
                                                                read(f,c1);
                                                        end;
                                                if ok then
                                                        begin
                                                                if c='N' then
                                                                c:='-' else
                                                                if c='A' then
                                                                c:='*' else
                                                                if c='O' then
                                                                c:='+' else
                                                                if c='T' then
                                                                        begin
                                                                        c:='[';
                                                                        read(f,c1);
                                                                end else
                                                                if c='F' then
                                                                        begin
                                                                                c:='\';
                                                                                read(f,c1);
                                                                                read(f,c1);
                                                                        end;
                                                        end;
                                                if c1='(' then
                                                        begin
                                                                inc(lo);
                                                                o[lo]:=c1;
                                                        end else
                                                        if c1=')' then
                                                                begin
                                                                go:=true;
                                                                inc(lp);
                                                                p[lp]:=c;
                                                                while (lo>0)and(o[lo]<>'(') do
                                                        begin
                                                                inc(lp);
                                                                p[lp]:=o[lo];
                                                        dec(lo);
                                                        end;
                                                if (lo<>0)and(o[lo]='(') then
                                                        dec(lo); end;

                                        end;
                                if (c in ['+','-','*','/']) then
                                        begin
                                             if prio(c)<prio(o[lo]) then
                                                begin
                                                        while (lo>0)and(o[lo]<>'(') do
                                                                begin
                                                                inc(lp);
                                                                p[lp]:=o[lo];
                                                                dec(lo);
                                                                end;
                                                        inc(lo);
                                                        o[lo]:=c;
                                                end else
                                                begin
                                                        inc(lo);
                                                        o[lo]:=c;
                                                end;
                                        end else
                                if c='(' then
                                        begin
                                                inc(lo);
                                                o[lo]:=c;
                                        end else
                                if c=')' then
                                        begin
                                                while (lo>0)and(o[lo]<>'(') do
                                                        begin
                                                                inc(lp);
                                                                p[lp]:=o[lo];
                                                        dec(lo);
                                                        end;
                                                if (lo<>0)and(o[lo]='(') then
                                                        dec(lo);
                                        end else
                                if c in ['A'..'\'] then  if go=false then
                                        begin
                                                inc(lp);
                                                p[lp]:=c;
                                        end;
                             end;

                end;
        if lo<>0 then
                for i:=lo downto 1 do
                        begin
                                inc(lp);
                                p[lp]:=o[i];
                        end;
end;


procedure afis;
var v:array[1..100] of longint;
        i,lv,d:longint;
begin
        lv:=0;
        for i:=1 to lp do
                if p[i] in ['A'..'\'] then
                        begin
                                inc(lv);
                                v[lv]:=a[p[i]];
                        end else
                        if p[i]='+' then
                                begin
                                        d:=v[lv-1]or v[lv];
                                        dec(lv);
                                        v[lv]:=d;
                                end else
                        if p[i]='-' then
                                begin
                                        d:=1- v[lv];
                                        v[lv]:=d;
                                end else
                        if p[i]='*' then
                                begin
                                        d:=v[lv-1]and v[lv];
                                        dec(lv);
                                        v[lv]:=d;
                                end; {else
                        if p[i]='/' then
                                begin
                                        d:=v[lv-1] div v[lv];
                                        dec(lv);
                                        v[lv]:=d;
                                end; }
        write(g,v[1]);
end;




begin
        car:=chr(ord('Z')+2);
        iofile;
        prel;
        {for i:=1 to lp do
                write(g,p[i]);}
        writeln(g);
        readln(f,n);
        for i:=1 to n do
                begin
                        read(f,c);
                        a[c]:=1-a[c];
                        afis;
                end;
        close(g);
end.