Autor Beitrag
Sotov
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Mi 14.06.06 16:55 
Ich möchte Dezimalzahlen in Binär umwandeln, unzwar mit einer Rekursion. Die Rekursion besteht darin, dass wenn man die Dezimalzahl durch 2 teilt, kommt etweder ein Rest oder eben keins. Wenn ein Rest kommt, dann ist es 1 wenn keins dann 0
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
117 :2 rest 1
58  :2 rest 0
29  :2 rest 1
14  :2 rest 0
7   :2 rest 1
3   :2 rest 1
1   :2 rest 1 
0   :2 rest 0

Also Binär ist 117 : 1110101

Nur habe ich ein Problem beim Erstellen der Rekursion. Falls mir jemand helfen könnte, oder sonst irgendwelche Tipps für mich parat hat...

Danke im Voraus
DelphiAnfänger
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 112

Win XP, Firefox 2.0, IE6
Delphi 5 Prof., Delphi 2005 PE
BeitragVerfasst: Mi 14.06.06 17:01 
deine rechnug ist falsch!
dezimal: 127
binär: 1111111

binär: 1110101
dezimal: 75

EDIT ich hatte noch ne null drangehängt :oops: (11101010 ist 234)


Zuletzt bearbeitet von DelphiAnfänger am Mi 14.06.06 17:23, insgesamt 1-mal bearbeitet
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 14.06.06 17:09 
@DelphiAnfänger: eine 7-bit-Binärzahl kann nicht größer als 127 sein. Zumal dann nicht, wenn Du so rechnest, wie Du's gezeigt hast. 1110101 = 64 + 32 + 16 + 4 + 1 = 117

Zum Thema Rekursion:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
Function Rekurse(Number: Integer; BitCount: Integer): String;
const Digits = '01';
Begin
    If Bitcount = 0 Then
        Result := ''
    else
        Result := Digits[(Number shr (BitCount - 1)) and 1] + Rekurse(Number and (1 shl BitCount) - 1, Bitcount - 1);
end;


Ist eine von vielen Rekursiven Möglichkeiten ...

_________________
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.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 14.06.06 17:13 
Moin!

Mein Vorschlag:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm1.Button1Click(Sender: TObject);

  function DezToBin(Value: Integer): String;
  begin
    if (Value > 1then
      Result := DezToBin(Value div 2) +Char( (Value mod 2) +48)
    else
      Result := Char( (Value mod 2) +48);
  end;

begin
  Edit2.Text := DezToBin(StrToIntDef(Edit1.Text,0));
end;

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Phantom1
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 390



BeitragVerfasst: Mi 14.06.06 17:18 
Noch ein Vorschlag ;O)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function DezToBin(Dez: LongInt): string;
begin
  if Dez>0 then
    Result:=DezToBin(Dez Div 2)+IntToStr(Dez Mod 2);
end;
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 14.06.06 17:20 
@user profile iconPhantom1:

DezToBin(0) :arrow: :?: :|

Abgesehen davon ist das meine Lösung... ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Sotov Threadstarter
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Mi 14.06.06 17:26 
Danke für eure Hilfe. Ich versuche jetzt, obwohl eure Vorschläge enthalten sehr viele mir unbekannte Sachen, wie zb DezToBin was wir bestimmt nicht verwenden dürfen, weil wir es noch gar nicht hatten... oder const Digits = '01' oder shr ...

Gibt es nicht eine ganz einfache Lösung?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 14.06.06 17:30 
Moin!

:? Hua... Ganz blutiger Anfänger... ;)

user profile iconSotov hat folgendes geschrieben:
obwohl eure Vorschläge enthalten sehr viele mir unbekannte Sachen, wie zb DezToBin

Öhm, das ist die Funktion, die du schreiben wolltest, das ist also beabsichtigt neu. :D

user profile iconSotov hat folgendes geschrieben:
weil wir es noch gar nicht hatten...
[...]
Gibt es nicht eine ganz einfache Lösung?

Mach mal ein paar Anfänger-Tutorials oder frag deinen Lehrer und pass mal besser im Unterricht auf. ;) Wenn ihr schon bei Rekursion seid, dann hast du nicht erst seit gestern Informatik. :mrgreen: Oder? ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Sotov Threadstarter
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Mi 14.06.06 17:36 
Leider habe ich einen behinderten Lehrer, der absolut gar nix erklärt und eigentlich nur will das wir uns selbder überlegen. Aber wenn keine Grundkenntnise vorhanden sind, können wir uns gar nichts so richtig alles selber überlegen.
Ich habe Informatik seit einen halben Jahr und wir sind schon bei Rekursion!

Nun gut. Ich versuche es jetzt! Obwohl ich glaube nicht, dass es klappt!

Vielen Dank für die Hilfe!
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: So 25.06.06 08:51 
Scheint eine sehr soziale Schule zu sein, wenn die sogar behinderte Lehrer einstellen :roll: .

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)