Autor Beitrag
Nippey
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: Sa 11.11.06 14:19 
Hallo ich bins Nochmal^^

Wir sind in der Schule immernoch an einem Primzahlenrechner dran.Wir haben das auch schon sehr weit bearbeitet und haben nun als Aufgabe den Primzahlrechner zuende zu stellen.Ich habe auch einen Primzahlenrechner nur er hat einen Fehler den ich nicht weg bekomme.Er gibt mir Zahlen als Primzahlen aus der dreier Reihe aus (9,12,15,21,...usw.).Das darf natürlich nicht sein aber ich weis nicht was für eine Bedingung ich noch Einbringne muss damit dieser Fehler behoben wird.Ich hoffe ihr könt mir ein wenig weiter helfen.

(P.S.:Das mit dem Einrücken hab ich noch nicht so ganz drauf...deshalb bitte nich wundern^^)


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:
unit u_Primzahlen;

interface

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

type
  TForm1 = class(TForm)
    lbl_Primzahlrechner: TLabel;
    lbl_Intervall: TLabel;
    edt_UG: TEdit;
    lbl_und: TLabel;
    edt_OG: TEdit;
    lbx_Primzahlen: TListBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button3Click(Sender: TObject);
begin
close
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
edt_OG.text := '';
edt_UG.text := '';
lbx_Primzahlen.Clear
end;

procedure TForm1.Button1Click(Sender: TObject);
  var
  vUG,vOG,vPruefZahl,vIndex,vZaehlVariable,vAnzahlListBoxEintraege : Integer;
  vRest1,vRest2 : Real;
  vPrimZahlEigenschaft : String;

begin

 //Eingabe: Einlesen der Zurückschreiben der Unter- und Obergrenze
   vUG:= StrToInt (edt_UG.Text);
   edt_UG.Text:= IntToStr (vUG);
   vOG:= StrToInt (edt_OG.Text);
   edt_OG.Text:= IntToStr (vOG);
 //Verarbeitung mit Ausgabe:

   lbx_PrimZahlen.Clear;

 //Zahlen auf Primzahl-Eigenschaften prüfen und ggfs. in Listbox schreiben
 //Anm.: Immer bei 2 beginnen, um Geschwindigkeit zu optimieren und Listbox-Einträge verweden zu können
 //anschließend nicht benötigte Werte in der Listbox löschen

   vPruefZahl:= 2;
   lbx_PrimZahlen.Items.Add (IntToStr (vPruefZahl));
   vPruefZahl:= 3;
   lbx_PrimZahlen.Items.Add (IntToStr (vPruefZahl));

   While vPruefZahl < vOG DO
     BEGIN
       vPruefZahl:= vPruefZahl + 1;
       vZaehlVariable:= 1;
       vAnzahlListBoxEintraege:= lbx_PrimZahlen.Items.Capacity;
         //While vZaehlVariable < lbx_PrimZahlen.Items.Capacity DO
             BEGIN
             //lbx_Primzahlen.Items.Add (IntToStr (lbx_PrimZahlen.Items.Capacity));
             vRest1:= vPruefzahl MOD StrToInt (lbx_PrimZahlen.Items [vZaehlVariable-1]);
             IF vRest1 = 0 THEN
             vZaehlVariable:= vZaehlVariable + 1;
             vRest2:= vPruefzahl MOD StrToInt (lbx_PrimZahlen.Items [vZaehlVariable-1]);
             IF (vRest1 = 0) OR (vRest2 = 0) THEN
             vPrimZahlEigenschaft:= 'Nein';
             IF (vRest1 <> 0) AND (vRest2 <> 0) THEN
             BEGIN
             vZaehlVariable:= lbx_PrimZahlen.Items.Capacity + 1;
             vPrimZahlEigenschaft:= 'Ja';
             IF vPrimZahlEigenschaft = 'Ja' THEN
             lbx_PrimZahlen.Items.Add (IntToStr (vPruefZahl));
             END;



           END;//IF Rest <> 0 THEN

   END;//While vPruefZahl <= vOG DO



           While StrToInt (lbx_PrimZahlen.Items [0]) < vUG DO
             BEGIN
               lbx_PrimZahlen.Items.Delete(0);
             END;//StrToInt (lbx_PrimZahlen.Items [0]) < vUG DO

end;

end.


Moderiert von user profile iconGausi: Delphi-Tags hinzugefügt
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: Sa 11.11.06 16:50 
(Änderungen von mir) user profile iconNippey hat folgendes geschrieben:


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:
   lbx_PrimZahlen.Clear;

   vPruefZahl:= 2;
   lbx_PrimZahlen.Items.Add (IntToStr (vPruefZahl));
   vPruefZahl:= 3;
   lbx_PrimZahlen.Items.Add (IntToStr (vPruefZahl));

   While vPruefZahl < vOG DO
   BEGIN
    vPruefZahl:= vPruefZahl + 1;
    vZaehlVariable:= 1;
    //While vZaehlVariable < lbx_PrimZahlen.Items.Capacity DO  //Durch das Raus gibt es auch einen Fehler
    While vZaehlVariable < lbx_Primzahlen.items.Count
    begin
     //vAnzahlListBoxEintraege:= lbx_PrimZahlen.Items.Capacity;
     vAnzahlListBoxEintraege:= lbx_PrimZahlen.Items.Count;//Capacity gibt die maximal mögliche Anzahl an,
                                                          //Count die aktuelle
     vRest1:= vPruefzahl MOD StrToInt (lbx_PrimZahlen.Items [vZaehlVariable-1]);
     IF vRest1 = 0 THEN
     begin
     // Die beiden Anweisungen gehören zusammen
      vPrimZahlEigenschaft:= 'Nein';
      break;//Vorzeitige Beendigung der Prüfungungsschleife
     end
     else // vrest1<>0
      vPrimZahlEigenschaft:= 'Ja';
    // vRest2:= vPruefzahl MOD StrToInt (lbx_PrimZahlen.Items [vZaehlVariable-1]); //Ist doch das gleiche
    // IF (vRest1 = 0) OR (vRest2 = 0) THEN
    //   vPrimZahlEigenschaft:= 'Nein';
    // IF (vRest1 <> 0) AND (vRest2 <> 0) THEN
      //vZaehlVariable:= lbx_PrimZahlen.Items.Capacity + 1; 
      //vZaehlVariable:= lbx_PrimZahlen.Items.Count + 1;
      vPrimZahlEigenschaft:= 'Ja'; // geht, aber setze doch eine boolsche Variable auf true 
     IF vPrimZahlEigenschaft = 'Ja' THEN //Auswerung doch nach der Prüfung?
      lbx_PrimZahlen.Items.Add (IntToStr (vPruefZahl));
     vZaehlVariable:= vZaehlVariable + 1; // Hochzählen ist nötig!
    end;
   END;//While vPruefZahl <= vOG DO


Hinweis Im Optionsmenü gibt es die Option "Bereche" dort "Delphi" auswählen um den Code in Delphitags einzuschließen. Hilft sehr.
ICh habe den Code etwas abgeändert, wie ich meine dass er funktioniert. Probier ihn mal aus.



end.
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: Sa 11.11.06 16:52 
Welche Klassenstufe bist Du?
Nippey Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: Sa 11.11.06 23:05 
Titel: thx
Danke schonmal für die Hilfe.Ich werde es morgen direkt ausprobieren.Ich bin der Klasse 12 des Wirtschaftsgymnasium im IV Leistungskurs.Warum fragst du?
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: So 12.11.06 11:28 
Warum ich frage?
1) String statt boolean;
2) keine arrays, wären hier viel besser
3) int:=int +1, statt inc(int)
4) Code stellenweise nicht eingerückt

aber, na gut, wenn ich da so an meinen InfoLehrer denke, Der hat noch nicht gerafft wie er die Spiele aus einem Verstecken Ordner löscht.
Nippey Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: So 12.11.06 12:53 
Titel: Re
Ja okay...in IV bin ich nich so der über könner.Den Code haben wir ja von unserem Lehrer vorgegebn bekommen.Arrays fangen wir nächste studnen vielleicht übernächste stunde an und das mit dem inc is mir noch ent aufgefallen thx.
Nippey Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: So 12.11.06 15:33 
Titel: Re
Also ich hab dein Programmcode jetzt mal ausprobiert.Das Problem bei deinem Code ist das er erstens auch Zahlen der 3er Reihe ausgibt so wie die 9 und er gibt Zahlen mehrfach aus.Das Problem hatten wir auch am Anfang das er guckt wieviel Zahlen in der Listbox drin sind und dann rechnet er falsch und gibt Zahlen merhfach aus...wenn man eine Grenze von 1-100 eingibt kommt es zum absturz weil er zu viele zahlen ausgibt^^.Kann dir auch glaub ich sagen warum...nachdem du eine Zahl in die Listbox reinschreibst gehst du weider hoch aber ohne die PruefZahl zu erhöhen...D.h. wenn du die 5 überprüfst dann sagt er es is eine PrimZahl aber nachdem er sie in die Listbox geschrieben hat geht er hoch ohne die zahl zu erhöhen,also wird die 5 nochmal ausgegeben.Weil dann aber die ZaehlVariable > ist als die Items in der Listbox wird dann erst ne höhere zahl genommen.ma gucken wie ich das lösen kann.Für Vorschläge bin ich immer zu haben^^.DAnke

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
   While vPruefZahl < vOG DO
   BEGIN
    vPruefZahl:= vPruefZahl + 1;
    vZaehlVariable:= 1;
    While vZaehlVariable < lbx_Primzahlen.items.Count DO
    begin
     vAnzahlListBoxEintraege:= lbx_PrimZahlen.Items.Count;
     vRest1:= vPruefzahl MOD StrToInt (lbx_PrimZahlen.Items [vZaehlVariable-1]);
     IF vRest1 = 0 THEN
     begin
      vPrimZahlEigenschaft:= 'Nein';
      break;//Vorzeitige Beendigung der Prüfungungsschleife
     end
     else // vrest1<>0
      vPrimZahlEigenschaft:= 'Ja';
     IF vPrimZahlEigenschaft = 'Ja' THEN
      lbx_PrimZahlen.Items.Add (IntToStr (vPruefZahl));
     vZaehlVariable:= vZaehlVariable + 1;
     break; // dieses BREAK sorgt dafür das eine Zahl (wie die 5) nicht 2 mal sugegeben wird!
    end;
   END;//While vPruefZahl <= vOG DO


Aber jetzt ist noch das Problem mti der 3er Reihe...HELP ME!^^

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: So 12.11.06 23:28 
Code nochmals geändert
Besser ist:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
   While vPruefZahl < vOG DO
   BEGIN
    vPruefZahl:= vPruefZahl + 1;
    vZaehlVariable:= 1;
    vPrimZahlEigenschaft:= 'Nein';
    While vZaehlVariable < lbx_Primzahlen.items.Count DO
    begin
     vAnzahlListBoxEintraege:= lbx_PrimZahlen.Items.Count;
     vRest1:= vPruefzahl MOD StrToInt (lbx_PrimZahlen.Items [vZaehlVariable-1]);
     IF vRest1 = 0 THEN
     begin
      vPrimZahlEigenschaft:= 'Nein';
      break;//Vorzeitige Beendigung der Prüfungungsschleife
     end
     else // vrest1<>0
      vPrimZahlEigenschaft:= 'Ja';
     vZaehlVariable:= vZaehlVariable + 1;
     //break; //muss weg sonst prüft er nur mit der ersten Zahl in der liste(halt die zwei)
    end;
    IF vPrimZahlEigenschaft = 'Ja' THEN// Eintrag erst nach der Prüfung, vorher geschah
     lbx_PrimZahlen.Items.Add (IntToStr (vPruefZahl));//dies mittendrin
   END;//While vPruefZahl <= vOG DO


Noch eine Frage:
Prüft ihr den Code mit Hilfe von Breakpoints, Überwachten Ausdrücken und ähnlichem? Dann wäre der Fehler sofort gefunden. Einfach einen Breakpoint auf die erste Anweisung der Schleife setzen(im EditorFenster F5) und dann mit Einzelschritt (mit F8 unf F7, Achtung verhalten sich leicht different) durch den Code gehen.

Fals noch weitere Fehler drinn sind, immer her damit.
Nippey Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: Mo 13.11.06 14:05 
Ja wir prüfen die Programme mit so Haltepunkten und gehen dann schritt für schritt mit F7 durch.ich werde mich gleich direkt mal an den code machen.dankeschön
Nippey Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: Mo 13.11.06 14:32 
Titel: THX!!!!!
Perfekt durch die 2 Umsetzungen der beiden Begriffe klappt der Rechner jetzt einwandfrei.Ich danke dir für deine Hilfe!Wir werden uns bald weider sehen denke ich 8)