📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Grids, Buttons;
type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
Button1: TButton;
Panel3: TPanel;
Label4: TLabel;
StringGrid1: TStringGrid;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Panel4: TPanel;
StringGrid2: TStringGrid;
Label5: TLabel;
Label6: TLabel;
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type array2r=array of array of real;
type array2i=array of array of integer;
//声明的两种动态二维数组类型
var
Form1: TForm1;
n:integer;
a:real;
tt:integer;
i1,ii:array2r;
o1,oo:array2r;
c1,cc:array2r;
g:array2r;
x:array2i;
temp1:integer;
temp2:integer;
temp3:integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i,j:integer;
begin
n:=strtoint(edit1.Text); //计划的年限总数
a:=strtofloat(edit2.Text); //折扣因子,0≤a ≤1
tt:=strtoint(edit3.text); //在第一年开始时,正在使用的机器的役龄
temp3:=round(n*(n+1)/2); //临时变量,用于辅助处理输入数据
stringgrid1.RowCount:=4;
stringgrid1.ColCount:=temp3+n+1;
stringgrid2.RowCount:=3;
stringgrid2.ColCount:=n+1; //设定两个stringgrid控件的行列数
SetLength(i1,n+1,n+1);
SetLength(o1,n+1,n+1);
SetLength(c1,n+1,n+1);
SetLength(ii,n+1,n+1);
SetLength(oo,n+1,n+1);
SetLength(cc,n+1,n+1);
SetLength(g,n+2,n+2);
SetLength(x,n+1,n+1); //设定所用动态数组的长度
temp1:=0;
temp2:=0; //两临时变量
for i:=1 to n do
begin
temp1:=temp2+1;
temp2:=temp2+n+1-i;
for j:=temp1 to temp2 do
stringgrid1.Cells[j,0]:=inttostr(j-temp1);
end;
for j:=temp3+1 to temp3+n do
stringgrid1.Cells[j,0]:=inttostr(j-temp3);
stringgrid1.Cells[0,1]:='收入';
stringgrid1.Cells[0,2]:='运行费用';
stringgrid1.Cells[0,3]:='更新费用';
//对stringgrid1控件进行边框文字提示
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i,j:integer;
begin
temp1:=0;
temp2:=0;
for i:=1 to n do
begin
temp1:=temp2+1;
temp2:=temp2+n+1-i;
for j:=temp1 to temp2 do
begin
i1[i,j-temp1]:=strtofloat(stringgrid1.cells[j,1]);
o1[i,j-temp1]:=strtofloat(stringgrid1.cells[j,2]);
c1[i,j-temp1]:=strtofloat(stringgrid1.cells[j,3]);
end;
end;
for j:=temp3+1 to temp3+n do
begin
i1[0,j-temp3]:=strtofloat(stringgrid1.Cells[j,1]);
o1[0,j-temp3]:=strtofloat(stringgrid1.Cells[j,2]);
c1[0,j-temp3]:=strtofloat(stringgrid1.Cells[j,3]);
end;
//读取输入的有关数据,分期(按第几年)进行存放到临时数组
for i:=1 to n do
for j:=0 to i do
begin
ii[i,j]:=i1[i-j,j];
oo[i,j]:=o1[i-j,j];
cc[i,j]:=c1[i-j,j];
end;
//将临时数组中的数据转化到各实际数组
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
var
j:integer;
t:integer;
begin
for t:=1 to n+1 do
g[n+1,t]:=0;
//因只研究n年情况,根据约定,后续年代收入值赋0
for j:=n downto 1 do
for t:=1 to j+tt-1 do
begin
g[j,t]:=ii[j,0]-oo[j,0]-cc[j,t]+a*g[j+1,1];
x[j,t]:=-1;
//默认是进行设备更新
if ii[j,t]-oo[j,t]+a*g[j+1,t+1]>g[j,t] then
begin
g[j,t]:=ii[j,t]-oo[j,t]+a*g[j+1,t+1];
x[j,t]:=1;
end;
//如果保持的效益更大,则采取保持不变
end;
stringgrid2.Cells[0,0]:='年';
stringgrid2.Cells[0,1]:='机龄';
stringgrid2.Cells[0,2]:='最佳策略';
for j:=1 to n do
stringgrid2.Cells[j,0]:=inttostr(j);
//对stringgrid2控件进行边框提示
stringgrid2.Cells[1,1]:=inttostr(1);
if x[1,1]=1 then
stringgrid2.Cells[1,2]:='保持'
else stringgrid2.Cells[1,2]:='更新';
//对第一年进行初始化数据
t:=1;
for j:=1 to n-1 do
begin
if x[j,t]=1 then
begin
t:=t+1;
stringgrid2.cells[j+1,1]:=inttostr(t);
if x[j+1,t]=1 then stringgrid2.Cells[j+1,2]:='保持'
else stringgrid2.Cells[j+1,2]:='更新';
end
else
begin
t:=1;
stringgrid2.cells[j+1,1]:=inttostr(t);
if x[j+1,t]=1 then stringgrid2.Cells[j+1,2]:='保持'
else stringgrid2.Cells[j+1,2]:='更新';
end;
end;
//进行反推,求得最佳方案中各年的机龄和最佳策略(是保持还是更新)
label6.Caption:='相应的最佳收益为:'+floattostr(g[1,1])+#13;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -