unit Main;

interface


procedure crypt(var s:string);
procedure encrypt(var s:string);

type
TOtvet = String;
TUrovni=array[1..3] of integer;
TOtvets = Record
Otvets: Array[1..5] of TOtvet;
GoodOtvet: Integer;
end;

TVopros = Record
TextVoprosa: String;
Otvets: TOtvets;
Slozhn: Integer;
EstKartinka: Boolean;
FileKartinki: String;
NomerV: Integer;
end;

TVoprosi = array[1..1900] of TVopros;
procedure loadtest(filename:string);
procedure select_questions(urovni:turovni);

var
Voprosi:TVoprosi;
NVoprosov:integer;

implementation
 uses dialogs;

procedure crypt(var s:string);
function boolrnd:boolean;
var
r:boolean;
begin
if random>0.5 then r:=true else r:=false;
boolrnd:=r;
end;
var
sout:string;
a:integer;
b,b1,b2:byte;
m1,m2:byte;
begin
randomize;
m1:=1+4+16+64;
m2:=2+8+32+128;
sout:='';
for a:=1 to length(s) do begin
 b:=ord(s[a]);
 repeat
 b1:=b and m1;
 b2:=b and m2;
 if boolrnd then b1:=b1+2;
 if boolrnd then b1:=b1+8;
 if boolrnd then b1:=b1+32;
 if boolrnd then b1:=b1+128;

 if boolrnd then b2:=b2+1;
 if boolrnd then b2:=b2+4;
 if boolrnd then b2:=b2+16;
 if boolrnd then b2:=b2+64;
 b2:=not b2;
 until ((b1>=32) and (b2>=32));
sout:=sout+char(b1);
sout:=sout+char(b2);
end;
s:=sout;
end;


procedure encrypt(var s:string);
var
sout:string;
index,a:integer;
m1,m2,b1,b2,b:byte;
begin
sout:='';
m1:=1+4+16+64;
m2:=2+8+32+128;
for a:=1 to length(s) div 2 do begin
index:=a*2;
b1:=ord(s[index-1]);
b2:=ord(s[index]);
b2:=not b2;
b1:=b1 and m1;
b2:=b2 and m2;
b:=b1 or b2;
sout:=sout+char(b);
end;
s:=sout;
end;




procedure select_questions(urovni:turovni);
var
selected:array[1..1900] of boolean;
outp:array[1..1900] of integer;
voprosi2:tvoprosi;
i,ur:integer;
cnt:integer;
m:integer;
function find_check(u:integer):boolean;
var
a:integer;
res:boolean;
begin
res:=false;
for a:=1 to nvoprosov do if not selected[a] then if voprosi[a].Slozhn=u then res:=true;
find_check:=res;
end;
procedure addtooutp(n:integer);
begin
selected[n]:=true;
inc(cnt);
outp[cnt]:=n;
end;
begin
cnt:=0;
for i:=1 to nvoprosov do selected[i]:=false;
for ur:=1 to 3 do
for i:=1 to urovni[ur] do
if find_check(ur) then begin
repeat
  repeat
    repeat
      m:=random(nvoprosov)+1;
    until m in [1..nvoprosov];
  until voprosi[m].Slozhn=ur;
until not selected[m];
addtooutp(m);
end else begin
showmessage('Ошибка! Невозможно создать требуемую выборку вопросов из данной базы...');
exit;
end;
for i:=1 to cnt do voprosi2[i]:=voprosi[outp[i]];
voprosi:=voprosi2;
nvoprosov:=cnt;
end;

procedure loadtest(filename:string);
var
i,j: Integer;
f: Text;
begin
assign(f,filename);
reset(f);
readln(f,nvoprosov);
for i:=1 to nvoprosov do begin
readln(f,voprosi[i].NomerV);
readln(f,voprosi[i].otvets.goodotvet);
readln(f,voprosi[i].slozhn);
readln(f,voprosi[i].textvoprosa);
for j:=1 to 5 do readln(f,voprosi[i].otvets.otvets[j]);
end;
closefile(f);
end;



end.
