Autor Beitrag
rob87
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 461

Win Me, Win XP Home, Win XP Prof
Delphi 2007 Enterprise
BeitragVerfasst: Di 22.01.08 16:23 
Hallo,

ich habe folgendes kleines Problem: Ich will Dezimalzahlen in Dualzahlen umwandeln und des Ganze wieder weiterverarbeiten. (Hab nur testweise die Ausgabe) Nur hab ich momentan ein kleines Datentyp-Problem zwischen den Variablen dezimalzahl und rest.
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:
23:
24:
procedure TfrmVerbrauchAnzeigen.BtnST1Click(Sender: TObject);
type
  TArrayErgebnis = array[1..15] of Integer;
var
  ergebnis : TArrayErgebnis;
  dezimalzahl : Double;
  rest : Integer;
  x : integer;
begin
  dezimalzahl := StrToInt(Edit1.Text);
  x := 1;

  repeat
    rest := Int(dezimalzahl) mod 2;
    dezimalzahl := dezimalzahl / 2;
    x := x + 1;
    ergebnis[x] := rest;
  until dezimalzahl<2;

  for x := 1 to 15 do
  begin
    ShowMessage(FloatToStr(ergebnis[x]));
  end;
end;

Zwecks Modulo braucht dezimalzahl den Datentyp Integer, aber zwecks der Division den Datentyp Double.

Ich weiß, es ist vermutlich nur ne Kleinigkeit *g* Aber ich blick grad gar nimma durch. :roll: Und bevor ich meinen Quellcode noch mehr durch irgendwelche Tests verwurschtle, dacht ich mir, post ich ihn mal hier. :D
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Di 22.01.08 16:29 
Ganzzahldivisionen gehen mit div.

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
nagel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 708

Win7, Ubuntu 10.10

BeitragVerfasst: Di 22.01.08 16:30 
Wo ist das Problem, wenn du dezimalzahl als Integer deklarierst? Du willst ja anscheinend nur Ganzzahlen umwandeln, oder?


Edit: Hm, offensichtlich haben meine Mitantworter dein Problem erkannt :wink: .


Zuletzt bearbeitet von nagel am Di 22.01.08 16:36, insgesamt 1-mal bearbeitet
Xong
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 113

WIN 2000
Borland Developer Studio 2006
BeitragVerfasst: Di 22.01.08 16:31 
Habs nicht ausprobiert:
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:
procedure TfrmVerbrauchAnzeigen.BtnST1Click(Sender: TObject);
type
  TArrayErgebnis = array[1..15] of Integer;
var
  ergebnis : TArrayErgebnis;
  dezimalzahl,rest,x : integer;
begin
  dezimalzahl := StrToInt(Edit1.Text);
  x := 1;

  repeat
    rest := Int(dezimalzahl) mod 2;
    dezimalzahl := dezimalzahl div 2;
    x := x + 1;
    ergebnis[x] := rest;
  until dezimalzahl<2;

  for x := 1 to 15 do
  begin
    ShowMessage(FloatToStr(ergebnis[x]));
  end;
end;


Muss aber auch schnell fort zum Sport. (Und was sich reimt ist gut...)
Bis morgen! =)

Xong
rob87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 461

Win Me, Win XP Home, Win XP Prof
Delphi 2007 Enterprise
BeitragVerfasst: Di 22.01.08 16:39 
OK. Hab ned an des div gedacht.

Und dann gehts so auch wunderbar:
ausblenden Delphi-Quelltext
1:
  dezimalzahl, rest, x : Integer;					

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
  repeat
    rest := dezimalzahl mod 2;
    dezimalzahl := dezimalzahl div 2;
    x := x + 1;
    ergebnis[x] := rest;
  until dezimalzahl<2;

Merci.

Aber nun nochwas: Kann mir jemand sagen, warum er in das Array "ergebnis" nicht nur die Werte aus
ausblenden Delphi-Quelltext
1:
    ergebnis[x] := rest;					

schreibt, sondern auch noch nen haufen anderen Quatsch??
jackle32
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 183
Erhaltene Danke: 7

Win7
Delphi XE5 Starter, RAD Studio XE7 Pro
BeitragVerfasst: Di 22.01.08 16:42 
Also ich würd so machen, sind noch ein paar andere Änderung drin und läuft so auch:

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:
23:
procedure TfrmVerbrauchAnzeigen.BtnST1Click(Sender: TObject); 
type
  TArrayErgebnis = array[1..15] of Integer;
var
  ergebnis : TArrayErgebnis;
  dezimalzahl, rest, x, y: integer;
begin
  dezimalzahl := StrToInt(Edit1.Text);
  x := 1;

  repeat
    rest := dezimalzahl mod 2;
    dezimalzahl := dezimalzahl div 2;
    ergebnis[x] := rest;
    Inc(x);
  until dezimalzahl<1;


  for y := 1 to x-1 do
  begin
    ShowMessage(FloatToStr(ergebnis[y]));
  end;
end;


So machen die Reihenfolge an manchen Stelle auch mehr Sinn und die Zahl wird jetzt richtig umgewandelt.

Gruß Jack

_________________
Es gibt keine dummen Fragen, nur dumme Antworten.
rob87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 461

Win Me, Win XP Home, Win XP Prof
Delphi 2007 Enterprise
BeitragVerfasst: Di 22.01.08 16:48 
user profile iconjackle32 hat folgendes geschrieben:
Also ich würd so machen, sind noch ein paar andere Änderung drin und läuft so auch:

ausblenden Delphi-Quelltext
1:
    Inc(x);					

Die Syntax kenn ich ned?

user profile iconjackle32 hat folgendes geschrieben:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
  for y := 1 to x-1 do
  begin
    ShowMessage(FloatToStr(ergebnis[y]));
  end;
end;

Und des macht mit dem y wesentlich mehr Sinn. Stimmt wohl.
:D :lol:
jackle32
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 183
Erhaltene Danke: 7

Win7
Delphi XE5 Starter, RAD Studio XE7 Pro
BeitragVerfasst: Di 22.01.08 16:53 
Inc(x) steht für Increment und bedeut, "zähl die Variable in Klammern hoch" (Drück doch mal F1, die Funktion ist oft echt hilfsreich).

Gruß Jack

P.S. hab ich die Stelle mit Inc(x) eine Zeile runter gerückt, da sonst immer erst in das zweite Feld des Array geschrieben hast

_________________
Es gibt keine dummen Fragen, nur dumme Antworten.
rob87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 461

Win Me, Win XP Home, Win XP Prof
Delphi 2007 Enterprise
BeitragVerfasst: Di 22.01.08 17:00 
Stimmt. Des hat ich übersehen. :wink:

Und des Inkrement kenn ich halt aus anderen Sprachen. Z.B.: PHP und Java: "x++"

Merci. Und vll. könntest du nochmal kurz wegen meinem Array schauen. Ich mach für heut Feierabend. :D :D
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 23.01.08 12:54 
Du deklarierst ein statisches Array, was auf dem Stack abgelegt wird, und initialisierst dieses nict. Der Compiler macht das für dich auch nicht, weshalb dort Datenmüll drin steht ...

BTW: Für die Umwandlung von Dezimal nach Binär gibt esunter Delphi einige andere gute Methoden ...
Siehe z.B. die Funktionen
ausblenden Delphi-Quelltext
1:
2:
3:
Function ByteToBinaryStr(AValue: Byte): String;
Function WordToBinaryStr(AValue: Word): String;
Function DWordToBinaryStr(AValue: DWORD): String;

in dieser Unit:
omorphia.svn.sourcef...ibrary/OIncProcs.pas

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
rob87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 461

Win Me, Win XP Home, Win XP Prof
Delphi 2007 Enterprise
BeitragVerfasst: Mi 23.01.08 14:02 
Wie initialisier ich denn das Array mit leeren Werten?

Also nicht mit Nullen.
Miri
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 657


Delphi 3 Prof., Delphi 2005 PE
BeitragVerfasst: Mi 23.01.08 14:05 
Was ist denn so schlimm an nullen?!
Du willst doch ne Dualzahl drin speichern, oder nicht?! Da passt doch ein mit nullen initialisiertes Array ganz gut! :gruebel:

_________________
Anonymes Eckenkind
rob87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 461

Win Me, Win XP Home, Win XP Prof
Delphi 2007 Enterprise
BeitragVerfasst: Mi 23.01.08 14:08 
Hmm.. Jein. Hast zwar eigentlich schon Recht, aber fänds leer doch besser. :D
Miri
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 657


Delphi 3 Prof., Delphi 2005 PE
BeitragVerfasst: Mi 23.01.08 14:13 
dann initialisier doch mit NULL ;-)

_________________
Anonymes Eckenkind
Xong
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 113

WIN 2000
Borland Developer Studio 2006
BeitragVerfasst: Mi 23.01.08 14:16 
user profile iconrob87 hat folgendes geschrieben:
... aber fänds leer doch besser. :D

Definiere "leer" mal auf den Ganzen Zahlen!
Oder anders gefragt: Wann ist eine Ganze Zahl "leer"?
Welchen Wert muss sie also haben?

Aufgrund des Wertebereichs der Ganzen Zahlen wirst du ein Array von Integer-Werten nicht mit "leer"en Einträgen füllen können.

Siehst du das Problem? =)
rob87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 461

Win Me, Win XP Home, Win XP Prof
Delphi 2007 Enterprise
BeitragVerfasst: Mi 23.01.08 14:18 
Okok. :D Also dann halt Null :D Wie mach ich des?

Einfach des Array mit dem Wert '0' füllen?
ausblenden Delphi-Quelltext
1:
2:
for x := 0 to (letzter index) do
  array[x] := 0;
nagel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 708

Win7, Ubuntu 10.10

BeitragVerfasst: Mi 23.01.08 14:21 
Ja
rob87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 461

Win Me, Win XP Home, Win XP Prof
Delphi 2007 Enterprise
BeitragVerfasst: Mi 23.01.08 14:25 
OK. Merci


<Thread gelöst>