unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids, StdCtrls;
type
TStudent = {$IFDEF CPUX86}packed{$ENDIF}record
name: string;
inf: byte;
phys: byte;
math: byte;
avg: real;
end;
TStudents = specialize TArray<TStudent>;
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FStudents: TStudents;
protected
procedure AddStudentToGrid(AStudent: TStudent);
public
procedure AddStudent(const AName: string; AInf, APhys, AMath: byte);
procedure ClearGrid;
procedure FilterAvg(AValue: Real = 0);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.ColCount := 6;
StringGrid1.RowCount := 2;
StringGrid1.Rows[0].AddStrings(
['N', 'Фамилия', 'Информатика', 'Физика', 'Математика', 'Ср. балл']);
StringGrid1.ColWidths[0] := 30;
StringGrid1.ColWidths[1] := 110;
StringGrid1.ColWidths[2] := 110;
StringGrid1.ColWidths[3] := 60;
StringGrid1.ColWidths[4] := 110;
StringGrid1.ColWidths[5] := 110;
end;
procedure TForm1.AddStudentToGrid(AStudent: TStudent);
begin
with AStudent do
StringGrid1.Rows[StringGrid1.RowCount - 1].AddStrings(
[
(StringGrid1.RowCount - 1).ToString,
name,
inf.ToString,
phys.ToString,
math.ToString,
FormatFloat('0.0', avg)
]
);
StringGrid1.RowCount := StringGrid1.RowCount + 1;
end;
procedure TForm1.AddStudent(const AName: string; AInf, APhys, AMath: byte);
begin
if not AName.IsEmpty then
begin
SetLength(FStudents, Length(FStudents) + 1);
with FStudents[High(FStudents)] do
begin
name := AName;
inf := AInf;
phys := APhys;
math := AMath;
avg := (AInf + APhys + AMath) / 3;
end;
AddStudentToGrid(FStudents[High(FStudents)]);
end;
end;
procedure TForm1.ClearGrid;
var
i: integer;
begin
for i := 1 to StringGrid1.RowCount - 1 do
StringGrid1.Rows[i].Clear;
StringGrid1.RowCount := 2;
end;
procedure TForm1.FilterAvg(AValue: Real);
var
i: integer;
begin
ClearGrid;
for i := 0 to High(FStudents) do
if FStudents[i].avg >= AValue then
AddStudentToGrid(FStudents[i]);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetLength(FStudents, 0);
ClearGrid;
AddStudent('Бабурин', 4, 4, 5);
AddStudent('Васильев', 5, 5, 5);
AddStudent('Дьяконова', 2, 3, 3);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FilterAvg(4.5);
end;
end.
Вместо того чтобы оставить тех кто подходит под условие - убирает все ячейки (схлопывает их, нулевая высота)
Как можно исправить?