Einzelnen Beitrag anzeigen

David Martens

Registriert seit: 29. Sep 2003
205 Beiträge
 
Delphi XE Enterprise
 
#14

AW: durchschnittszeit ermitteln

  Alt 14. Jun 2010, 20:05
Also wenn du nicht auf deine 10 Durchläufe pochst dann mach es doch einfach so:

Delphi-Quellcode:
type
  TForm1 = class(TForm)
    btnStart: TButton;
    btnStop: TButton;
    lbl1: TLabel;
    lblZeit: TLabel;
    lbl2: TLabel;
    lbl3: TLabel;
    lblDurchschnitt: TLabel;
    Timer1: TTimer;
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    fZeit, // Variable für die aktuelle Stopuhr
    fZeitCumulativ : extended; // Variable für die aufaddierten Stopzeiten
    iCount : integer; // Variable für Anzahl der Stopzeiten
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnStartClick(Sender: TObject);
begin
  // Stopuhr startet bei 0 :)
  fZeit := 0;

  // Zeit anzeigen
  lblZeit.Caption := FormatDateTime('hh:nn:ss zzz', fZeit);

  // Timer/Stopuhr starten
  Timer1.Enabled := true;
end;

procedure TForm1.btnStopClick(Sender: TObject);
begin
  // Timer/Stopuhr anhalten
  Timer1.Enabled := false;

  // Zeit zu den Stopzeiten addieren und Anzahl hochsetzen
  fZeitCumulativ := fZeitCumulativ + fZeit;
  inc(iCount);

  // Durchschnitt anzeigen
  lblDurchschnitt.Caption := FormatDateTime('hh:nn:ss zzz', fZeitCumulativ / iCount);

  lbl3.Caption := 'von ' + IntToStr(iCount);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  // nächsten Zeitpunkt berechnen
  // Timer1.Interval / 1000 gewärleistet immer die richtige Schrittweite egal was im Timer eigestellt ist
  fZeit := fZeit + Timer1.Interval / 1000 / 86400;

  // Zeit anzeigen
  lblZeit.Caption := FormatDateTime('hh:nn:ss zzz', fZeit);
end;
mit folgender .dfm

Delphi-Quellcode:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 125
  ClientWidth = 231
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object lbl1: TLabel
    Left = 8
    Top = 8
    Width = 22
    Height = 13
    Caption = 'Zeit:'
  end
  object lblZeit: TLabel
    Left = 16
    Top = 24
    Width = 3
    Height = 13
  end
  object lbl2: TLabel
    Left = 112
    Top = 8
    Width = 64
    Height = 13
    Caption = 'Durchschnitt:'
  end
  object lblDurchschnitt: TLabel
    Left = 120
    Top = 24
    Width = 3
    Height = 13
  end
  object lbl3: TLabel
    Left = 182
    Top = 8
    Width = 21
    Height = 13
    Caption = 'von '
  end
  object btnStart: TButton
    Left = 8
    Top = 56
    Width = 75
    Height = 25
    Caption = 'Start'
    TabOrder = 0
    OnClick = btnStartClick
  end
  object btnStop: TButton
    Left = 8
    Top = 87
    Width = 75
    Height = 25
    Caption = 'Stop'
    TabOrder = 1
    OnClick = btnStopClick
  end
  object Timer1: TTimer
    Enabled = False
    Interval = 10
    OnTimer = Timer1Timer
    Left = 104
    Top = 56
  end
end
  Mit Zitat antworten Zitat