Скачиваний:
15
Добавлен:
15.06.2014
Размер:
36.86 Кб
Скачать

Отчет по лаб.раб. №5 ст.гр.№552002 Довнара Д.О.

4. Создать стек из случайных целых чисел и поменять местами крайние элементы.

unit Unit1;

var

Form1: TForm1;

Lst: TList;

procedure TForm1.Button1Click(Sender: TObject);

var

c: integer;

begin

c:=StrToInt(Edit1.Text);

Listbox1.Items.Add(Edit1.Text);

Lst.Addk(c);

inc(Lst.nm_zap);

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

Lst:=Tlist.Create;

Lst.nm_zap:=0;

end;

procedure TForm1.Button2Click(Sender: TObject);

var

a,b:integer;

begin

Lst.Readspk(a);

Lst.Readsp1(b);

Lst.Add1(a);

Lst.Addk(b);

Listbox1.Clear;

Lst.Print(ListBox1);

end;

procedure TForm1.Button3Click(Sender: TObject);

var

c:integer;

begin

Randomize;

c:=Random(100);

Listbox1.Items.Add(inttostr(c));

Lst.Addk(c);

inc(Lst.nm_zap);

end;

procedure TForm1.Button4Click(Sender: TObject);

begin

Edit2.Text:=IntToStr(Lst.Ariphm);

end;

procedure TForm1.Button5Click(Sender: TObject);

begin

Edit2.Text:=IntToStr(Lst.Quadr);

end;

unit Unit2;

interface

uses stdctrls, SysUtils;

type

TInf=integer;

PSel=^Sel;

Sel=Record

inf:TInf;

A:PSel;

end;

TList=Class(TObject)

sp1,spk,sp:PSel;

nm_zap:integer;

Constructor Create;

Procedure Addk(inf:Tinf);

Procedure Add1(inf:Tinf);

Procedure Readsp1(var Inf:Tinf);

Procedure Readspk(var Inf:Tinf);

Procedure Print(ListB1:TListBox);

end;

implementation

Constructor TList.Create;

begin

inherited create;

sp1:=nil;

spk:=nil;

end;

Procedure TList.Addk(inf:Tinf);

begin

if spk=Nil then

begin

New(spk);

spk^.A:=Nil;

spk^.Inf:=inf;

sp1:=spk;

end

else

begin

New(spk^.A);

spk:=spk^.A;

spk^.Inf:=Inf;

spk^.A:=Nil;

end;

end;

Procedure TList.Add1(inf:Tinf);

begin

if sp1=nil then

begin

New(sp1);

spk:=sp1;

sp^.inf:=inf;

sp1^.A:=nil;

end;

New(sp);

Sp^.inf:=inf;

Sp^.A:=Sp1;

Sp1:=Sp;

end;

Procedure TList.Readsp1(var Inf:Tinf);

begin

if sp1<>Nil then

begin

Inf:=sp1^.Inf;

sp:=sp1;

sp1:=sp1^.A;

if sp1=Nil then spk:=Nil;

Dispose(sp);

end;

end;

Procedure TList.Readspk(var Inf:Tinf);

begin

if spk<>Nil then

begin

Inf:=spk.Inf;

if sp1=spk then

begin

Dispose(spk);

sp1:=Nil;

spk:=Nil

end

else

begin

sp:=sp1;

while sp^.A<>Spk do sp:=sp^.A;

Dispose(spk);

spk:=sp;

spk^.A:=Nil;

end;

end;

end;

Procedure TList.Print(ListB1:TListBox);

begin

sp:=sp1;

while sp<>nil do

begin

ListB1.Items.Add(inttostr(Sp^.inf));

sp:=sp^.A;

end;

end;

Function TList.Ariphm:integer;

var

box:PSel;

sum,i:Integer;

begin

box:=spk;

i:=0; sum:=0;

while box<>nil do begin

Inc(i);

sum:=sum+box.inf;

box:= box.a;

end;

Result:=sum div i;

end;

Function TList.Quadr:integer;

var

box:PSel;

i,ari:Integer;

sum:Extended;

begin

ari:=Ariphm;

box:=sp1;

i:=0; sum:=0;

while box<>nil do begin

Inc(i);

sum:=sum+Sqr(box.inf-ari);

box:= box.a;

end;

sum:=sum/i;

Result:=Round(Sqrt(sum));

end;

end.

Соседние файлы в папке чётн,нечётн, добавление эелемента