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

interface

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

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

var
  Form1: TForm1;
implementation

{$R *.DFM}


procedure TForm1.Button1Click(Sender: TObject);
var F:TextFile;
    S:String;
begin
if OpenDialog1.Execute then Memo1.Lines.LoadFromFile(Opendialog1.Filename)
                       else
                       begin
                       ShowMessage('Вы отказались от выбора файла');
                       Exit
                       end;
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
                 begin
                 ShowMessage('Файл пуст');
                 Exit;{Выход, если список пуст}
                 end
end;
procedure TForm1.Button3Click(Sender: TObject);
label Shag3;
const
HFMIN=Ord('0')+Ord('0');
HFMAX=Ord('z')+Ord('z');
var
   S,Word:String;
   NRow, i,Kol,KolO, Com,Key, Key1: Integer;
begin
{Рисование ХТ и занесение пустых значений во все ее ячейки}
KolO:=0;{Количество колизий}
Com:=0;{Количество сравнений}
with StringGrid1 do
begin
cells[0,0]:='HF';
cells[1,0]:='Address';
For NRow:=1 to HFMAX do
                     begin
                      cells[0,NRow]:=InttoStr(NRow);
                      cells[1,NRow]:=''
                     end
end;
{Собственно хэширование}
for i:=0 to Words.Count-1 do
begin
Kol:=0;
s:=Words[i];
if length(S)>=3 then Key:=Ord(S[1])+Ord(S[3]){Вычисление значения ключа}
                else Key:=Ord(S[1]);
if StringGrid1.Cells[1,Key]='' then
                              begin {ячейка с данным значением ХФ свободна}
                               Com:=Com+1;{число сравнений}
                               StringGrid1.Cells[1,Key]:=S;{Заносим запись в ТИ по значению указателя}
                              end
                              else {ячейка с данным заначением ХФ занята}
                              begin
Shag3:                         Kol:=Kol+1;
                               KolO:=KolO+1;
                               Com:=Com+1;
                               Key1:=(Key*Kol) mod (HFMAX-HFMIN+1);
                               if StringGrid1.Cells[1,Key1]='' then
                                                             begin {ячейка с данным значением ХФ свободна}
                                                             Com:=Com+1;{число сравнений}
                                                             StringGrid1.Cells[1,Key1]:=S;{Заносим запись в ТИ по значению указателя}
                                                             end
                                                             else {ячейка с данным заначением ХФ занята}
                                                             if Key1=Key then
                                                                       begin
                                                                       ShowMessage('Ошибка! В ХТ нет свободного места');
                                                                       Exit
                                                                       end
                                                                       else
                                                                       begin
                                                                        key:=key1;
                                                                        goto Shag3;

                                                                       end
                               end;
end;
Word:=InttoStr(KolO);
Edit2.Text:=Word;
word:=InttoSTR(Com);
Edit3.Text:=Word;
end;

procedure TForm1.Button4Click(Sender: TObject);
Label M1;
var i,Address,NomNow,Code,Com,Kol: integer;
    SnomNow,Now,Word:String;
begin
with StringGrid2 do
begin
cells[0,0]:='Address';
cells[1,0]:='Meaning';
cells[2,0]:='Left';
cells[3,0]:='Right';
for i:=1 to 245 do begin
                   cells[0,i]:=InttoStr(i);
                   cells[1,i]:='';
                   cells[2,i]:='';
                   cells[3,i]:=''
                   end;
end;
Com:=0;
Kol:=0;
StringGrid2.Cells[1,1]:=Words[0];
Address:=1;
for i:=1 to Words.Count-1 do
begin
NomNow:=1;
Now:=StringGrid2.Cells[1,1];
M1:if Words[i]=Now then
                  begin
                  ShowMessage('Ошибка! Не может быть двух одинаковых идентификаторов');
                  Exit
                  end
                  else
                  begin
                  if Words[i]<Now then
                                  begin
                                  Com:=Com+1;
                                  if stringGrid2.Cells[2,NomNow]='' then
                                                                     begin
                                                                     Address:=Address+1;
                                                                     StringGrid2.Cells[2,NomNow]:=InttoStr(Address);
                                                                     StringGrid2.Cells[1,Address]:=Words[i];
                                                                     end
                                                                     else
                                                                     begin
                                                                     Kol:=Kol+1;
                                                                     SNomNow:=StringGrid2.Cells[2,NomNow];
                                                                     val(SNomNow,NomNow,Code);
                                                                     Now:=StringGrid2.Cells[1,NomNow];
                                                                     goto M1
                                                                     end
                                  end
                                  else
                                  begin
                                  Com:=Com+1;
                                  if stringGrid2.Cells[3,NomNow]='' then
                                                                     begin
                                                                     Address:=Address+1;
                                                                     StringGrid2.Cells[3,NomNow]:=InttoStr(Address);
                                                                     StringGrid2.Cells[1,Address]:=Words[i];
                                                                     end
                                                                     else
                                                                     begin
                                                                     Kol:=Kol+1;
                                                                     SNomNow:=StringGrid2.Cells[3,NomNow];
                                                                     val(SNomNow,NomNow,Code);
                                                                     Now:=StringGrid2.Cells[1,NomNow];
                                                                     goto M1
                                                                     end
                                  end
                  end
end;
Word:=inttostr(Com);
Edit5.Text:=Word
end;

procedure TForm1.Button2Click(Sender: TObject);
const
HFMIN=Ord('0')+Ord('0');
HFMAX=Ord('z')+Ord('z');
label Poisk;
var s,Word:string;
    Kol,Com,Key,Key1:integer;
begin
Kol:=0;
Com:=0;
S:=Edit1.Text;
if s='' then
        begin
        Showmessage('Вы не ввели идентификатор для поиска');
        Exit
        end;
if length(S)>=3 then Key:=Ord(S[1])+Ord(S[3])
                else Key:=Ord(S[1]);
if StringGrid1.Cells[1,Key]='' then
                               begin
                               Com:=Com+1;
                               ShowMessage('Идентификатор не найден');
                               Word:=InttoStr(Com);
                               Edit6.Text:=Word;
                               Word:=InttoStr(Kol);
                               Edit7.Text:=Word;
                               Exit
                               end
                               else
                               begin
                               if S=StringGrid1.Cells[1,Key] then
                                                             begin
                                                             Com:=Com+1;
                                                             ShowMessage('Идентификатор находится по адресу'+inttoStr(Key));
                                                             Word:=InttoStr(Com);
                                                             Edit6.Text:=Word;
                                                             Word:=InttoStr(Kol);
                                                             Edit7.Text:=Word;
                                                             Exit
                                                             end
                                                             else
                                                             begin
                                                             Kol:=1;
                                                             Com:=Com+1;
Poisk:                                                       Key1:=(Key*Kol) mod (HFMAX-HFMIN+1){-2};
                                                             if StringGrid1.Cells[1,Key1]='' then
                                                                                            begin
                                                                                            Com:=Com+1;
                                                                                            ShowMessage('Идентификатор не найден');
                                                                                            Word:=InttoStr(Com);
                                                                                            Edit6.Text:=Word;
                                                                                            Word:=InttoStr(Kol);
                                                                                            Edit7.Text:=Word;
                                                                                            Exit
                                                                                            end
                                                                                            else
                                                                                            begin
                                                                                            if S=StringGrid1.Cells[1,Key1] then
                                                                                                                           begin
                                                                                                                           Com:=Com+1;
                                                                                                                           ShowMessage('Идентификатор находится по адресу'+inttoStr(Key1));
                                                                                                                           Word:=InttoStr(Com);
                                                                                                                           Edit6.Text:=Word;
                                                                                                                           Word:=InttoStr(Kol);
                                                                                                                           Edit7.Text:=Word;
                                                                                                                           Exit
                                                                                                                           end
                                                                                                                           else
                                                                                                                           begin
                                                                                                                           Key:=Key1;
                                                                                                                           Kol:=Kol+1;
                                                                                                                           goto Poisk
                                                                                                                           end
                                                                                            end;
                                                             end
                               end

end;

procedure TForm1.Button5Click(Sender: TObject);
label der;
var S,Now,Word:string;
    Com,Kol,Code,NomNow:integer;
begin
Com:=0;
Kol:=0;
S:=Edit1.Text;
If S='' then begin
             ShowMessage('Вы не ввели идентификатор для поиска');
             Exit
             end;
NomNow:=1;
Now:=StringGrid2.Cells[1,NomNow];
der:if S=Now then
         begin
         Com:=Com+1;
         ShowMessage('Идентификатор находится по адресу '+InttoStr(NomNow));
         Word:=InttoStr(Com);
         Edit8.Text:=Word;
         end
         else
         begin
         Com:=Com+1;
         if S<Now then
                  begin
                  Com:=Com+1;
                  Kol:=Kol+1;
                  if StringGrid2.Cells[2,NomNow]='' then
                                                    begin
                                                    ShowMessage('Идентификатор не найден ');
                                                    Word:=InttoStr(Com);
                                                    Edit8.Text:=Word;
                                                    end
                                                    else
                                                    begin
                                                    val(StringGrid2.Cells[2,NomNow],NomNow,Code);
                                                    Now:=StringGrid2.Cells[1,NomNow];
                                                    Goto der
                                                    end
                  end
                  else
                  begin
                  Com:=Com+1;
                  Kol:=Kol+1;
                  if StringGrid2.Cells[3,NomNow]='' then
                                                    begin
                                                    ShowMessage('Идентификатор не найден ');
                                                    Word:=InttoStr(Com);
                                                    Edit8.Text:=Word;
                                                    end
                                                    else
                                                    begin
                                                    val(StringGrid2.Cells[3,NomNow],NomNow,Code);
                                                    Now:=StringGrid2.Cells[1,NomNow];
                                                    Goto der
                                                    end
                  end
         end

end;

end.
Соседние файлы в папке СПО