Umrechnung von Binär in Dezimalzahlen und umgekehrt

Beschreibung

Mit dem Programm binaer_dezimal.pas kann man Dezimalzahlen in Binäzahlen umrechnen und umgekehrt. Dabei wird dies in LaTeX-Code ausgegeben. Da Programm bietet verschiedene Berechnungsmöglichkeiten.

  • Binärzahlen in Dezimalzahlen umrechnen
    • einfache Umrechnung
    • Binär von einer Zahl bis zu einer anderen Zahl zählen
    • Vorgänger und Nachfolger bestimmen
    • i Zahlen binär weiterzählen
  • Dezimalzahlen in Binärzahlen umrechnen

Als Beispiel ist hier die Ausgabe für den Vorgänger und Nachfolger von 101000001010 gezeigt:

 binaer.tex 
Vorgänger und Nachfolger von $(101000001010)_2$ sind: $\begin{array}{rcl} (101000001001)_2 & = & 2569 \quad = \quad 2048 + 512 + 8 + 1 \\ & = & 1 \* {\color[rgb]{1,0,0}2048} + 0 \* {\color[rgb]{1,0,0}1024} + 1 \* {\color[rgb]{1,0,0}512} + 0 \* {\color[rgb]{1,0,0}256} + 0 \* {\color[rgb]{1,0,0}12 8} + 0 \* {\color[rgb]{1,0,0}64} + 0 \* {\color[rgb]{1,0,0}32} + 0 \* {\color[rgb]{1,0,0}16} + 1 \* {\color[rgb]{1,0,0}8} + 0 \* {\color[rgb]{1,0,0}4} + 0 \* {\color[rgb]{1,0,0}2} + 1 \* {\color[rgb]{1,0,0}1} \\ (101000001010)_2 & = & 2570 \quad = \quad 2048 + 512 + 8 + 2 \\ & = & 1 \* {\color[rgb]{1,0,0}2048} + 0 \* {\color[rgb]{1,0,0}1024} + 1 \* {\color[rgb]{1,0,0}512} + 0 \* {\color[rgb]{1,0,0}256} + 0 \* {\color[rgb]{1,0,0}12 8} + 0 \* {\color[rgb]{1,0,0}64} + 0 \* {\color[rgb]{1,0,0}32} + 0 \* {\color[rgb]{1,0,0}16} + 1 \* {\color[rgb]{1,0,0}8} + 0 \* {\color[rgb]{1,0,0}4} + 1 \* {\color[rgb]{1,0,0}2} + 0 \* {\color[rgb]{1,0,0}1} \\ (101000001011)_2 & = & 2571 \quad = \quad 2048 + 512 + 8 + 2 + 1 \\ & = & 1 \* {\color[rgb]{1,0,0}2048} + 0 \* {\color[rgb]{1,0,0}1024} + 1 \* {\color[rgb]{1,0,0}512} + 0 \* {\color[rgb]{1,0,0}256} + 0 \* {\color[rgb]{1,0,0}12 8} + 0 \* {\color[rgb]{1,0,0}64} + 0 \* {\color[rgb]{1,0,0}32} + 0 \* {\color[rgb]{1,0,0}16} + 1 \* {\color[rgb]{1,0,0}8} + 0 \* {\color[rgb]{1,0,0}4} + 1 \* {\color[rgb]{1,0,0}2} + 1 \* {\color[rgb]{1,0,0}1} \\ \end{array}$ end.

Programm

Zunächst benötigt man die Datei funktionen.pas:

 funktionen.pas 
unit funktionen; interface function binaerconvert(zahl: string) : integer; function bc2(zahl : string) : integer; function d2b(zahl : integer) : string; implementation function binaerconvert(zahl: string) : integer; begin if (zahl<>'') then begin if (copy(zahl, length(zahl), 1)='1') then binaerconvert:=2*binaerconvert(copy(zahl,1,length(zahl)-1))+1 else binaerconvert:=2*binaerconvert(copy(zahl,1,length(zahl)-1)); end else binaerconvert:=0; end; { binaerconvert } function bc2(zahl : string) : integer; var i : integer; res : integer; begin res:=0; i:=0; while (i

Und hier kommt das Pascal-Programm:

 binaer_dezimal.pas 
program binaer_und_dezimal_bestimmungen; uses funktionen; procedure dezimalinschritten(zahl : integer); var i,groesster,tempzahl : integer; first : boolean; begin groesster:=1; while (2*groesster<=zahl) do groesster:=2*groesster; i:=groesster; tempzahl:=zahl; first:=true; writeln('$\begin{array}{rcl}'); write(' '); write(tempzahl,' & = & '); while (i<>0) do begin if (tempzahl>=i) then begin if (not first) then write(' + '); write(i); first:=false; tempzahl:=tempzahl-i; end; i:=i div 2; end; writeln(' \\'); i:=groesster; tempzahl:=zahl; first:=true; write(' & = & '); while (i<>0) do begin if (not first) then write(' + '); if (tempzahl>=i) then begin write('1 \* {\color[rgb]{1,0,0}',i,'}'); tempzahl:=tempzahl-i; end else write('0 \* {\color[rgb]{1,0,0}',i,'}'); first:=false; i:=i div 2; end; writeln(' \\'); i:=groesster; tempzahl:=zahl; write(' & = & '); while (i<>0) do begin if (tempzahl>=i) then begin write('1'); tempzahl:=tempzahl-i; end else write('0'); i:=i div 2; end; writeln(' \\'); writeln('\end{array}$'); writeln; end; procedure binaerschritten3(zahl : integer); var i,groesster,tempzahl : integer; first : boolean; begin groesster:=1; while (2*groesster<=zahl) do groesster:=2*groesster; i:=groesster; tempzahl:=zahl; first:=true; write(' & = & '); while (i<>0) do begin if (tempzahl>=i) then begin if (not first) then write(' + '); write(i); first:=false; tempzahl:=tempzahl-i; end; i:=i div 2; end; writeln(' \\'); i:=groesster; tempzahl:=zahl; first:=true; write(' & = & '); while (i<>0) do begin if (not first) then write(' + '); if (tempzahl>=i) then begin write('1 \* {\color[rgb]{1,0,0}',i,'}'); tempzahl:=tempzahl-i; end else write('0 \* {\color[rgb]{1,0,0}',i,'}'); first:=false; i:=i div 2; end; writeln(' \\'); end; { binaerschritten3 } procedure binaerschritten(zahl : integer); var i,groesster,tempzahl : integer; first : boolean; begin groesster:=1; while (2*groesster<=zahl) do groesster:=2*groesster; i:=groesster; tempzahl:=zahl; first:=true; write(' \quad = \quad '); while (i<>0) do begin if (tempzahl>=i) then begin if (not first) then write(' + '); write(i); first:=false; tempzahl:=tempzahl-i; end; i:=i div 2; end; writeln(' \\'); i:=groesster; tempzahl:=zahl; first:=true; write(' & = & '); while (i<>0) do begin if (not first) then write(' + '); if (tempzahl>=i) then begin write('1 \* {\color[rgb]{1,0,0}',i,'}'); tempzahl:=tempzahl-i; end else write('0 \* {\color[rgb]{1,0,0}',i,'}'); first:=false; i:=i div 2; end; writeln(' \\'); end; { binaerschritten } procedure binaerschritten2(zahl : integer); var i,groesster,tempzahl : integer; first : boolean; begin groesster:=1; while (2*groesster<=zahl) do groesster:=2*groesster; i:=groesster; tempzahl:=zahl; first:=true; write(' & = & '); while (i<>0) do begin if (not first) then write(' + '); if (tempzahl>=i) then begin write('1 \* {\color[rgb]{1,0,0}',i,'}'); tempzahl:=tempzahl-i; end else write('0 \* {\color[rgb]{1,0,0}',i,'}'); first:=false; i:=i div 2; end; writeln(' \\'); end; { binaerschritten2 } var eingabe : char; zahl : integer; zahl1 : integer; zahl2 : integer; binaer : string[255]; binaer1 : string[255]; binaer2 : string[255]; binaerx : string[255]; i,x : integer; begin writeln; writeln('Berechnung von Binärzahlen in Dezimalzahlen und umgekehrt'); writeln('-------------------------------------------------'); writeln('Eingabe für binär = b, dezimal = d'); readln(eingabe); case (eingabe) of 'b', 'B' : begin writeln('einfach binär 1'); writeln('binär von bis 2'); writeln('binär vorgänger, nachfolger 3'); writeln('binär i weiter 4'); readln(i); case (i) of 1 : begin while (not eof) do begin readln(binaer); zahl := bc2(binaer); writeln('Es ist: '); writeln; writeln('$\begin{array}{rcl}'); write(' '); write('(',binaer,')_2'); binaerschritten3(zahl); writeln(' & = & ' ,zahl, ' \\'); writeln('\end{array}$'); writeln; end; end; 2 : begin while (not eof) do begin readln(binaer1); readln(binaer2); zahl1:=bc2(binaer1); zahl2:=bc2(binaer2); if (zahl1 < zahl2) then begin writeln('\item Es ist: '); writeln; writeln('$\begin{array}{rclcl}'); for x:=zahl1 to zahl2 do begin binaerx:=d2b(x); writeln(' (',binaerx,')_2 & = & ' ,x, ' '); binaerschritten2(x); end; writeln('\end{array}$'); end else writeln('Dies ist keine ordentlich eingabe'); end; end; 3 : begin while (not eof) do begin readln(binaer1); zahl1:=bc2(binaer1); zahl2:=zahl1+1; writeln('Vorgänger und Nachfolger von $(' ,binaer1,')_2$ sind:'); writeln; writeln('$\begin{array}{rcl}'); for x:=zahl1-1 to zahl2 do begin binaerx:=d2b(x); writeln(' (',binaerx,')_2 & = & ' ,x, ' '); binaerschritten(x); end; writeln('\end{array}$'); writeln; end; end; 4 : begin while (not eof) do begin readln(binaer1); readln(zahl2); zahl1:=bc2(binaer1); writeln('Die ' ,zahl2, ' Nachfolger von $(' ,binaer1,')_2$ sind:'); zahl2:=zahl2+zahl1; writeln; writeln('$\begin{array}{rcl}'); for x:=zahl1 to zahl2 do begin binaerx:=d2b(x); writeln(' (',binaerx,')_2 & = & ' ,x, ''); binaerschritten(x); end; writeln('\end{array}$'); writeln; end; end; end; { case } end; { case } 'd', 'D' : begin while (not eof) do begin readln(zahl); writeln('Es ist: '); writeln; dezimalinschritten(zahl); end; end; end; { case } end.

Letzte Änderung: 26.04.2012: 17:23:40 von X. Rendtel

Creative Commons Lizenzvertrag
Dieses Werk bzw. Inhalt steht unter einer Creative Commons Namensnennung-Weitergabe unter gleichen Bedingungen 3.0 Unported Lizenz.
Beruht auf einem Inhalt unter www.rendtel.de.