Einzelnen Beitrag anzeigen

Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.477 Beiträge
 
Delphi 12 Athens
 
#10

Re: Calculate Min Max of integer

  Alt 3. Dez 2009, 11:12
Delphi-Quellcode:
type
  TMyInt = record
  private
    FHasValue: Boolean;
    FMax: Integer;
    FMin: Integer;
    FValue: Integer;
    procedure SetValue(AValue: Integer);
  public
    procedure Clear;
    property HasValue: Boolean read FHasValue;
    property Max: Integer read FMax;
    property Min: Integer read FMin;
    property Value: Integer read FValue write SetValue;
  end;

type
  TForm3 = class(TForm)

    ButtonStart: TButton;
    ButtonStop: TButton;
    Timer1: TTimer;

    procedure ButtonStartClick(Sender: TObject);
    procedure ButtonStopClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
    FMyInit: TMyInt;
    procedure ShowMyInit;
  public
    { Public declarations } 
  end;

implementation

{$R *.dfm} 

procedure TMyInt.Clear;
begin
  FHasValue := False;
  FMax := 0;
  FMin := 0;
  FValue := 0;
end;

procedure TMyInt.SetValue(AValue: Integer);
begin
  if FHasValue then
  begin
    if FValue <> AValue then
    begin
      FValue := AValue;

      if FMax < AValue then
        FMax := AValue;

      if FMin > AValue then
        FMin := AValue;
    end;
  end
  else
  begin
    FHasValue := True;
    FMax := AValue;
    FMin := AValue;
    FValue := AValue;
  end;
end;

// usage

procedure TForm3.ButtonStartClick(Sender: TObject);
begin
  FMyInt.Clear;
  ShowMyInit;
  Timer1.Enabled := True;
end;

procedure TForm3.ButtonStopClick(Sender: TObject);
begin
  Timer1.Enabled := False;
end;

procedure TForm3.ShowMyInit;
begin
  if FMyInt.HasValue then
    Caption := Format('Value %d Min %d Max %d', [FMyInt.Value, FMyInt.Min, FMyInt.Max])
  else
    Caption := 'Value Min Max'
end;

procedure TForm3.Timer1Timer(Sender: TObject);
begin
  FMyInit.Value := Random(100);
  ShowMyInit;
end;
  Mit Zitat antworten Zitat