Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - Java in Delphi Übersetzung richtig misslungen


ute.h_80 - Mi 07.01.04 19:24
Titel: Java in Delphi Übersetzung richtig misslungen
Hab versucht nen Java Code in Delphi zu übersetzen, aber es funktioniert nicht richtig. :oops:
Vielleicht kann mir ja jemand helfen.

Java Code

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:
// sieve of Eratosthenes
   public void erat(long num) {
      String out = (num +" =");
      boolean isPrime = true, notfirst = false;
      long temp = num;
      
      while (temp % 2 == 0) {
   temp /= 2;
   if (notfirst) { out += " *"; }
   else { notfirst = true; }
   out += " 2";
   isPrime = false;
      }
      
      int check = 3;
      while (check <= (num / 2)) {
   while (temp % check == 0) {
      temp /= check;
      if (notfirst) { out += " *"; }
      else { notfirst = true; }
      out += (" "+ check);
      isPrime = false;
   }
   check +=2;
      }
      
      
      if (isPrime) { eratField.setText(prime); out += (" "+prime); }
      else { eratField.setText(composite); }

      eratcalc.setText(out);
   }


und hier mein Delphi versuch

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:
     ERATO_WEG.Clear;
     num := Strtoint64(ERATO_ZAHL1.Text);

     ticks := GetTickCount;
     temp := num;
     notfirst := false;
     isPrime := true;

     WHILE (temp mod 2) = 0 DO
           BEGIN
                temp := temp div 2;
                IF notfirst = false THEN ERATO_WEG.Lines[0]:= ''
                ELSE notfirst := true;
                ERATO_WEG.Lines[0] := '2';
                isPrime := false;
           END;
     check := 3;
     WHILE check <= (num div 2DO
           BEGIN
                WHILE (temp mod check) = 0 DO
                      BEGIN
                           temp := temp div check;
                           IF notfirst = false THEN ERATO_WEG.Lines[0] := ERATO_WEG.Lines[0] + ' * '
                           ELSE notfirst := true;
                           ERATO_WEG.Lines[0] := ERATO_WEG.Lines[0] + inttostr(check);
                           isPrime := false;
                      END;
                check := check + 2;
           END;
     ERATO_WEG.Lines[0] := ERATO_WEG.Lines[0] + ' = ' + inttostr(num);

     IF isPrime = true THEN ERATO_WEG.Lines.Add('Primzahl')
     ELSE ERATO_WEG.Lines.Add(inttostr(num) + ' = zusammengesetzte Primzahl');


Moderiert von user profile iconMotzi: Code- durch Delphi-Tags ersetzt


teebee - Do 08.01.04 15:29
Titel: Re: Java in Delphi Übersetzung richtig misslungen
Ich habe gerade keine Zeit, mir das genauer anzuschauen, aber zwei Dinge sind mir aufgefallen:

a) Ich würde aus Performancegründen (Du willst die Zeit messen, die das ganze dauert?) während des Algorithmus nicht immer auf die erste Zeile des Memos schreiben, sondern auf einen String, der dann zum Schluss ins Memo geschrieben wird.

b) Du hast die If-Anweisungen falsch übersetzt:
(z.B. im oberen Codebereich)
ute.h_80 hat folgendes geschrieben:

Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
...
      while (temp % 2 == 0) {
   temp /= 2;
   if (notfirst) { out += " *"; }
    ^^^^^^^
   else { notfirst = true; }
   out += " 2";
   isPrime = false;
      }
...

Hier wird geschaut, ob notfirst TRUE ist, Du prüfst auf FALSE:
ute.h_80 hat folgendes geschrieben:

Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
...
WHILE (temp mod 2) = 0 DO
           BEGIN
                temp := temp div 2;
                IF notfirst = false THEN ERATO_WEG.Lines[0]:= ''
                ^^^^^^^^^^^                                 ^^^^Hier löschst Du alles, es soll doch was angehängt werden...
                ELSE notfirst := true;
                ERATO_WEG.Lines[0] := '2';
                isPrime := false;
           END;
...

Ich würde es so machen:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
...
While (temp Mod 2)=0 Do Begin
 temp := temp Div 2;
 If NotFirst Then OutString := OutString+" *" Else NotFirst := True;
 OutString := OutString+" 2";
 IsPrime := False;
End;
...

Wenn Du die If-Anweisungen nochmal checkst, sollte es eigentlich gehen, vorausgesetzt, der Algorithmus stimmt vom Prinzip...

Gruß, teebee


ute.h_80 - Mo 12.01.04 12:12
Titel: Danke
jetzt klappt es, DANKE!!!