Скачиваний:
22
Добавлен:
02.05.2014
Размер:
16.86 Кб
Скачать
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;

type
TForm1 = class(TForm)
Memo1: TMemo;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
Label1: TLabel;
Label2: TLabel;
OpenDialog1: TOpenDialog;
Button1: TButton;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
Label3: TLabel;
Label4: TLabel;
GroupBox3: TGroupBox;
Label5: TLabel;
Edit1: TEdit;
Button2: TButton;
Label6: TLabel;
Edit2: TEdit;
Label7: TLabel;
Edit3: TEdit;
Label8: TLabel;
Edit4: TEdit;
Label9: TLabel;
Edit5: TEdit;
Label10: TLabel;
Edit6: TEdit;
Label11: TLabel;
Edit7: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
Words: TstringList;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
label M1;
var
F:TextFile;
S:String;
NRow: Integer;

begin
{Загрузка файла при помощи пользовательского диалога в поле Memo}
if OpenDialog1.Execute then Memo1.Lines.LoadFromFile(OpenDialog1.Filename)
else Exit;{Выход, если пользователь отказался выбрать файл}
AssignFile(F, OpenDialog1.FileName);
reset(f);{Открытие файла для чтения}
{Создание строкового списка из строк файла}
Words:=TStringList.Create;{Вызов конструктора класса}
Words.Sorted:=False;
Screen.Cursor:=crHourGlass;{Изменение типа курсора перед длительной работой}
{Извлечение строк из файла и занесение их в список}
while not EOF(F) do
begin
readln(F,S);
if S<>'' then Words.Add(S)
end;
Screen.Cursor:=crDefault;{Восстановление типа курсора по умолчанию}
CloseFile(F);
if Words.Count=0 then Exit;{Выход, если список пуст}
{Рисование ХТ и занесение пустых значений во все ее ячейки}
with StringGrid1 do
begin
cells[0,0]:='HF';
cells[1,0]:='Address';
For NRow:=1 to 255 do cells[0,NRow]:='d'+InttoStr(NRow);
end;
{Рисование ТИ}
with StringGrid2 do
begin
cells[0,0]:='Address';
cells[1,0]:='Meaning';
cells[2,0]:='Left';
cells[3,0]:='Right';
end;

end;

procedure TForm1.Button2Click(Sender: TObject);
label M1;
var
S,SLInk,Word:String;
HF,Code,Link,Com,L: Integer;
ComS:real;
begin
Com:=0;
S:=Edit1.Text;{Считывание идентификатора}
{Собственно хэширование}
L:=length(S);
HF:=Ord(S[1])+Ord(S[L])+Ord(S[trunc(L/2)+1]);{Вычисление значения хэш-функции}
if StringGrid1.Cells[1,HF]='' then
begin {ячейка с данным значением ХФ свободна}
Com:=Com+1;
ShowMessage('Идентификатор с данным значением в таблице отсутствует');
Word:=InttoStr(Com);
Edit6.text:=Word;
ComS:=Com/Memo1.Lines.Count;
Word:=FloattoStr(ComS);
Edit7.Text:=Word;
Exit
end
else{ячейка с данным заначением ХФ занята}
begin
Com:=Com+1;
SLink:=StringGrid1.Cells[1,HF];{считываем адрес из ХФ}
val(SLink,Link,Code);{переходим по ссылке}
M1: if S=StringGrid2.Cells[1,Link] then {идентификатор найден}
begin
Com:=Com+1;
Showmessage('Идентификатор находится по адресу '+SLink);
Word:=InttoStr(Com);
Edit6.text:=Word;
ComS:=Com/Memo1.Lines.Count;
Word:=FloattoStr(ComS);
Edit7.Text:=Word;
Exit
end
else
if S>StringGrid2.Cells[1,Link] then
begin
Com:=Com+1;
if StringGrid2.Cells[3,Link]='' then
begin{Правая ссылочная область пуста, Идентификатор не найден}
Com:=Com+2;
ShowMessage('Идентификатор с данным значением в таблице отсутствует');
Word:=InttoStr(Com);
Edit6.text:=Word;
ComS:=Com/Memo1.Lines.Count;
Word:=FloattoStr(ComS);
Edit7.Text:=Word;
Exit
end
else
begin{Правая ссылочная область занята}
Com:=Com+2;
SLink:=StringGrid2.Cells[3,Link];{читаем из нее адрес}
val(SLink,Link,Code);{переходим по ссылке}
goto M1
end
end
else {Ид находится левее старого}
if StringGrid2.Cells[2,Link]='' then
begin {левая ссылочная область пуста}
Com:=Com+1;
ShowMessage('Идентификатор с данным значением в таблице отсутствует');
Word:=InttoStr(Com);
Edit6.text:=Word;
ComS:=Com/Memo1.Lines.Count;
Word:=FloattoStr(ComS);
Edit7.Text:=Word;
Exit
end
else{левая ссылочная облась занята}
begin
SLink:=StringGrid2.Cells[2,Link];
Com:=Com+1;
val(SLink,Link,Code);
goto M1
end
end;

end;

procedure TForm1.Button3Click(Sender: TObject);
label M1;
var
S,SLInk,Word:String;
Address, NRow, HF,i,j,Code,Link, Kol, Com,L: Integer;
KolS,ComS:real;

begin
with StringGrid1 do
begin
cells[0,0]:='HF';
cells[1,0]:='Address';
For NRow:=1 to 255 do begin
cells[0,NRow]:='d'+InttoStr(NRow);
cells[1,NRow]:='';
end;
end;
{Рисование ТИ}
with StringGrid2 do
begin
cells[0,0]:='Address';
cells[1,0]:='Meaning';
cells[2,0]:='Left';
cells[3,0]:='Right';
for i:=1 to 3 do
for j:=1 to 100 do
StringGrid2.Cells[i,j]:='';
end;
Address:=1;{Указатель указывает на первую строку в ТИ}
Kol:=0;{Количество колизий}
Com:=0;{Количество сравнений}
{Собственно хэширование}
for i:=0 to Words.Count-1 do
begin
s:=Words[i];
L:=length(S);
HF:=Ord(S[1])+Ord(S[L])+Ord(S[trunc(L/2)+1]);{Вычисление значения хэш-функции}
if StringGrid1.Cells[1,HF]='' then
begin {ячейка с данным значением ХФ свободна}
Com:=Com+1;
StringGrid1.Cells[1,HF]:=Inttostr(Address);{заносим указатель в ХТ}
StringGrid2.Cells[0,Address]:=InttoStr(Address);{прорисовываем адрес в ТИ}
StringGrid2.Cells[1,Address]:=S;{Заносим запись в ТИ по значению указателя}
end
else {ячейка с данным заначением ХФ занята}
begin
Kol:=Kol+1;
Com:=Com+1;
SLink:=StringGrid1.Cells[1,HF];{считываем адрес из ХФ}
val(SLink,Link,Code);{переходим по ссылке}
M1: if S>StringGrid2.Cells[1,Link] then {добавляемый идентификатор находится правее старого}
begin
Com:=Com+1;
if StringGrid2.Cells[3,Link]='' then
begin{Правая ссылочная область пуста}
Com:=Com+1;
StringGrid2.Cells[3,Link]:=InttoStr(Address);
StringGrid2.Cells[0,Address]:=InttoStr(Address);
StringGrid2.Cells[1,Address]:=S
end
else
begin{Правая ссылочная область занята}
Com:=Com+1;
Kol:=Kol+1;
SLink:=StringGrid2.Cells[3,Link];{читаем из нее адрес}
val(SLink,Link,Code);{переходим по ссылке}
goto M1
end
end
else {ДИд находится левее старого}
begin
Com:=Com+1;
if StringGrid2.Cells[2,Link]='' then
begin {левая ссылочная область пуста}
Com:=Com+1;
StringGrid2.Cells[2,Link]:=InttoStr(Address);
StringGrid2.Cells[0,Address]:=InttoStr(Address);
StringGrid2.Cells[1,Address]:=S
end
else{левая ссылочная облась занята}
begin
Kol:=Kol+1;
Com:=Com+1;
SLink:=StringGrid2.Cells[2,Link];
val(SLink,Link,Code);
goto M1
end
end
end;
Address:=Address+1{переход указателя на следующую запись}
end;
Word:=InttoStr(Kol);
Edit2.Text:=Word;
word:=InttoSTR(Com);
Edit3.Text:=Word;
KolS:=Kol/Memo1.Lines.Count;
ComS:=Com/Memo1.Lines.Count;
Word:=FloattoStr(KolS);
Edit4.Text:=Word;
Word:=FloattoStr(ComS);
Edit5.Text:=Word;
end;
end.
Соседние файлы в папке СПО
  • #
    02.05.2014188 б22Project1.dpr
  • #
    02.05.20141 Кб23Project1.dsk
  • #
    02.05.2014876 б22Project1.res
  • #
    02.05.20149.78 Кб22Unit1.dcu
  • #
    02.05.20144.48 Кб22Unit1.dfm
  • #
    02.05.201416.86 Кб22Unit1.pas