Listing HOTEL.PAS
Program Hotel;
const maxn=50;
type date=array[1..maxn] of
record
niv:Integer;
varsta:Integer;
nume:string[40]
end;
var i,j,n,m:Integer;
c:Char;
f,g:Text;
bucatar,cofetar:date;
texte:string;
ce,relatie,nivel,s1,s2:Integer;
minmax,este1,este2:Boolean;
Procedure desface(texte:string;
var ce,relatie,nivel:Integer;
var minmax:Boolean);
var i,eroare:Integer;
begin
if Copy(texte,1,8)='bucatar:'
then ce:=1
else
if Copy(texte,1,8)='cofetar:'
then ce:=2
else ce:=3;
texte:=Copy(texte,7,Length(texte)-6);
if ce<>3 then texte:=Copy(texte,3,
Length(texte)-2);
case texte[1] of
'<': if texte[2]='='
then relatie:=4
else relatie:=1;
'>': if texte[2]='='
then relatie:=5
else relatie:=2;
'=': relatie:=3
end;
if relatie>3
then texte:=Copy(texte,3,Length(texte)-2)
else
texte:=Copy(texte,2,Length(texte)-1);
i:=Pos(':',texte);
Val(Copy(texte,1,i-1),nivel,eroare);
texte:=Copy(texte,i+1,3);
minmax:=texte='MIN'
end;
Procedure cauta(n,relatie,nivel:Integer;
unde:date; minmax:Boolean;
var exista:Boolean; var s:Integer);
var i:Integer;
Function mai_mare(i:Integer):Boolean;
begin
case relatie of
1: mai_mare:=unde[i].niv<nivel;
2: mai_mare:=unde[i].niv>nivel;
3: mai_mare:=unde[i].niv=nivel;
4: mai_mare:=unde[i].niv<=nivel;
5: mai_mare:=unde[i].niv>=nivel
end
end;
Function rel(i,s:Integer):Boolean;
begin
if minmax
then
rel:=(unde[i].varsta<unde[s].varsta)
or (unde[i].varsta=unde[s].varsta)
and (unde[i].nume<=unde[s].nume)
else
rel:=(unde[i].varsta>unde[s].varsta)
or(unde[i].varsta=unde[s].varsta)
and(unde[i].nume<=unde[s].nume)
end;
begin
s:=1;
while (s<=n) and not mai_mare(s) do s:=s+1;
for i:=s+1 to n do
if mai_mare(i) and rel(i,s) then s:=i;
exista:=s<=n
end;
Procedure Scrie_rezultate;
begin
case ce of
1: if este1
then Writeln(g,bucatar[s1].nume)
else Writeln(g,'NU ESTE');
2: if este2
then Writeln(g,cofetar[s2].nume)
else Writeln(g,'NU ESTE');
3: if este1 and este2
then
begin
if minmax
then
if (bucatar[s1].varsta<
cofetar[s2].varsta) or
(bucatar[s1].varsta=
cofetar[s2].varsta) and
(bucatar[s1].nume<=
cofetar[s2].nume)
then Writeln(g,bucatar[s1].nume)
else Writeln(g,cofetar[s2].nume)
else
if (bucatar[s1].varsta>
cofetar[s2].varsta) or
(bucatar[s1].varsta=
cofetar[s2].varsta) and
(bucatar[s1].nume<=
cofetar[s2].nume)
then Writeln(g,bucatar[s1].nume)
else Writeln(g,cofetar[s2].nume)
end
else
if este1
then Writeln(g,bucatar[s1].nume)
else
if este2
then
Writeln(g,cofetar[s2].nume)
else
Writeln(g,'NU ESTE')
end
end;
Begin
Assign(f,'BUCATAR.IN');
Reset(f);
n:=0;
while not Eof(f) do
begin
n:=n+1;
Readln(f,bucatar[n].niv,
bucatar[n].varsta,c,bucatar[n].nume)
end;
Close(f);
Assign(f,'COFETAR.IN'); Reset(f);
m:=0;
while not Eof(f) do
begin
m:=m+1;
Readln(f,cofetar[m].niv,
cofetar[m].varsta,c,cofetar[m].nume)
end;
Close(f);
Assign(f,'INTREB.IN'); Reset(f);
Assign(g,'RASPUNS.OUT'); Rewrite(g);
while not Eof(f) do
begin
Readln(f,texte);
desface(texte,ce,relatie,nivel,minmax);
if ce<>2
then cauta(n,relatie,nivel,bucatar,
minmax,este1,s1);
if ce<>1
then cauta(m,relatie,nivel,cofetar,
minmax,este2,s2);
Scrie_rezultate
end;
Close(f); Close(g)
End.