
Interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls, ExtCtrls;
CONST Nmax=1000;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
N11: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
ListBox1: TListBox;
ListBox2: TListBox;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
OD: TOpenDialog;
SD: TSaveDialog;
C: TMenuItem;
ListBox3: TListBox;
Button3: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure N1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure CClick(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure ListBox2Click(Sender: TObject);
procedure ListBox3Click(Sender: TObject);
end;
var
Form1: TForm1;
X: array [1..Nmax] of integer;
Count:integer;
ThreadID1, ThreadID2, ThreadID3: DWord;
HThread1, HThread2, HThread3: THandle;
Implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if Edit1.Text<>'' then
begin
listBox1.Items.Add(Edit1.Text);
listBox2.Items.Add(Edit1.Text);
listBox3.Items.Add(Edit1.Text);
Count:=Count+1;
x[Count-1]:=strtoint(Edit1.Text);
Edit1.Clear;
Edit1.SetFocus;
end
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ListBox1.DeleteSelected;
ListBox2.DeleteSelected;
ListBox3.DeleteSelected;
Count:=Count-1;
end;
procedure TForm1.CClick(Sender: TObject);
var
f:file of integer;
i,k:integer;
begin
if SD.Execute then
begin
AssignFile(f,SD.FileName);
rewrite(f);
for i:=0 to ListBox1.Count-1 do
begin
k:=Strtoint(ListBox1.Items[i]);
write(f,k);
end;
closefile(f);
end;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in [#48..#57,#16,#8]) then
Key:=#0
end;
procedure TForm1.N1Click(Sender: TObject);
var
f:file of integer;
k:integer;
begin
if OD.Execute then
begin
Count:=0;
AssignFile(f,OD.FileName);
reset(f);
while not eof(f) do
begin
read(f,k);
ListBox1.Items.Add(Inttostr(k));
ListBox2.Items.Add(IntToStr(k));
ListBox3.Items.Add(IntToStr(k));
x[Count]:=k;
Count:=Count+1;
end;
closefile(f);
end;
end;
procedure Execute1;
var
i,j,c:integer;
begin
for i:=1 to Count-1 do
begin
j:=i-1;
c:=x[i];
while (c<x[j]) and (j>=0) do
begin
x[j+1]:=x[j];
j:=j-1;
end;
x[j+1]:=c;
end;
end;
procedure Execute2;
var
i,j,c,m:integer;
begin
for i:=0 to Count-1 do
begin
m:=i;
c:=x[i];
for j:=i+1 to Count-1 do
if x[j]<c
then
begin
m:=j;
c:=x[j];
end;
x[m]:=x[i];
x[i]:=c;
end;
end;
procedure Execute3;
var
i,j,h,tmp:integer;
flag:boolean;
begin
h:=(Count-1) div 2;
repeat
i:=h;
repeat
j:=i-h;
flag:=true;
repeat
if x[j]<=x[j+h]
then
flag:=false
else
begin
tmp:=x[j];
x[j]:=x[j+h];
x[j+h]:=tmp;
end;
j:=j-h;
until (j<0) or not flag;
i:=i+1;
until i>(Count-1);
h:=h div 2;
until h<=0;
end;
procedure TForm1.N3Click(Sender: TObject);
var
i,j,c,m:integer;
begin
ListBox1.Items.Clear;
ListBox2.Items.Clear;
ListBox3.Items.Clear;
HThread1:=CreateThread(nil,0,@Execute1,nil,0,ThreadID1);
HThread2:=CreateThread(nil,0,@Execute2,nil,0,ThreadID2);
HThread3:=CreateThread(nil,0,@Execute3,nil,0,ThreadID3);
for i:=0 to Count-1 do
begin
ListBox1.Items.Add(IntToStr(x[i]));
ListBox2.Items.Add(IntToStr(x[i]));
ListBox3.Items.Add(IntToStr(x[i]));
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Count:=0;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Edit1.SetFocus;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
ListBox2.ItemIndex:=ListBox1.ItemIndex;
ListBox3.ItemIndex:=ListBox1.ItemIndex;
end;
procedure TForm1.ListBox2Click(Sender: TObject);
begin
ListBox1.ItemIndex:=ListBox2.ItemIndex;
ListBox3.ItemIndex:=ListBox2.ItemIndex;
end;
procedure TForm1.ListBox3Click(Sender: TObject);
begin
ListBox1.ItemIndex:=ListBox3.ItemIndex;
ListBox2.ItemIndex:=ListBox3.ItemIndex;
end;
end.
Результаты выполнения программы:
Задание 2.3. Текст модуля
unit Unit1;