Autor Beitrag
mitor
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Mo 10.01.05 15:59 
Hallo,

ich habe folgendes Problem: Wie finde ich heraus, was für Primzahlen sich zwischen zwei Zahlen (z.B. 1 und 10) befinden. In diesem Fall wären das 1, 2, 3, 5, 7. Leider spuckt mein Programm nur die erste Zahl aus :|.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    ListBox1: TListBox;
    Label2: TLabel;
    Label4: TLabel;
    Label3: TLabel;
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    GroupBox2: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Edit2: TEdit;
    Label9: TLabel;
    Edit3: TEdit;
    ListBox2: TListBox;
    Button3: TButton;
    Button2: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


//Primzahltest
procedure TForm1.Button1Click(Sender: TObject);
var zahl, teilmenge, i:integer;
begin
zahl:=strtoint(edit1.Text);

for i:=1 to zahl do
   begin
       if zahl mod i = 0
          then
              begin
              teilmenge:=i;
              listbox1.Items.add(inttostr(teilmenge));
              end;
   end;

if listbox1.Items.count = 2
  then
      begin
      label3.Visible:=true;
      label4.Visible:=false;
      end
  else
      begin
      label3.Visible:=false;
      label4.Visible:=true;
      end;
end;

//Programm beenden
procedure TForm1.Button4Click(Sender: TObject);
begin
close
end;

//Primzahlen in den Grenzen
procedure TForm1.Button3Click(Sender: TObject);
var untergrenze, obergrenze, i, zahl, teilmenge:integer;
begin
untergrenze:=strtoint(edit2.Text);
obergrenze:=strtoint(edit3.Text);

for i:=untergrenze to obergrenze do
    if zahl mod i = 0
      then
          zahl:=i;
          ListBox2.items.add(inttostr(zahl));
      end;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
edit1.Clear;
edit2.Clear;
edit3.Clear;
listbox1.Items.Clear;
listbox2.items.clear;
end;

end.
Kroni
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 720

Win 98, Win ME, Win2k, Win XP
D3 Pro
BeitragVerfasst: Mo 10.01.05 16:24 
Das Problem dürfte darin liegen, dass du eine If anweisung hast, die wohl ZWEI BEfehle aufmal durchführen soll, aber wenn du gein
BEGIN
END;
darum setzt, führt sie nur den ersten Befehl aus....
Ein Beispiel:

ausblenden Delphi-Quelltext
1:
2:
3:
if not (2=2) then 
    Edit1.text:='Hallo';
    Edit2.text:='Du';

da zwei immer gleich zwei ist, wird die Bedingung nie erfüllt sein, dennoch erscheint in Edit2 ein 'Du', da Delphi immer nur die erste Anweisung nach einer If Abfrage zu der Bedingung zählt.

Willst du jedoch, dass zwei oder Mehr anweisungen gemacht werden, solltest du eine begin end; Klammer setezen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
if not(2=2) then
   begin
   Edit1.text:='hallo';
   Edit2.text:='Du';
   end;

Jetzt passiert gar nichts.

Das dürfte das Problem sein.

Was auch sinnlos ist, ist die Zuweisung von I:=Z......
Ist meine meinung
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: Di 11.01.05 10:14 
Button1click testnur eine zahl und button3click kann nicht richtig funktionieren, da in zeile 93 die Variable Zahl keinen Inhalt hat.

Übrigens kann man diese Routinen noch stark verbessern, such dazu mal im Forum. Gibt eine Menge Beiträge zu diesem Thema.
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Di 11.01.05 20:07 
Also wie jetzt? Begin/End setzen oder nicht?

MIr is schon klar das man das ganze noch vereinfachen kann - danach habe ich aber nicht gefragt....
st-matze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 138

Win 3.11, Win 95, Win 98, Win XP
D7 Ent, D6 Pers, (D5 Pers)
BeitragVerfasst: Di 11.01.05 20:22 
ich glaube es geht um folgende funktion:


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.Button3Click(Sender: TObject);  
var untergrenze, obergrenze, i, zahl, teilmenge:integer;  
begin  
untergrenze:=strtoint(edit2.Text);  
obergrenze:=strtoint(edit3.Text);  
 //zahl wird nicht initialisiert
 //standard wert ist dann 0
for i:=untergrenze to obergrenze do  
    if zahl mod i = 0  // 0 mod irgendetwas ist wahr
      then 
      begin //hier muss begin hin
          zahl:=i;  //zahl = untergrenze
//nachdem zahl implizit auf untergrenze gesetzt wurde wird (Zahl mod i =0) nie wieder wahr, da immer i > untergrenze
//ergo nur erster wert (untergrenze) wird ausgegeben, egal ob primzahl oder nicht
          ListBox2.items.add(inttostr(zahl));   //ergebnis ausgeben
      end; 
end;
st-matze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 138

Win 3.11, Win 95, Win 98, Win XP
D7 Ent, D6 Pers, (D5 Pers)
BeitragVerfasst: Di 11.01.05 20:26 
@mitor

Hinweis :!: :

räum deinen Quellcode auf
    - Variablen (auch edit - felder. labels und andere komponenten) eindeutig und selbsterklärend bezeichnen, so dass di funktion aus dem namen ersichtilich ist
    - Kommentare hinzufügen


st-matze
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Di 11.01.05 20:47 
Ehrlich gesagt schnall ich jetzt überhaupt nichts mehr....

Wie kann ich nun 'zahl' initialisieren?
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: Di 11.01.05 21:49 
zahl:=1;
st-matze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 138

Win 3.11, Win 95, Win 98, Win XP
D7 Ent, D6 Pers, (D5 Pers)
BeitragVerfasst: Mi 12.01.05 07:56 
für die Primzahlenberechnung, kann ich dir auch folgendes Thema empfehlen link
st-matze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 138

Win 3.11, Win 95, Win 98, Win XP
D7 Ent, D6 Pers, (D5 Pers)
BeitragVerfasst: Mi 12.01.05 08:29 
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    EdUntergrenze: TEdit;
    EdObergrenze: TEdit;
    BtnStarteBerechnung: TButton;
    LBErgebnisse: TListBox;
    procedure BtnStarteBerechnungClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;
  TIntegers = array of integer;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function isPrime(IN_zahl: integer):boolean;
//Prüfen, ob eine übergebene Zahl PrimZahl ist
var teiler  : integer;
    wurzel  : extended;
begin
  teiler  :=  3;
  if (IN_Zahl <= 1) or ( (not odd(IN_Zahl)) and (IN_Zahl <> 2)) then
  //Wenn die Zahl kleiner oder gleich 1 ist -> keine Primzahl
  //Wenn die Zahl gerade (hier "nicht ungerade) und nicht 2 ist -> keine Primzahl
  begin
    Result  :=  false;
    wurzel  :=  0;
  end
  else
  begin
    //Initialisierung
    Result  :=  true;
    wurzel  :=  sqrt(IN_zahl);
  end;
  while ((teiler  <=  wurzel) and Result) do
  //für alle teiler von 3 bis wurzel aus zahl prüfen,
  //ob Zahl teilbar ist
  begin
    if ((IN_zahl mod teiler)=0) then
    //wenn zahl durch teiler teilbar -> keine Primzahl
      result:=false;
    Inc(teiler,2);
  end;
  //Wenn schleife für alle Teiler durchlaufen, bleibt Result auf true - > primzahl
end;

function getPrimesBetween(IN_UG, IN_OG: integer): TIntegers;
var
  cnt   : integer;
  cnt2  : integer;
begin
  if IN_UG > IN_OG then
  begin
  //Wenn UG größer als OG, dann werte vertauschen
    cnt   :=  IN_OG;
    IN_OG :=  IN_UG;
    IN_UG :=  cnt;
  end;
  SetLength(Result, IN_OG - IN_UG + 1);
  //Länge des Ergebniss arrays auf maximale ergebnissmenge setzen
  cnt2:=0;
  for cnt :=  IN_UG to  IN_OG do
  begin
    if  isPrime(cnt)  then
    // für alle zahlen im bereich primzahlen prüfung durchführen
    begin
      Result[cnt2]  :=  cnt;
      Inc(cnt2);
    end;
  end;
  // Länge des Arrays auf tatsächlich gefunden Ergebnissmenge verkürzen
  SetLength(Result,cnt2);
end;

procedure TForm1.BtnStarteBerechnungClick(Sender: TObject);
var
  ug, og:integer;
  primes: TIntegers;
  cnt:  integer;
begin
  try
    LBErgebnisse.Clear; 
    ug  :=  strtoint(EdUntergrenze.text);
    og  :=  strtoint(EdObergrenze.text);
    primes  :=  getPrimesBetween(ug,og);
    cnt :=  Length(primes);
    while cnt > 0 do
    begin
      Dec(cnt);
      LBErgebnisse.Items.Add(inttostr(primes[cnt]));
    end;
  except
    on e: Exception do
      showmessage(e.Message);
  end;
end;

end.
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Mi 12.01.05 14:07 
wenn du mir jetzt noch sagtst was cnt und cnt2 ist....
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: Mi 12.01.05 19:53 
cnt=cOUnt=zählen
st-matze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 138

Win 3.11, Win 95, Win 98, Win XP
D7 Ent, D6 Pers, (D5 Pers)
BeitragVerfasst: Do 13.01.05 08:02 
:autsch:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
function getPrimesBetween(IN_UG, IN_OG: integer): TIntegers;  
var  
  cnt   : integer;  
  cnt2  : integer;  
begin  
{..}
    cnt   :=  IN_OG;  //<< hier wird cnt nur als Hilfsvariable verwendet
{..}
  for cnt :=  IN_UG to  IN_OG do  
// für alle cnt von Untergrenze bis Obergrenze ...
// cnt enthält die aktuelle Zahl die geprüft werden soll, ob si prime ist.
  begin  
    if  isPrime(cnt)  then  // Prüfung mit cnt
    begin  
//Nutzung von cnt2 als Index für das Array der gefunden Primes
      Result[cnt2]  :=  cnt;  
      Inc(cnt2);  
    end;  
//cnt2 enthält am ende der Schleife die Anzahl der gefunden Primes, logisch
{..}
end;  

 
procedure TForm1.BtnStarteBerechnungClick(Sender: TObject);  
var  
{..}
  cnt:  integer;  
begin  
  try  
{..}
// in diesem abschnitt wird cnt als Index für das array mit den gefunden Primes verwendet
    cnt :=  Length(primes);  
    while cnt > 0 do  
    begin  
      Dec(cnt);  
      LBErgebnisse.Items.Add(inttostr(primes[cnt]));  
    end;  
{..}
end;  

 
end.
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Do 13.01.05 08:50 
Entschuldige das ich gefragt habe...mach die Topic lieber dicht bevor ich noch erschlagen werde.... :?
st-matze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 138

Win 3.11, Win 95, Win 98, Win XP
D7 Ent, D6 Pers, (D5 Pers)
BeitragVerfasst: Do 13.01.05 08:55 
nich so gemeint, hab nur nen bisschen schlechte laune heut

aber den queltext zweimal anschauen, bevor fragen stellt schad auch nicht