Дана действительная матрица NxM. Необходимо найти сумму положительных элементов матрицы. использовать stringgrid — Delphi(Делфи)

На форме расположены StringGrid1, Edit1, Edit2, Edit3 и Button1.

В Edit1 вводится кол-во элементов N матрицы

В Edit2 вводится кол-во элементов M матрицы

Матрица вводится вручную как в Exel

При нажатии на Button1, в Edit3 выводится сумма положительных элементов матрицы.

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if Edit1.Text <> '' then
    StringGrid1.ColCount := StrToInt(Edit1.Text);
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
  if Edit2.Text <> '' then
    StringGrid1.RowCount := StrToInt(Edit2.Text);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Text := '5';
  Edit2.Text := '5';
  StringGrid1.FixedCols := 0;
  StringGrid1.FixedRows := 0;
  StringGrid1.ColCount := 5;
  StringGrid1.RowCount := 5;
  StringGrid1.Options := StringGrid1.Options + [goEditing];
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i, j: Integer;
  S: Integer; // Переменная для подсчета суммы
begin
  S := 0;
  for i := 0 to StringGrid1.ColCount - 1 do
    for j := 0 to StringGrid1.RowCount - 1 do
    begin
      if StringGrid1.Cells[i, j] = '' then
        Exit;
      if StrToInt(StringGrid1.Cells[i, j]) > 0 then
        S := S + StrToInt(StringGrid1.Cells[i, j]);
    end;
  Edit3.Text := IntToStr(S);
end;

Leave a Comment

83 − 77 =