Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Calculate Min Max of integer (https://www.delphipraxis.net/144225-calculate-min-max-integer.html)

nanix 2. Dez 2009 20:55


Calculate Min Max of integer
 
Hello how could i calculate this ... i was fooling around but still can't get it working here is what i got so far.
Basicly all values are the same MIN MAX VALUE

Delphi-Quellcode:
unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm3 = class(TForm)

    Button1: TButton;
    Timer1: TTimer;
    Button2: TButton;


    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


 type
  TMyInt = record
    FValue: Integer;
    FMin: Integer;
    FMax: Integer;
    FInit: Boolean;
  private
    procedure SetValue(const IntValue: Integer);
  public
    procedure Init;

    property Value: Integer read FValue write SetValue;
    property Min: Integer read FMin;
    property Max: Integer read FMax;
    end;

var
  Form3: TForm3;







implementation
{$R *.dfm}

procedure TMyInt.Init;
begin
  FInit := False;
end;

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

  if not FInit then
  begin
    FMax := FValue;
    FMin := FValue;
    FInit := True;
  end
  else
  begin
    if FValue > FMax then
      FMax := FValue;
    if FValue < FMin then
      FMin := FValue;
  end;
end;

// usage


procedure TForm3.Timer1Timer(Sender: TObject);
var
  temp: TMyInt;
begin
  temp.Init;
  temp.Value:=Random(100);
  form3.Caption:='Value '+inttostr(Temp.Value)+' Min '+inttostr(temp.Min)+' Max '+inttostr(temp.Max);

end;

end.

Mithrandir 2. Dez 2009 21:05

Re: Calculate Min Max of integer
 
Shouldn't this be TRUE ?

Delphi-Quellcode:
procedure TMyInt.Init;
begin
  FInit := True;
end;

nanix 2. Dez 2009 21:08

Re: Calculate Min Max of integer
 
Well result is the same i believe :roll:
But is there a simplier way for this too much code IMHO..

Mithrandir 2. Dez 2009 21:13

Re: Calculate Min Max of integer
 
Zitat:

Zitat von nanix
Well result is the same i believe :roll:

We're not in church. Do you believe it or have you tried it?

nanix 2. Dez 2009 21:17

Re: Calculate Min Max of integer
 
Well maybe becouse the random number?

Mithrandir 2. Dez 2009 21:23

Re: Calculate Min Max of integer
 
Well, I see... You know the problem, why don't you fix that? Let me show you something:

Delphi-Quellcode:
unit Unit3;

interface

{...}

var
  Form3: TForm3;
  temp: TMyInt;

{...}

procedure TMyInt.Init;
begin
  FInit := True;
end;

{...}

procedure TForm3.Timer1Timer(Sender: TObject);
begin
  temp.Init;
  temp.Value:=Random(100);
  form3.Caption:='Value '+inttostr(Temp.Value)+' Min '+inttostr(temp.Min)+' Max '+inttostr(temp.Max);
end;

end.
See the difference? ;)

nanix 2. Dez 2009 21:31

Re: Calculate Min Max of integer
 
Yes but is this why not work?

Mithrandir 2. Dez 2009 21:32

Re: Calculate Min Max of integer
 
Try it... Or what would you like to know? It's hard to understand your question...

nanix 2. Dez 2009 21:33

Re: Calculate Min Max of integer
 
Or is there a more newbie friendly solution to this more simple code?

Blup 3. Dez 2009 10:12

Re: Calculate Min Max of integer
 
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;


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:13 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz