unit Main_FormU;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls;
type
TValueArray =
array of Double;
TMain_Form =
class( TForm )
ValuesA_ListBox : TListBox;
ValuesB_ListBox : TListBox;
ValueSumA_Label : TLabel;
ValueSumB_Label : TLabel;
Result_Label : TLabel;
RandomData_Button: TButton;
procedure RandomData_ButtonClick( Sender : TObject );
private
FValuesA : TValueArray;
FValuesB : TValueArray;
FSumA : Double;
FSumB : Double;
FResult : Double;
protected
procedure RandomFill(
var Values : TValueArray );
procedure Calculate;
function SumValues( Values : TValueArray ) : Double;
procedure ShowValues( Values : TValueArray; AListBox : TListBox );
procedure RefreshView;
public
{ Public-Deklarationen }
end;
var
Main_Form : TMain_Form;
implementation
{$R *.dfm}
{ TMain_Form }
procedure TMain_Form.Calculate;
begin
FSumA := SumValues( FValuesA );
FSumB := SumValues( FValuesB );
FResult := FSumB - FSumA;
end;
function TMain_Form.SumValues( Values : TValueArray ) : Double;
var
LIndex : Integer;
begin
Result := 0;
for LIndex := Low( Values )
to High( Values )
do
begin
Result := Result + Values[LIndex];
end;
end;
procedure TMain_Form.RefreshView;
begin
ShowValues( FValuesA, ValuesA_ListBox );
ShowValues( FValuesB, ValuesB_ListBox );
ValueSumA_Label.Caption := FloatToStrF( FSumA, ffNumber, 8, 2 );
ValueSumB_Label.Caption := FloatToStrF( FSumB, ffNumber, 8, 2 );
Result_Label.Caption := FloatToStrF( FResult, ffNumber, 8, 2 );
end;
procedure TMain_Form.ShowValues( Values : TValueArray; AListBox : TListBox );
var
LIndex : Integer;
begin
AListBox.Items.BeginUpdate;
try
AListBox.Clear;
for LIndex := Low( Values )
to High( Values )
do
AListBox.Items.Add( FloatToStrF( Values[LIndex], ffNumber, 8, 2 ) );
finally
AListBox.Items.EndUpdate;
end;
end;
procedure TMain_Form.RandomData_ButtonClick( Sender : TObject );
var
LCount : Integer;
LIndex : Integer;
begin
RandomFill( FValuesA );
RandomFill( FValuesB );
Calculate;
RefreshView;
end;
procedure TMain_Form.RandomFill(
var Values : TValueArray );
var
LCount : Integer;
LIndex : Integer;
begin
LCount := Random( 10 ) + 3;
SetLength( Values, LCount );
for LIndex := Low( Values )
to High( Values )
do
begin
Values[LIndex] := Round( Random * 1000 * 100 ) / 100;
end;
end;
end.