AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Calculate Min Max of integer

Ein Thema von nanix · begonnen am 2. Dez 2009 · letzter Beitrag vom 3. Dez 2009
Antwort Antwort
Seite 1 von 2  1 2      
nanix
(Gast)

n/a Beiträge
 
#1

Calculate Min Max of integer

  Alt 2. Dez 2009, 20:55
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.
  Mit Zitat antworten Zitat
Benutzerbild von Mithrandir
Mithrandir
(CodeLib-Manager)

Registriert seit: 27. Nov 2008
Ort: Delmenhorst
2.379 Beiträge
 
#2

Re: Calculate Min Max of integer

  Alt 2. Dez 2009, 21:05
Shouldn't this be TRUE ?

Delphi-Quellcode:
procedure TMyInt.Init;
begin
  FInit := True;
end;
米斯蘭迪爾
"In einer Zeit universellen Betruges wird das Aussprechen der Wahrheit zu einem revolutionären Akt." -- 1984, George Orwell
  Mit Zitat antworten Zitat
nanix
(Gast)

n/a Beiträge
 
#3

Re: Calculate Min Max of integer

  Alt 2. Dez 2009, 21:08
Well result is the same i believe
But is there a simplier way for this too much code IMHO..
  Mit Zitat antworten Zitat
Benutzerbild von Mithrandir
Mithrandir
(CodeLib-Manager)

Registriert seit: 27. Nov 2008
Ort: Delmenhorst
2.379 Beiträge
 
#4

Re: Calculate Min Max of integer

  Alt 2. Dez 2009, 21:13
Zitat von nanix:
Well result is the same i believe
We're not in church. Do you believe it or have you tried it?
米斯蘭迪爾
"In einer Zeit universellen Betruges wird das Aussprechen der Wahrheit zu einem revolutionären Akt." -- 1984, George Orwell
  Mit Zitat antworten Zitat
nanix
(Gast)

n/a Beiträge
 
#5

Re: Calculate Min Max of integer

  Alt 2. Dez 2009, 21:17
Well maybe becouse the random number?
  Mit Zitat antworten Zitat
Benutzerbild von Mithrandir
Mithrandir
(CodeLib-Manager)

Registriert seit: 27. Nov 2008
Ort: Delmenhorst
2.379 Beiträge
 
#6

Re: Calculate Min Max of integer

  Alt 2. Dez 2009, 21:23
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?
米斯蘭迪爾
"In einer Zeit universellen Betruges wird das Aussprechen der Wahrheit zu einem revolutionären Akt." -- 1984, George Orwell
  Mit Zitat antworten Zitat
nanix
(Gast)

n/a Beiträge
 
#7

Re: Calculate Min Max of integer

  Alt 2. Dez 2009, 21:31
Yes but is this why not work?
  Mit Zitat antworten Zitat
Benutzerbild von Mithrandir
Mithrandir
(CodeLib-Manager)

Registriert seit: 27. Nov 2008
Ort: Delmenhorst
2.379 Beiträge
 
#8

Re: Calculate Min Max of integer

  Alt 2. Dez 2009, 21:32
Try it... Or what would you like to know? It's hard to understand your question...
米斯蘭迪爾
"In einer Zeit universellen Betruges wird das Aussprechen der Wahrheit zu einem revolutionären Akt." -- 1984, George Orwell
  Mit Zitat antworten Zitat
nanix
(Gast)

n/a Beiträge
 
#9

Re: Calculate Min Max of integer

  Alt 2. Dez 2009, 21:33
Or is there a more newbie friendly solution to this more simple code?
  Mit Zitat antworten Zitat
Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.429 Beiträge
 
Delphi 10.4 Sydney
 
#10

Re: Calculate Min Max of integer

  Alt 3. Dez 2009, 10: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
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:59 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz