Einzelnen Beitrag anzeigen

Benutzerbild von c113plpbr
c113plpbr

Registriert seit: 18. Nov 2003
Ort: localhost
674 Beiträge
 
Delphi 2005 Professional
 
#12

Re: guter stil????

  Alt 25. Mär 2006, 22:52
Zitat von xaromz:
Ach ja,
in der Routine "Pruefen":
Delphi-Quellcode:
  if p = true then
    pruefen := true
  else
    pruefen := false;
Wie wäre es hier mit
Result := p; Gruß
xaromz
Wie wäre es ganz ohne p? das is nämlich vollkommen überflüssig ...

mein vorschlag:
Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function pruefen(n: int64): boolean;
    procedure zerlegen(n: Int64);
  end;

var
  m: int64;
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.pruefen(n: int64): boolean;
var
  x: Int64;
begin
  Result := True;
  x := Round(n / 2);
  if(n >= 2)then
  begin
    while(x > 1)do
    begin
      if((n mod x) = 0)then
      begin
        Result := False;
        Break;
      end;
      Dec(x);
    end;
  end
  else
    Result := False;
end;

procedure TForm1.zerlegen(n: Int64);
var
  x: int64;
begin
  x := 2;
  while((n mod x) <> 0)do
    Inc(x);

  if(Length(Label2.Caption) > 0)then
    Label2.Caption := Label2.Caption + '*';

  Label2.Caption := Label2.Caption + IntToStr(x);

  if not(pruefen(n div x))then
    zerlegen(n div x);

  m := m * x;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  m := 1;
  Label2.Caption := '';

  if(pruefen(StrToInt(Edit1.Text)))then
    Label2.Caption := Edit1.Text + ' ist eine Primzahl!'
  else
  begin
    zerlegen(StrToInt(Edit1.Text));
    Label2.Caption := Label2.Caption + '*' + IntToStr(StrToInt(Edit1.Text) div m);
  end;
end;

end.
Allerdings gefällt mir an diesem Code grundsätzlich gesehen die globale variable "m" nicht ...

ciao, Philipp
Philipp
There is never enough time to do all the nothing you want.
*HABENWILL*
  Mit Zitat antworten Zitat