⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 用delphi语言实现运筹学排序问题(双工序)程序
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Grids, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Panel3: TPanel;
    Label2: TLabel;
    StringGrid1: TStringGrid;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Panel4: TPanel;
    Label3: TLabel;
    StringGrid2: TStringGrid;
    Label4: 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;  //自定义的二维动态数组
var
  Form1: TForm1;
  n:integer;
  a:array2r;
  b:array2r;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  j:integer;
begin
  n:=strtoint(edit1.Text);         //待加工的工件总数
  stringgrid1.ColCount:=n+1;
  stringgrid2.ColCount:=n+1;       //设定两个stringgrid控件的列数(行数默认为3)
  for j:=1 to n do
    stringgrid1.Cells[j,0]:=inttostr(j);
  stringgrid1.Cells[0,0]:='工件';
  stringgrid1.Cells[0,1]:='工序A';
  stringgrid1.Cells[0,2]:='工序B';
  stringgrid2.Cells[0,0]:='工件';
  stringgrid2.Cells[0,1]:='工序A';
  stringgrid2.Cells[0,2]:='工序B';
  //完成边框提示
  setlength(a,3,n+1);
  setlength(b,3,n+1);
  //设定动态数组的长度
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  i,j:integer;
begin
  for i:=0 to 2 do
    for j:=1 to n do
      a[i,j]:=strtofloat(stringgrid1.Cells[j,i]);
end;
//读取输入的工件与加工时间关系表(包括工件序号)

procedure TForm1.BitBtn2Click(Sender: TObject);
var
  i,j:integer;
  p,q:integer;  //临时变量,用于记录当前最小元素的行列号
  x,y:integer;  //临时变量,用于向新的数组导出数值
  min:real;     //记录原始数组中的最小值
  temp1,temp2,temp:real; //临时变量,用于求解总的加工时间
  zong:real;    //总的加工时间
label 1;        //自定义的一个标签,与goto配套使用
begin
  x:=0;
  y:=n+1;       //用于对b进行赋值.(x代表从前往后,y代表从后往前)
1:min:=a[1,1];  //给最小值赋初值
  p:=0;
  q:=0;         //变量赋初值,无其他意义,只为防止出现意想不到的错误
  for i:=1 to 2 do
  for j:=1 to n do
    if a[i,j]<=min then
    begin
      min:=a[i,j];
      p:=i;
      q:=j;
    end;
    //求出当前原始数组的最小值并记录其行列号

  if p=1 then           //当该元素位于第一行时,将其从b的前端插入
  begin
    x:=x+1;
    b[0,x]:=a[0,q];
    b[1,x]:=a[p,q];
    b[2,x]:=a[p+1,q];   //赋b值
    a[p,q]:=10000;
    a[p+1,q]:=10000;    //把已映射过来的列置为无穷大,相当于把它剔除掉
  end
  else  if p=2 then     //当该元素位于第二行时,将其从b的后端插入
  begin
    y:=y-1;
    b[0,y]:=a[0,q];
    b[1,y]:=a[p-1,q];
    b[2,y]:=a[p,q];
    a[p-1,q]:=10000;
    a[p,q]:=10000;
  end;

  if y-x>1 then goto 1;  //循环的条件和中止条件
                         //当a中元素已全部映射过来时,跳出循环,进行结果输出
  temp:=0;
  temp1:=0;
  temp2:=0;
  for j:=1 to n-1 do
  begin
    temp1:=temp1+b[1,j+1];
    temp2:=temp2+b[2,j];
  end;
  if temp1>temp2 then temp:=temp1
  else temp:=temp2;
  //特别说明:本例中未考虑各工件间有等待时间的情形,那是排队论的问题

  zong:=temp+b[1,1]+b[2,n];  //总的加工时间

  label4.Caption:='最优加工顺序为:  ';
  for i:=0 to 2 do
    for j:=1 to n do
      stringgrid2.cells[j,i]:=floattostr(b[i,j]);
  for j:=1 to n do
    label4.caption:=label4.caption+floattostr(b[0,j])+'  ';
  label4.Caption:=label4.Caption+#13;
  label4.Caption:=label4.Caption+'总加工时间为:'+floattostr(zong)+#13;
 //输出计算结果
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -