Pagini recente » Cod sursa (job #1440283) | Cod sursa (job #1271482) | Cod sursa (job #3145752) | Cod sursa (job #2935398) | Cod sursa (job #35101)
Cod sursa(job #35101)
program bool;
var a:array[0..1005] of char;
b:array[1..2005] of string;
car,poz,c,t,i,n,m:longint;
x:char;
f,g:text;
sel:array[0..30] of boolean;
function termen():longint; forward;
function factor():longint; forward;
function expresie():longint;
var rez:longint;
begin
rez:=termen();
while b[car]='OR' do begin
inc(car);
rez:=rez or termen();
end;
expresie:=rez;
end;
function termen():longint;
var rez:longint;
begin
rez:=factor();
while b[car]='AND' do begin
inc(car);
rez:=rez and factor();
end;
termen:=rez;
end;
function factor():longint;
var x,rez:longint;
begin
if b[car]='(' then begin
inc(car);
rez:=expresie();
inc(car);
end
else begin
if b[car]='NOT' then begin
inc(car);
if b[car]='(' then begin
inc(car);
rez:=expresie();
if rez=1 then rez:=0 else rez:=1;
inc(car);
end
else begin
x:=factor();
if x=1 then rez:=0 else rez:=1;
end;
end
else begin
if b[car]='FALSE' then rez:=0 else
if b[car]='TRUE' then rez:=1 else begin
x:=ord(b[car][1])-ord('A')+1;
if sel[x]=false then rez:=0 else rez:=1;
inc(car);
end;
end;
end;
factor:=rez;
end;
begin
assign(f,'2-bool.in'); reset(f);
assign(g,'bool.out'); rewrite(g);
while not(eoln(f)) do begin
inc(n); read(f,a[n]);
end;
i:=1;
while i<=n do begin
if (a[i]='N') and(a[i+1]='O') and(a[i+2]='T') then begin
inc(m); b[m]:='NOT';
i:=i+2;
end
else
if (a[i]='(') or(a[i]=')') then begin
inc(m); b[m]:=a[i];
end
else
if(a[i]='O') and(a[i+1]='R') then begin
inc(m); b[m]:='OR';
i:=i+1;
end
else
if (a[i]='A') and(a[i+1]='N') and(a[i+2]='D') then begin
inc(m); b[m]:='AND';
i:=i+2;
end
else
if (a[i]='T') and(a[i+1]='R') and(a[i+2]='U') and(a[i+3]='E') then begin
inc(m); b[m]:='TRUE';
i:=i+3;
end
else if (a[i]='F') and(a[i+1]='A') and(a[i+2]='L') then begin
inc(m); b[m]:='FALSE';
i:=i+4;
end
else if a[i] in ['A'..'Z'] then begin
inc(m); b[m]:=a[i];
end;
inc(i);
end;
fillchar(sel,sizeof(sel),false);
readln(f,t);
for i:=1 to t do begin
read(f,x); car:=1;
poz:=ord(x)-ord('A')+1;
if sel[poz]=true then sel[poz]:=false else sel[poz]:=true;
c:=expresie();
write(g,c);
end;
writeln(g);
close(f); close(g);
end.