Здравствуйте!
Есть задача об оптимальной замене оборудования.Я ее почти сделал только не могу вывести итоговую таблицу, где будет показана в каком году нужно оставить оборудование или заменить его.
Как можно из листбокса отправить данные в стрингрид и заменить их на букву?
Заранее спасибо!
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Grids;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
GroupBox1: TGroupBox;
Button4: TButton;
Button3: TButton;
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
GroupBox2: TGroupBox;
StringGrid1: TStringGrid;
GroupBox3: TGroupBox;
StringGrid2: TStringGrid;
TabControl1: TTabControl;
ListBox1: TListBox;
Label3: TLabel;
Label4: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure TabControl1Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
tabl: array of array of Integer;
f: array of array of Integer;
yt: array of array of string;
c: integer;
implementation
{$R *.dfm}
function Max(a,b: integer): Integer;
begin
if a>b then
Result:=a
else
Result:=b;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
stringgrid1.Cells[0,1]:='R(t)';
stringgrid1.Cells[0,2]:='Z(t)';
stringgrid2.Cells[0,1]:='U';
button1.Enabled:=false;
stringgrid1.enabled:=false;
button4.Enabled:=false;
button2.Enabled:=false;
end;
//Кноака расчитать
procedure TForm1.Button1Click(Sender: TObject);
var a: array of Integer;
i,j,n,a0,d: Integer;
begin
c:=strtoint(Edit1.Text);
SetLength(tabl,StringGrid1.ColCount-1);
for i:=0 to Length(tabl)-1 do
SetLength(tabl[i],StringGrid1.RowCount-1);
SetLength(a,Length(tabl)-1);
for i:=0 to Length(tabl)-1 do
for j:=0 to Length(tabl[i])-1 do
tabl[i,j]:=strtoint(StringGrid1.Cells[i+1,j+1]);
a0:=tabl[0,0]-tabl[0,1]-c;
for i:=Length(a)-1 downto 0 do
a[i]:=tabl[i,0]-tabl[i,1];
SetLength(f,Length(a)-1);
for i:=Length(f)-1 downto 0 do
SetLength(f[i],i+1);
//Формула
for i:=Length(f)-1 downto 0 do
for j:=Length(f[i])-1 downto 0 do
begin
if i=Length(f)-1 then
f[i,j]:=Max(a[j+1],a0)
else
f[i,j]:=Max(a[j+1]+f[i+1,j+1],a0+f[i+1,0]);
end;
//Заполнение
TabControl1.Tabs.Clear;
for i:=0 to Length(f)-1 do
TabControl1.Tabs.Add('f('+inttostr(i+2)+')');
TabControl1.TabIndex:=0;
Label3.Caption:=inttostr(TabControl1.TabIndex);
ListBox1.Clear;
for i:=0 to Length(f[0])-1 do
ListBox1.Items.Add(inttostr(f[i,0]));
Label4.Caption:='F(max) = '+inttostr(a0+c+f[0,0]);
button1.Enabled:=false;
button2.Enabled:=true;
edit1.Enabled:=false;
button4.Enabled:=false;
end;
procedure TForm1.TabControl1Change(Sender: TObject);
var i: integer;
begin
ListBox1.Clear;
Label3.Caption:=inttostr(TabControl1.TabIndex);
for i:=0 to Length(f[TabControl1.TabIndex])-1 do
ListBox1.Items.Add(inttostr(f[TabControl1.TabIndex,i]));
end;
//Кнопка повтор
procedure TForm1.Button2Click(Sender: TObject);
var
i,k:Integer;
begin
stringgrid1.ColCount:=2;
stringgrid1.Cells[0,1]:='R(t)';
stringgrid1.Cells[0,2]:='Z(t)';
stringgrid1.Cells[1,0]:='';
stringgrid1.Cells[1,1]:='';
stringgrid1.Cells[1,2]:='';
stringgrid2.ColCount:=2;
stringgrid2.Cells[0,1]:='U';
stringgrid2.Cells[1,0]:='';
TabControl1.Tabs.Clear;
ListBox1.Clear;
label4.caption:='';
label3.caption:='';
button1.Enabled:=false;
button2.Enabled:=false;
button3.Enabled:=true;
edit1.Enabled:=true;
edit2.Enabled:=true;
button4.Enabled:=false;
for i:=0 to StringGrid1.ColCount do
StringGrid1.Cells[i+1,0]:=inttostr(i);
end;
procedure TForm1.Button3Click(Sender: TObject);
var i,h:integer;
begin
stringgrid1.enabled:=true;
h:=strtoint(edit2.text);
edit1.Enabled:=false;
edit2.Enabled:=false;
for i:=0 to h do
StringGrid1.Cells[i+1,0]:=inttostr(i);
for i:=0+1 to h do
stringgrid1.ColCount:=stringgrid1.ColCount+1;
button1.Enabled:=true;
button3.Enabled:=false;
button4.Enabled:=true;
for i:=1 to h do
StringGrid2.Cells[i,0]:=inttostr(i);
for i:=0+1 to h do
stringgrid2.ColCount:=stringgrid1.ColCount-1;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
stringgrid1.ColCount:=2;
stringgrid1.Cells[0,1]:='R(t)';
stringgrid1.Cells[0,2]:='Z(t)';
stringgrid1.Cells[1,0]:='';
stringgrid2.ColCount:=2;
stringgrid2.Cells[0,1]:='U';
stringgrid2.Cells[1,0]:='';
stringgrid1.enabled:=false;
stringgrid2.enabled:=false;
edit1.Enabled:=true;
edit2.Enabled:=true;
button1.Enabled:=false;
button4.Enabled:=false;
button3.Enabled:=true;
end;
end.