Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Методичні вказівки з курсової роботи AтаПЗОВПвТ....doc
Скачиваний:
7
Добавлен:
05.12.2018
Размер:
7.49 Mб
Скачать

Текст програми на мові Delphi, яка реалізує симплексний метод рішення тз

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, Grids;

type

TForm1 = class(TForm)

IsGrid: TStringGrid;

Button1: TButton;

StringGrid1: TStringGrid;

AGrid: TStringGrid;

BGrid: TStringGrid;

Button2: TButton;

Label1: TLabel;

Label2: TLabel;

Label3: TLabel;

Label4: TLabel;

Label5: TLabel;

Button3: TButton;

Button4: TButton;

Button5: TButton;

Button6: TButton;

Edit2: TEdit;

Label6: TLabel;

Label7: TLabel;

Edit3: TEdit;

Label8: TLabel;

Label9: TLabel;

Label10: TLabel;

Label11: TLabel;

Label12: TLabel;

Label13: TLabel;

Label14: TLabel;

Label15: TLabel;

Edit1: TEdit;

Edit4: TEdit;

Edit5: TEdit;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure Button4Click(Sender: TObject);

procedure Button5Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

procedure Button6Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1 : TForm1;

c:array [1..10,1..11] of real;

i, j, k1, n, k, m, ku, kx : integer;

st, st1 : array [1..100,1..100] of real;

a, b : array [1..10] of real;

max, min, sum : real;

i1, j1, iteraciya : integer;

ind : boolean;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

begin

for i:=1 to m do

for j:=1 to n do

c[i,j] := strtofloat(IsGrid.cells[j-1,i-1]);

for i:=1 to m do

a[i]:=strtofloat(AGrid.cells[0,i-1]);

for i:=1 to n do

b[i]:=strtofloat(BGrid.cells[i-1,0]);

///////////первая строка

sum:=0;

k1:=1;

for i:=1 to m do

for j:=1 to n do

begin

st[1,3+k1]:=c[i,j];

sum:=sum+c[i,j];

k1:=k1+1;

end;

for i:=1 to ku do

st[1,3+m*n+i]:=sum;

///////////////следующие m-строк

for i:=1 to m do

for j:=1 to n do

st[1+i,3+j+(i-1)*n]:=1;

//////////следующие (n-1)-строк

for i:=1 to (n-1) do

for j:=1 to m do

st[1+m+i,4+(i-1)+(j-1)*n]:=1;

///////////первый столбец

for i:=1 to ku do

st[1+i,1]:=sum;

///////////второй столбец

for i:=1 to ku do

st[1+i,2]:=m*n+i;

///////////третий столбец

for i:=1 to m do

st[1+i,3]:=a[i];

for i:=1 to (n-1) do

st[1+m+i,3]:=b[i];

/////////доп.перем.

for i:=1 to ku do

st[1+i,3+m*n+i]:=1;

////////разнесение элементов массива st в StringGrid1

for i:=1 to (1+ku) do

for j:=1 to (3+m*n+ku) do

stringgrid1.Cells[j-1,i-1]:=currtostr(st[i,j]);

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

st[2+ku,1]:=0;

for i:=1 to ku do

st[2+ku,1]:=st[2+ku,1]+st[1+i,1]*st[1+i,3];

for j:=1 to (m*n+ku) do

begin

st[2+ku,3+j]:=0;

for i:=1 to ku do

st[2+ku,3+j]:=st[2+ku,3+j]+st[1+i,1]*st[1+i,3+j];

st[2+ku,3+j]:=st[2+ku,3+j]-st[1,3+j];

end;

for i:=1 to (2+ku) do

for j:=1 to (3+m*n+ku) do

stringgrid1.Cells[j-1,i-1]:=currtostr(st[i,j]);

Edit5.Top := 88 + 25*(ku+1);

Edit5.Text := ' ' + currtostr(st[2+ku,1]);

ind:=false;

for i:=1 to (m*n+ku) do

if (st[2+ku,3+i]>0) then

ind:=true;

if (ind) then

label6.Caption:='Решение не оптимально'

else

label6.Caption:='Решение оптимально';

end;

procedure TForm1.Button3Click(Sender: TObject);

begin

Close

end;

procedure TForm1.Button4Click(Sender: TObject);

begin

m:=StrToInt(Edit1.Text);

n:=StrToInt(Edit2.Text);

Agrid.RowCount:=m;

Bgrid.ColCount:=n;

IsGrid.RowCount:=m;

IsGrid.ColCount:=n;

ku:= m+n-1;

kx:= ku+m*n;

StringGrid1.RowCount:=2+ku;

StringGrid1.ColCount:=3+m*n+ku;

if (m=2) and (n=4) then

begin

with Agrid do

begin

Cells[0,0] := '20';

Cells[0,1] :='100';

end;

with Bgrid do

begin

Cells[0,0]:='40'; Cells[1,0]:='10'; Cells[2,0]:='50'; Cells[3,0]:='20';

end;

with Isgrid do

begin

Cells[0,0]:='5'; Cells[1,0]:='3'; Cells[2,0]:='3'; Cells[3,0]:='3';

Cells[0,1]:='4'; Cells[1,1]:='2'; Cells[2,1]:='2'; Cells[3,1]:='3';

end;

end;

if (m=2) and (n=3) then

begin

with Agrid do

begin

Cells[0,0] :='70';

Cells[0,1] :='80';

end;

with Bgrid do

begin

Cells[0,0]:='40'; Cells[1,0]:='50'; Cells[2,0]:='60';

end;

with Isgrid do

begin

Cells[0,0]:='1'; Cells[1,0]:='2'; Cells[2,0]:='4';

Cells[0,1]:='2'; Cells[1,1]:='5'; Cells[2,1]:='3';

end;

end;

if (m=3) and (n=4) then

begin

with Agrid do

begin

Cells[0,0] :='80';

Cells[0,1] :='100';

Cells[0,2] :='105';

end;

with Bgrid do

begin

Cells[0,0]:='55';

Cells[1,0]:='60';

Cells[2,0]:='75';

Cells[3,0]:='95';

end;

with Isgrid do

begin

Cells[0,0]:='5'; Cells[1,0]:='7'; Cells[2,0]:='8'; Cells[3,0]:='6';

Cells[0,1]:='3'; Cells[1,1]:='4'; Cells[2,1]:='6'; Cells[3,1]:='4';

Cells[0,2]:='5'; Cells[1,2]:='6'; Cells[2,2]:='6'; Cells[3,2]:='5';

end;

end;

end;

procedure TForm1.Button5Click(Sender: TObject);

label 1;

begin

////////замена i1 строки на j1 столбец

st[i1,1]:=st[1,j1];

st[i1,2]:=j1-3;

////////пересчет таблицы

for i:=2 to (ku+1) do

for j:=3 to (m*n+ku+3) do

if (i=i1) then

st1[i,j]:=st[i,j]/st[i1,j1]

else

st1[i,j]:=st[i,j]-st[i1,j]/st[i1,j1]*st[i,j1];

for i:=2 to (ku+1) do

for j:=3 to (m*n+ku+3) do

st[i,j]:=st1[i,j];

{delta 0}

st[2+ku,1]:=0;

for i:=1 to ku do

st[2+ku,1]:=st[2+ku,1]+st[1+i,1]*st[1+i,3];

{индексная строка}

for i:=1 to (m*n+ku) do

begin

st[2+ku,3+i]:=0;

for j:=1 to ku do

st[2+ku,3+i]:=st[2+ku,3+i]+st[1+j,1]*st[1+j,3+i];

st[2+ku,3+i]:=st[2+ku,3+i]-st[1,3+i];

end;

Edit5.Top := 88 + 25*(ku+1);

Edit5.Text := ' ' + currtostr(st[2+ku,1]);

////////////проверка на оптимальность

ind:=false;

for i:=1 to (m*n+ku) do

if (st[2+ku,3+i]>0) then

ind:=true;

if (ind) then

begin

label6.Caption:='Решение не оптимально';

iteraciya:=iteraciya+1;

end

else

begin

label6.Caption:='Решение оптимально'+#13+'произведено'+' '+inttostr(iteraciya+1)+' '+'итерации';

button2.Enabled:=false;

button5.Enabled:=false;

button6.Enabled:=false;

label7.Caption := '';

end;

for i:=1 to (2+ku) do

for j:=1 to (m*n+ku+3) do

stringgrid1.Cells[j-1,i-1]:=currtostr(st[i,j]);

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

iteraciya:=0;

end;

procedure TForm1.Button6Click(Sender: TObject);

begin

max:=-9999;

for j:=4 to (3+m*n) do

if st[ku+2,j]>max then

begin

max:=st[ku+2,j];

j1:=j;

end;

///////////////////min

min:=9999;

for i:=2 to ku+1 do

if (st[i,j1]>0) then

if (st[i,3]/st[i,j1]<min) then

begin

min:=st[i,3]/st[i,j1];

i1:=i;

end;

label7.Caption:='max = '+currtostr(max)+' j1 = '+inttostr(j1)+#13+'min = '+currtostr(st[i1,3])+' i1= '+inttostr(i1);

end;

end.

Додаток 17