Задачка: погрузить груз заданной массы из таблицы на судно определенного тоннажа, вводимого или набираемого в ComboBox. Суть в том, что если тоннаж больше заданной величины груза в ячейки, то он должен перейти и погрузить груз из следующий и так далее пока не заполниться, а в ячейках с забранным грузом проставлять нули. Проблема заключается в том, что не всегда корректно происходит погрузка на разных значения и шаг не всегда происходит и еще вылезает постоянно знак минус. Как сделать добавление и удаление записей в таблице?
unit Unit1;

interface

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

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Button2: TButton;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Button3: TButton;
ComboBox1: TComboBox;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[0,0]:='Номер состава';
StringGrid1.Cells[0,1]:='434';
StringGrid1.Cells[0,2]:='285';
StringGrid1.Cells[0,3]:='013';
StringGrid1.Cells[0,4]:='202';

StringGrid1.Cells[1,0]:='Длина';
StringGrid1.Cells[1,1]:='990';
StringGrid1.Cells[1,2]:='540';
StringGrid1.Cells[1,3]:='990';
StringGrid1.Cells[1,4]:='680';

StringGrid1.Cells[2,0]:='Масса';
StringGrid1.Cells[2,1]:='340';
StringGrid1.Cells[2,2]:='200';
StringGrid1.Cells[2,3]:='150';
StringGrid1.Cells[2,4]:='100';

StringGrid1.Cells[3,0]:='Тип';
StringGrid1.Cells[3,1]:='Золота';
StringGrid1.Cells[3,2]:='Серебро';
StringGrid1.Cells[3,3]:='Медь';
StringGrid1.Cells[3,4]:='Аллюминий';

StringGrid1.Cells[4,0]:='Кол-во вагонов';
StringGrid1.Cells[4,1]:='30';
StringGrid1.Cells[4,2]:='20';
StringGrid1.Cells[4,3]:='50';
StringGrid1.Cells[4,4]:='15';

StringGrid1.Cells[5,0]:='Скорость';
StringGrid1.Cells[5,1]:='67';
StringGrid1.Cells[5,2]:='83';
StringGrid1.Cells[5,3]:='51';
StringGrid1.Cells[5,4]:='90';

end;

procedure TForm1.Button1Click(Sender: TObject);
var x,y,y1,y2,y3:integer;
a,a1,a2,a3:integer;
begin
x:=strtoint(ComboBox1.Text); //strtoint(Edit1.text);
y:=strtoint(StringGrid1.Cells[2,1]);
a:=(x-y);
if y<=x then StringGrid1.Cells[2,1]:='0';
if y>=x then StringGrid1.Cells[2,1]:=inttostr(a);
Edit2.Text:=inttostr(a);
if a=0 then exit;
if a<0 then exit;

begin
a:=strtoint(Edit2.Text);
//if a>0 then a:=a*(-1);
y1:=strtoint(StringGrid1.Cells[2,2]);
a1:=(a-y1);
if y1<=a1 then StringGrid1.Cells[2,2]:='0';
if y1>=a1 then StringGrid1.Cells[2,2]:=inttostr(a1);
Edit2.Text:=inttostr(a1);
if a1=0 then exit;
if a1<0 then exit;

begin
a1:=strtoint(Edit2.Text);
if a1>0 then a1:=a1*(-1);
y2:=strtoint(StringGrid1.Cells[2,3]);
a2:=(a1-y2);
if y2<=a1 then StringGrid1.Cells[2,3]:='0';
if y2>=a1 then StringGrid1.Cells[2,3]:=inttostr(a2);
Edit2.Text:=inttostr(a2);
if a2=0 then exit;
if a2<0 then exit;

begin
a2:=strtoint(Edit2.Text);
if a2>0 then a2:=a2*(-1);
y3:=strtoint(StringGrid1.Cells[2,4]);
a3:=(a2-y3);
if y3<=a2 then StringGrid1.Cells[2,4]:='0';
if y3>=a2 then StringGrid1.Cells[2,4]:=inttostr(a2);
end;
end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
StringGrid1.Cells[0,0]:='Номер состава';
StringGrid1.Cells[0,1]:='434';
StringGrid1.Cells[0,2]:='285';
StringGrid1.Cells[0,3]:='013';
StringGrid1.Cells[0,4]:='202';

StringGrid1.Cells[1,0]:='Длина';
StringGrid1.Cells[1,1]:='990';
StringGrid1.Cells[1,2]:='540';
StringGrid1.Cells[1,3]:='990';
StringGrid1.Cells[1,4]:='680';

StringGrid1.Cells[2,0]:='Масса';
StringGrid1.Cells[2,1]:='340';
StringGrid1.Cells[2,2]:='200';
StringGrid1.Cells[2,3]:='150';
StringGrid1.Cells[2,4]:='100';

StringGrid1.Cells[3,0]:='Тип';
StringGrid1.Cells[3,1]:='Золота';
StringGrid1.Cells[3,2]:='Серебро';
StringGrid1.Cells[3,3]:='Медь';
StringGrid1.Cells[3,4]:='Аллюминий';

StringGrid1.Cells[4,0]:='Кол-во вагонов';
StringGrid1.Cells[4,1]:='30';
StringGrid1.Cells[4,2]:='20';
StringGrid1.Cells[4,3]:='50';
StringGrid1.Cells[4,4]:='15';

StringGrid1.Cells[5,0]:='Скорость';
StringGrid1.Cells[5,1]:='67';
StringGrid1.Cells[5,2]:='83';
StringGrid1.Cells[5,3]:='51';
StringGrid1.Cells[5,4]:='90';

Edit2.Clear;
end;

end.