Program lab5;
uses crt;
const
n=1000;
type cell = record
slov:string[50];
key: integer;
end;
var
kolcol,kolsl,sel,quit,i,j,x,k:integer;{переменные}
s,s1:string;
ch:char;
f1:text;
hashtbl: array[0..n] of cell;
bool:boolean;
{Функция получения ХЭШ кода слова}
Function hash(s1:string): Integer;
var res,ti,code:integer;
st:string;
begin
res:=0;
for i:=1 to length(s1) do
res:=(255*res+abs(ord(s1[i])))mod kolsl;
hash:=res;
end;
{Функция сравнения слов}
Function StrCm(s3,s4:string): Integer;
var bool2:boolean; kp:integer;
begin
bool2:=false;
kp:=1;
StrCm:=0;
If length(s3)=length(s4) then
while (bool2<>true) and (length(s3)<>kp) do
begin
if s3[kp]<>s4[kp] then
begin
bool2:=true;
StrCm:=1;
end
else
begin
kp:=kp+1;
end;
end
else
StrCm:=1;
end;
{Метод линейных проб}
procedure linpro(s1:string);
var st,s2:array [0..50] of char;
hs,num,res:integer;
begin
{Получаем ХЭШ код слова}
hs:=hash(s1);
num:=hs;
{Ищем в массиве место для данного кода}
bool:=true;
while (bool=true) do
begin
{Если в ячейке есть значение, то коллизия}
if hashtbl[num].key<>0 then
begin
res:=StrCm(hashtbl[num].slov,s1);
If res<>0 then
begin
kolcol:=kolcol+1;
num:=num+1;
if (num>kolsl) and (num<n) then kolsl:=num;
if (num>n) then num:=num mod n;
end
else
begin
bool:=false;
end;
end
else
begin
Hashtbl[num].slov:=s1;
hashtbl[num].key:=num;
bool:=false;
end;
end;
end;
{Метод квадратичных проб}
procedure kvpro(s1:string);
var
hs,num,zn,res:integer;
bool:boolean;
begin
Hs:=hash(s1);
num:=hs;
zn:=1;
i:=1;
{Поиск в массиве места для слова}
bool:=true;
while bool=true do
begin
{Если в ячейке есть значение, то коллизия}
if hashtbl[num].key<>0 then
begin
res:=StrCm(hashtbl[num].slov,s1);
If res<>0 then
begin
kolcol:=kolcol+1;
{Находим новую позицию как квадрат, меняем знак}
num:=(num+i*zn)+(num+i*zn);
zn:=zn*-1;
if zn=1 then i:=i+1;
if (num>kolsl) and (num<n) then kolsl:=num;
{Проверяем, не вышли ли за пределы массива}
if (num>n) then num:=num mod n;
if num<0 then num:=1;
end
else
bool:=false;
end
else
begin
Hashtbl[num].slov:=s;
hashtbl[num].key:=num;
bool:=false;
end;
end;
end;
{Вывод таблицы}
procedure writecell(h: array of cell);
begin
writeln('| KEY | Slovo | KEY | Slovo |');
for i:=1 to n do
begin
if h[i].key<>0 then
if i mod 40 <> 0 then
if i mod 2 = 0 then
writeln(h[i].key:16,h[i].slov:21)
else
write(h[i].key:16,h[i].slov:21)
else
readln;
end;
end;
begin {начало программы}
clrscr; {очистка экрана}
assign(f1,'Text.txt');
randomize;
while quit<>1 do
begin
clrscr;
writeln('-----------------------');
writeln('Laboratornaya rabota #5');
writeln('1. Prosmotr Teksta');
writeln('2. HASH');
writeln('3. Exit');
writeln('-----------------------');
writeln('----BY-HELL_Phoenix----');
readln(sel);
case sel of
1:
begin
clrscr;
reset(f1);
s:='';
while Not Eof(f1) Do
begin
read(f1,ch);
write(ch);
end;
readln;
end;
2:
begin
clrscr;
kolcol:=0;
kolsl:=200;
s:='';
{Обнуляем значения массива}
for i:=0 to n do
begin
hashtbl[i].key:=0;
hashtbl[i].slov:='';
end;
{Метод линейных проб}
assign(f1,'Text.txt');
reset(f1);
while not eof(f1) do
begin
read(f1,ch);
if ch<>' ' then
s:=s+ch
else
begin
linpro(s);
s:='';
end;
end;
j:=0;
for i:=1 to n do
if hashtbl[i].key<>0 then
j:=j+1;
clrscr;
Writeln('Lineynie probi: Ob"em tablici = ',j,' ','Collisiya = ',kolcol);
close(f1);
assign(f1,'Text.txt');
reset(f1);
kolsl:=200;
kolcol:=0;
s:='';
{Обнуляем значения массива}
for i:=0 to n do
begin
hashtbl[i].key:=0;
hashtbl[i].slov:='';
end;
{Метод квадратичных проб}
reset(f1);
while not eof(f1) do
begin
read(f1,ch);
if ch<>' ' then
s:=s+ch
else
begin
kvpro(s);
s:='';
end;
end;
j:=0;
for i:=1 to n do
if hashtbl[i].key<>0 then
j:=j+1;
writeln;
Writeln('Kvadratichnye probi: Ob"em tablici = ',j,' ','Collisiya = ',kolcol);
readln;
end;
3:
begin
break;
end;
end;
end;
end.
Not long after we moved to Paris, in the fall of 1995, my wife, Martha, and I saw, in the window of a shop on the rue Saint-Sulpice, a nineteenth-century engraving, done in the manner, though I'm now inclined to think not from the hand, of Daumier. It shows a train on its way from the Right Bank of Paris to the moon. The train has a steam locomotive and six cars, and it is chugging up a pretty steep track. The track is supported on two high, slender spires that seem to be anchored somewhere in the Fifth Arrondissement (you can see the Pantheon in silhouette nearby), and then the track just goes right up and touches the full moon up in the clouds. I suppose the two pillars are stronger than they look. The train is departing at twilight—presumably its an overnight trip—and among the crowd on the ground below, only a couple of top-hatted bourgeois watch the lunar ex press go on its way with any interest, much less wonder. Every body else in the crowd of thirteen or so people on the platform, mostly moms and dads and kids, are running around and making
begin {начало программы}
clrscr; {очистка экрана}
assign(f1,'Text.txt');
...
case sel of
1:
begin
clrscr;
reset(f1);
...
2:
begin
...
assign(f1,'Text.txt');
reset(f1);
...
var
a:word;
b:integer;
begin
a:=130;
b:=130;
write(a*255:10,b*255:10);
readln;
end.
Function StrCm(s3,s4:string): boolean;
var
bool2:boolean;
begin
bool2:=length(s3)=length(s4);
while (bool2) and (length(s3)>0) do
begin
bool2:=s3[1]=s4[1];
delete(s3,1,1);
delete(s4,1,1);
end;
StrCm:=bool2;
end;
hs:=hash(s1);
num:=hs;
. . .
res:=StrCm(hashtbl[num].slov,s1);
If res<>0 then
Если Вы уже зарегистрированы на Портале - войдите в систему, если Вы еще не регистрировались - пройдите простую процедуру регистрации.