Guest User

Untitled

a guest
Jul 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. Zahlensysteme_u1.pas 13.12.2009 18:02:54 Seite 1 von 4
  2. 1: unit Zahlensysteme_u1;
  3. 2:
  4. 3: interface
  5. 4:
  6. 5: uses
  7. 6: Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  8. 7: Dialogs, StdCtrls, ExtCtrls;
  9. 8:
  10. 9: type
  11. 10: TForm1 = class(TForm)
  12. 11: Label1: TLabel;
  13. 12: Edit_Eingabe: TEdit;
  14. 13: Label2: TLabel;
  15. 14: Edit_Ausgabe: TEdit;
  16. 15: RG_Auswahl: TRadioGroup;
  17. 16: Button1: TButton;
  18. 17: Label_Dez: TLabel;
  19. 18: procedure Button1Click(Sender: TObject);
  20. 19: private
  21. 20: { Private-Deklarationen }
  22. 21: public
  23. 22: { Public-Deklarationen }
  24. 23: end;
  25. 24:
  26. 25: var
  27. 26: Form1: TForm1;
  28. 27:
  29. 28: implementation
  30. 29:
  31. 30: {$R *.dfm}
  32. 31:
  33. 32: procedure TForm1.Button1Click(Sender: TObject);
  34. 33: var
  35. 34: set_ziffer: set of char;
  36. 35: s_ein : string; // Eingabezahl als Zeichenkette gelesen
  37. 36: s_aus : string; // Ausgabezahl als Zeichenkette
  38. 37: basis : integer; // Hilfsvariable "basis"
  39. 38: stellen : integer; // Anzahl der Stellen der Eingabezahl
  40. 39: potenz : integer; // Höchster vorkommender Stellenwert
  41. 40: wert : integer; // Wert der aktuellen Potenz
  42. 41: zahl : integer; // Wert der Eingabezahl im Dezimalsystem
  43. 42: i : integer; // Hilfsvariable: Schleifenzähler
  44. 43:
  45. 44: begin
  46. 45: case RG_Auswahl.ItemIndex of
  47. 46: 0: // Dezimalzahl => Binärzahl
  48. 47: begin
  49. 48: set_ziffer := ['0' .. '9']; // Mögliche Ziffern der Eingabezahl
  50. 49: // Zunächst bestimmen wir den Wert der Eingabezahl im Dez-System
  51. 50: s_ein := Edit_Eingabe.Text; // Einlesen der Eingabezahl
  52. 51: basis := 10; // Eingabezahl im Dezimalsystem: Basis = 10
  53. 52: stellen := length(s_ein); // Zählen der Stellen der Eingabezahl
  54. 53: // Überprüfen der Eingabeziffern
  55. Zahlensysteme_u1.pas 13.12.2009 18:02:54 Seite 2 von 4
  56. 54: for i := 1 to stellen do
  57. 55: if not (s_ein[i] in set_ziffer)
  58. 56: then begin
  59. 57: MessageBox(0,'Falsche Ziffer in Eingabe','Hinweis',MB_OK);
  60. 58: exit;
  61. 59: end;
  62. 60:
  63. 61: // Berechnen der höchsten Potenz der Eingabezahl
  64. 62: potenz := 1;
  65. 63: for i := 1 to stellen-1 do
  66. 64: potenz := potenz * basis;
  67. 65:
  68. 66: zahl := 0; // "Erden" der Variable zahl
  69. 67: for i := 1 to stellen do
  70. 68: begin
  71. 69: zahl := zahl + strtoint(s_ein[i]) * potenz;
  72. 70: potenz := potenz div basis;
  73. 71: end; // for-Schleife
  74. 72:
  75. 73: // Die Variable zahl enthält nun den Wert der Eingabezahl im Dez-System
  76. 74: Label_Dez.Caption := inttostr(zahl);
  77. 75:
  78. 76: // Nun wird "zahl" ins Ausgabesystem umgewandelt:
  79. 77: basis := 2; // Binärsystem
  80. 78: // Bestimmen der höchsten Potenz im Ausgabesystem
  81. 79: potenz := 1; // Aktuelle Potenz im Ausgabesystem
  82. 80: while potenz <= zahl do
  83. 81: potenz := potenz * basis;
  84. 82: potenz := potenz div basis;
  85. 83: // Jetzt geht es los: Ziffern der Ausgabezahl bestimmen
  86. 84: s_aus := ''; // Ausgabestring "erden"
  87. 85: repeat
  88. 86: wert := zahl div potenz; // Wert der aktuellen Stelle
  89. 87: zahl := zahl mod potenz; // Zahl anpassen
  90. 88: s_aus := s_aus + inttostr(wert);
  91. 89: potenz := potenz div basis; // neuer Stellenwert
  92. 90: until potenz = 0;
  93. 91: Edit_Ausgabe.Text := s_aus;
  94. 92: end; // of case 0
  95. 93: 1: // Dezimalzahl => Hexadezimalzahl
  96. 94: begin
  97. 95: set_ziffer := ['0' .. '9']; // Mögliche Ziffern der Eingabezahl
  98. 96: // Zunächst bestimmen wir den Wert der Eingabezahl im Dez-System
  99. 97: s_ein := Edit_Eingabe.Text; // Einlesen der Eingabezahl
  100. 98: basis := 10; // Eingabezahl im Dezimalsystem: Basis = 10
  101. 99: stellen := length(s_ein); // Zählen der Stellen der Eingabezahl
  102. 100: // Überprüfen der Eingabeziffern
  103. 101: for i := 1 to stellen do
  104. 102: if not (s_ein[i] in set_ziffer)
  105. 103: then begin
  106. 104: MessageBox(0,'Falsche Ziffer in Eingabe','Hinweis',MB_OK);
  107. 105: exit;
  108. 106: end;
  109. Zahlensysteme_u1.pas 13.12.2009 18:02:54 Seite 3 von 4
  110. 107:
  111. 108: // Berechnen der höchsten Potenz der Eingabezahl
  112. 109: potenz := 1;
  113. 110: for i := 1 to stellen-1 do
  114. 111: potenz := potenz * basis;
  115. 112:
  116. 113: zahl := 0; // "Erden" der Variable zahl
  117. 114: for i := 1 to stellen do
  118. 115: begin
  119. 116: zahl := zahl + strtoint(s_ein[i]) * potenz;
  120. 117: potenz := potenz div basis;
  121. 118: end; // for-Schleife
  122. 119:
  123. 120: // Die Variable zahl enthält nun den Wert der Eingabezahl im Dez-System
  124. 121: Label_Dez.Caption := inttostr(zahl);
  125. 122:
  126. 123: // Nun wird "zahl" ins Ausgabesystem umgewandelt:
  127. 124: basis := 16; // Binärsystem
  128. 125: // Bestimmen der höchsten Potenz im Ausgabesystem
  129. 126: potenz := 1; // Aktuelle Potenz im Ausgabesystem
  130. 127: while potenz <= zahl do
  131. 128: potenz := potenz * basis;
  132. 129: potenz := potenz div basis;
  133. 130: // Jetzt geht es los: Ziffern der Ausgabezahl bestimmen
  134. 131: s_aus := ''; // Ausgabestring "erden"
  135. 132: repeat
  136. 133: wert := zahl div potenz; // Wert der aktuellen Stelle
  137. 134: zahl := zahl mod potenz; // Zahl anpassen
  138. 135: case wert of
  139. 136: 10: s_aus := s_aus + 'A';
  140. 137: 11: s_aus := s_aus + 'B';
  141. 138: 12: s_aus := s_aus + 'C';
  142. 139: 13: s_aus := s_aus + 'D';
  143. 140: 14: s_aus := s_aus + 'E';
  144. 141: 15: s_aus := s_aus + 'F';
  145. 142: 0..9:s_aus := s_aus + inttostr(wert);
  146. 143: end; // of case
  147. 144: potenz := potenz div basis; // neuer Stellenwert
  148. 145: until potenz = 0;
  149. 146: Edit_Ausgabe.Text := s_aus;
  150. 147: end; // of case 1
  151. 148: 2: // Binärzahl => Dezimalzahl
  152. 149: begin
  153. 150: end; // of case 2
  154. 151: 3: // Binärzahl => Hexadezimalzahl
  155. 152: begin
  156. 153: end; // of case 3
  157. 154: 4: // Hexadezimalzahl => Dezimalzahl
  158. 155: begin
  159. 156: end; // of case 4
  160. 157: 5: // Hexadezimalzahl => Binärzahl
  161. 158: begin
  162. 159: end; // of case 5
  163. Zahlensysteme_u1.pas 13.12.2009 18:02:54 Seite 4 von 4
  164. 160: end; // of case
  165. 161: end; // of procedure TForm1.Button1Click
  166. 162:
  167. 163: end.
Add Comment
Please, Sign In to add comment