Autor Beitrag
n-regen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 202
Erhaltene Danke: 2



BeitragVerfasst: Sa 06.05.06 14:10 
Bezieht sich auf: www.delphi-forum.de/viewtopic.php?t=42364
Wie kann man eine Zahl in Einer, Zehner ... zerlegen und die Stellen jeweils in einzelne Variablen verteilen?
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 06.05.06 14:19 
Einfach mal nachsehn, was mod und div so machen ;)

AXMD
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Sa 06.05.06 14:22 
Ganz grob mit Mod und Div:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Einer := aZahl Mod 10;

// letzte Stelle wegdividieren
aZahl := aZahl Div 10;

// weitermachen
Zehner := aZahl Mod 10;
aZahl := aZahl Div 10;

Hunderter := aZahl Mod 10; 
// usw...


Kann man eleganter mit nem Array machen, und da die einzelnen Ziffern reinschreiben, und dann mit ner Schleife (Abbruchbedingung: aZahl = 0)

Edit: Zu langsam...

_________________
We are, we were and will not be.
nullplan001
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 212

Win 2000 Professional, Debian Linux 4.0 (Etch,Stable)
Pascal (FreePascal 2.0.2, TurboPascal 7.0), C(++) (G++/GCC 3.4.2 + MinGW), Java (JDK 1.5.0_07), PHP (PHP 5.1.4)
BeitragVerfasst: Sa 06.05.06 14:32 
Das ist eigentlich ganz einfach:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
procedure Zerlegen(bla:longword;var output:^word);
begin
  output^ := bla mod 10;
  Zerlegen(bla div 10,output + 2);
end;

Naja, das ist die rekursive Variante. Ansonsten kann ich folgende Version anbieten:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
function Stelle(source:longword;n:byte):byte;
begin
  Result := source div exp((n - 1) * ln(10)) mod 10;
end;

Diese Funktion berechnet die n-te Stelle von source.
Tschö,
nullplan

_________________
Ich fahr' nicht selber, weil ich festgestellt habe: ich fahre zu emotional. Bin 180 gefahren wo 30 erlaubt war... -- Jürgen von der Lippe
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 06.05.06 14:35 
user profile iconnullplan001 hat folgendes geschrieben:
Das ist eigentlich ganz einfach:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
procedure Zerlegen(bla:longword;var output:^word);
begin
  output^ := bla mod 10;
  Zerlegen(bla div 10,output + 2);
end;


Sorry, aber das kann nicht funktionieren, weil du keine Abbruchbedingung hast (mal abgesehn von der Stackgröße :mrgreen: ). Außerdem: warum pfuscht du hier mit Zeigern rum?

AXMD
nullplan001
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 212

Win 2000 Professional, Debian Linux 4.0 (Etch,Stable)
Pascal (FreePascal 2.0.2, TurboPascal 7.0), C(++) (G++/GCC 3.4.2 + MinGW), Java (JDK 1.5.0_07), PHP (PHP 5.1.4)
BeitragVerfasst: Sa 06.05.06 14:53 
Ups, das war wohl nicht nichts. :oops: Egal, ich hab ja noch eine weitere Funktion mitgeliefert. Hat eigentlich Delphi eine Potenzierungsfunktion? Dann brauch ich mich nicht mehr mit exp und ln rumzuschlagen. Die obige Methode ist im übrigen mit Potenz- und Logarithmengesetzen beweisbar, dass sie "nur" potenziert. Aber egal, zweiteres funktioniert garantiert, ersteres war ein Experiment.
Tschö,
nullplan

_________________
Ich fahr' nicht selber, weil ich festgestellt habe: ich fahre zu emotional. Bin 180 gefahren wo 30 erlaubt war... -- Jürgen von der Lippe
LLCoolDave
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 212

Win XP
Delphi 2005
BeitragVerfasst: Sa 06.05.06 15:45 
Es gäbe Power, ich bevorzuge aber die Exp/Ln variante aber auch, weil man damit den Informatiklehrer gerne mal kurzfristig verwirren kann, weil er nicht damit rechnet.
n-regen Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 202
Erhaltene Danke: 2



BeitragVerfasst: Sa 06.05.06 16:02 
Ich hab mir selber mal was ausgedacht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
s:=IntToStr(Input);
  l:=length(s);
  if l >= 1 then
  a := Strtoint(s[1]);
  if l >= 2 then
  b := Strtoint(s[2]);
  if l >= 3 then
  c := Strtoint(s[3]);
  if l >= 4 then
  d := Strtoint(s[4]);
  if l = 5 then
  e := Strtoint(s[5]);
  if l > 5 then

Bitte mal nachschauen ob und wenn ja was falsch is und mir sagen wiesos da ne fehlermeldung gibt.
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 06.05.06 16:12 
user profile iconn-regen hat folgendes geschrieben:
Bitte mal nachschauen ob und wenn ja was falsch is und mir sagen wiesos da ne fehlermeldung gibt.


Wieso lässt du dir nicht vom Compiler sagen, was falsch ist? Dazu ist er ja da. Außerdem würde ich an deiner Stelle das ein oder andere Tutorium zu Schleifen lesen.

AXMD
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 06.05.06 18:13 
ungetestet:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
var
  stellen: array of integer;
  zahl: integer;
begin
  while zahl >= 10 do
  begin
    setlength(stellen, length(stellen) + 1);
    stellen[high(stellen)] := zahl mod 10;
    zahl := zahl div 10;
  end;
end;


dann hast du in deinem array alle stellen drin.