?? unit1.pas
字號:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Mask, ExtCtrls, Grids, Buttons;
const long=250;//定義常量控制數組長度
type
TForm1 = class(TForm)
Panel3: TPanel;
Panel2: TPanel;
Label1: TLabel;
Label2: TLabel;
StringGrid1: TStringGrid;
Panel1: TPanel;
RadioGroup1: TRadioGroup;
Panel4: TPanel;
StringGrid2: TStringGrid;
Edit1: TEdit;
Edit2: TEdit;
Panel5: TPanel;
BitBtn1: TBitBtn;
Panel6: TPanel;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
var
VarNum,ConNum:integer;//定義整形變量,存放變量個數和約束條件個數}
leixing:String;//存放目標函數類型
x:array[1..long] of integer;//存放變量的值
z:array of real;//存放每次枚舉的函數值
meijucishu:integer;//枚舉次數,即所有取值的可能組合的個數
biaoshi:array of char;//用于標識當前組合是否是可行解
a:array[1..long,1..long] of real;//存放系數矩陣
b:array[1..long] of real;//存放限定向量
c:array[1..long] of real;//存放目標函數系數
opt:array[1..long] of integer;//存放操作符
implementation
uses Unit2;//調用unit2單元的數據
{$R *.DFM}
procedure chushihuabianliang;
var i,j:Integer;
begin
for i:=1 to long do
for j:=1 to long do
a[i,j]:=0;
for i:=1 to long do
begin
b[i]:=0;
opt[i]:=0;
end;
for j:=1 to long do
begin
c[j]:=0;
x[j]:=0;
end;
end;
{------------初始化變量-------------------------}
procedure TForm1.BitBtn2Click(Sender: TObject);
var i,j:integer;
heli:Boolean;//判斷輸入的數據是否合理
begin
heli:=true;//賦初值
try
VarNum:=strtoint(edit1.text);
ConNum:=strtoint(edit2.text); {輸入變量個數和約束條件個數}
except
begin
heli:=False;
showmessage('輸入有誤!'+#13+'請確定您輸入的是整數并且沒有空格');
//數據的合理性檢驗
exit;
end;
end;
if not heli then exit;
stringgrid1.ColCount:=VarNum+1;
stringgrid1.Cells[0,0]:='變量';
stringgrid1.Cells[0,1]:='系數';
//表格1的列數=變量數+1; (第一列用作標簽) }
for i:=1 to VarNum do
begin
stringgrid1.Cells[i,0]:='X'+inttostr(i);
stringgrid1.Cells[i,1]:='';
end;
//表格1外觀,第一行顯示變量名,第二行用來輸入系數,這里賦初值為0
//第一行、第一列用作標簽}
stringgrid2.ColCount:=VarNum+2;
stringgrid2.RowCount:=ConNum+1;
//表格2的列數=變量數+2;(最后兩列為運算符及常數項)
//表格2的行數=約束條件數+1;(第一行用作標簽)}
for i:=0 to VarNum-1 do
stringgrid2.Cells[i,0]:='X'+inttostr(i+1);
//表格2第一行,顯示變量名X1,X2,…
stringgrid2.Cells[VarNum,0]:= '運算符';
//表格2第一行倒數第二列,顯示約束條件中的運算符
stringgrid2.Cells[VarNum+1,0]:='b';
//表格2第一行最后一列,顯示約束條件的常數項b
for j:=1 to ConNum do
for i:=0 to VarNum-1 do
stringgrid2.Cells[i,j]:='';
//約束條件系數矩陣賦初值為空,等待用戶輸入
for j:=1 to ConNum do
begin
stringgrid2.Cells[VarNum,j]:= '';
//表格2倒數第二列,用來輸入約束條件中的符號,默認為等號
stringgrid2.Cells[VarNum+1,j]:='';
//表格2最后一列,用來輸入約束條件中的常數項b,默認為空}
end;
StringGrid1.SetFocus;
end;
{-----------設置初始輸入表格------------------------}
procedure duqushuju;
var i,j:Integer;
begin
for i:=1 to ConNum do
for j:=0 to VarNum-1 do
if Form1.StringGrid2.Cells[j,i]<>'' then
a[i,j+1]:=StrToFloat(Form1.StringGrid2.Cells[j,i]);
//讀取0-1整數規劃的系數矩陣
for i:=1 to ConNum do
if Form1.StringGrid2.Cells[VarNum+1,i]<>'' then
b[i]:=StrToFloat(Form1.StringGrid2.Cells[varnum+1,i]);
//讀取0-1整數規劃的限定向量
for i:=1 to ConNum do
if Form1.StringGrid2.Cells[VarNum,i]<>'' then
opt[i]:=StrToInt(Form1.StringGrid2.Cells[varnum,i]);
//讀取0-1整數規劃的操作符,用-1,0,1分別表示小于、等于、大于
for j:=1 to VarNum do
if Form1.StringGrid1.Cells[j,1]<>'' then
c[j]:=StrToFloat(Form1.StringGrid1.Cells[j,1]);
//讀取0-1整數規劃目標函數系數
end;
{-----------自定義過程讀取初始化數據------------------}
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
chushihuabianliang;
//調用自定義過程初始化變量
RadioGroup1Click(Sender);//改變目標函數類型
duqushuju;//讀取輸入的數據
form2.show;
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
if RadioGroup1.ItemIndex=0 then
leixing:='Max'
else
leixing:='Min';
//使目標函數的類型不斷更新
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
Edit1.SetFocus;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -