
02.06.2017, 05:56
|
Модератор
|
|
Регистрация: 17.04.2008
Сообщения: 8,093
Версия Delphi: 7, XE3, 10.2
Репутация: 49089
|
|
Ну на:
Код:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids;
type
TForm1 = class(TForm)
edCols: TEdit;
edRows: TEdit;
Label1: TLabel;
Label2: TLabel;
btSetSize: TButton;
sgMatrix: TStringGrid;
btRandom: TButton;
blCalculate: TButton;
sgResult: TStringGrid;
procedure edColsKeyPress(Sender: TObject; var Key: Char);
procedure btSetSizeClick(Sender: TObject);
procedure btRandomClick(Sender: TObject);
procedure blCalculateClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.blCalculateClick(Sender: TObject);
var
I, J : Integer;
Num, Sum : Integer;
Item : Integer;
begin
For I := 1 To sgMatrix.ColCount Do
Begin
Num := 0;
Sum := 0;
For J := 1 To sgMatrix.RowCount Do
Begin
Item := StrToInt(sgMatrix.Cells[I,J]);
If (Item mod 2) = 0 Then Inc(Num);
If Item < 20 Then Sum := Sum + Item;
End;
sgResult.Cells[I,0] := IntToStr(Num);
sgResult.Cells[I,1] := IntToStr(Sum);
End;
end;
procedure TForm1.btRandomClick(Sender: TObject);
var
I, J : Integer;
begin
Randomize;
For I := 1 To sgMatrix.ColCount Do
Begin
sgResult.Cells[I,0] := '';
sgResult.Cells[I,1] := '';
For J := 1 To sgMatrix.RowCount Do
sgMatrix.Cells[I,J] := IntToStr(Random(100) + 1);
End;
end;
procedure TForm1.btSetSizeClick(Sender: TObject);
var
C, R : Integer;
I, J : Integer;
begin
Try
C := StrToInt(edCols.Text);
R := StrToInt(edRows.Text);
Except
ShowMessage('Can''t convert Cols or Rows to Integer.');
Exit;
End;
If (C < 1) Or (R < 1) Then
Begin
ShowMessage('Cols and/or Rows can''t be less than 1.');
Exit;
End;
Inc(C);
Inc(R);
sgMatrix.ColCount := C;
sgMatrix.RowCount := R;
sgResult.ColCount := C;
For I := 1 To R Do
sgMatrix.Cells[0,I] := InttoStr(I);
For I := 1 To C Do
sgMatrix.Cells[I,0] := InttoStr(I);
sgResult.Cells[0,0] := 'Num';
sgResult.Cells[0,1] := 'Sum';
end;
procedure TForm1.edColsKeyPress(Sender: TObject; var Key: Char);
begin
If Not (Key In ['0'..'9',#8]) Then Key := #0;
end;
end.
|