Autor Beitrag
jonathanschmid
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 11.02.06 23:46 
Hallo!
Ich habe grad zu einer Aufgabenstellung in meinem Grundlagen-Delphi-Buch folgenden Quelltext geschrieben:
ausblenden volle Höhe 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:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
  var i,lepos: Byte;
      sum    : Integer;
      inhalt : ShortString;
  begin
  inhalt:=Edit1.Text;              //Inhalt des Editfeldes holen  
  lepos:=Length(inhalt);           //Letzte Position dieses Inahltes für Zählschleife
  sum:=0;                          // sum initialisieren
  for i:=0 to lepos do
    begin
    if (Copy(inhalt,i,1)>='0') and (Copy(inhalt,i,1)<='9') then        
      sum:=sum+StrToInt(Copy(inhalt,i,1))    //Wenn Zahl, dann dazurechnen
    end;
  Label1.Caption:=IntToStr(sum)      //Quersumme ausgeben
  end;
end.

Ich weiß, das ist eigentlich ganz simpel... Und deswegen frag ich mich auch warum das einfach nicht funktioniert! Also das Problem ist, dass das Ergebniss um eins zu hoch ist.
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: So 12.02.06 00:06 
user profile iconjonathanschmid hat folgendes geschrieben:
Hallo!
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
procedure TForm1.Button1Click(Sender: TObject);
  var i,lepos: Byte;
      sum    : Integer;
      inhalt : ShortString;
  begin
  inhalt:=Edit1.Text;              //Inhalt des Editfeldes holen  
  lepos:=Length(inhalt) - 1;           //Letzte Position dieses Inahltes für Zählschleife
  sum:=0;                          // sum initialisieren
  for i:=0 to lepos do
    begin
    if (Copy(inhalt,i,1)>='0') and (Copy(inhalt,i,1)<='9') then        
      sum:=sum+StrToInt(Copy(inhalt,i,1))    //Wenn Zahl, dann dazurechnen
    end;
  Label1.Caption:=IntToStr(sum)      //Quersumme ausgeben
  end;
end.



Versuch mal das (mit -1).
Es geht darum, dass Copy einen Index erwartet, also die Zählung bei 0 losgeht, Length aber die Anzahl der Buchstaben zurückgibt. Der Zähler bei Length beginnt also bei 1.
Das wir dir noch oft begegnen.

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
jonathanschmid
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 12.02.06 00:08 
Jetzt ist es um 1 zu hoch!!!

Erstaunlich ist, dass die Auflösung der Aufgabe in meinem Buch ohne das -1 ist... Da hat wohl der Autor einen Fehler gemacht (nicht der 1. in dem Buch :( )
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: So 12.02.06 00:15 
wie gehts jetzt, oder nicht?

Und wenn es immer ein zu hoch ist, dann zieh doch einfach eins ab, das ist zwar die durty-Method, aber wenn das so ist, kannste es auch so machen

_________________
Programmers never die, they just GOSUB without RETURN
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: So 12.02.06 09:46 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm1.Button1Click(Sender: TObject);
var i,lepos: Byte;
      sum    : Integer;
      inhalt : ShortString;
  begin
  inhalt:=Edit1.Text;
  lepos:=Length(inhalt);
  sum:=0;
  for i:=1 to lepos do {1 to lepos !}
    begin
    if (Copy(inhalt,i,1)>='0') and (Copy(inhalt,i,1)<='9') then
      sum:=sum+StrToInt(Copy(inhalt,i,1))
    end;
  Label1.Caption:=IntToStr(sum)
end;
jonathanschmid
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 12.02.06 10:47 
Vielen Dank Blackheart! Das ist die Lösung...
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: So 12.02.06 11:22 
Quersummenberechnung über Stringroutinen. Sowas steht in einem Buch? Wenn man was über ne Zahl wissen möchte, dann arbeitet man mit der Zahl:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
function Quersumme(AZahl: Integer): Integer;
begin
  result := (aZahl Mod 10);
  while aZahl > 0 do
  begin
    aZahl := aZahl DIV 10;
    result := result + (aZahl Mod 10);
  end;
end;
Aufruf der Funktion z.B. so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage(inttostr(Quersumme(StrToInt(Edit1.Text))));
end;

_________________
We are, we were and will not be.
jonathanschmid
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 13.02.06 14:27 
Tja, das glaub ich dir sofort, dass man das normalerweise so macht, aber ich hab leider noch keine Ahnung von functions. Das ist ein Delphi-Grundlagenbuch und functions kommen eben weiter hinten...
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: So 02.12.07 13:15 
@Gausi: Die String-Prozedur ist schneller als die mit der Zahl. Ich verwende zwar nicht die gleiche aber eine ähnliche. Den Quelltext gibt's nach dem Weihnachts-Gewinnspiel :wink:
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: So 02.12.07 13:18 
Ja? Mir egal. Mein Rätsel-1-Knacker braucht ca. ne halbe Sekunde, ohne hochoptimierte Einzeltests. Das reicht mir. :mrgreen:

_________________
We are, we were and will not be.
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: So 02.12.07 14:13 
Naja, ich dachte mein Prog braucht ca. eine hable Stunde, das stimmt auch, aber leider findet er kein Ergebnis... Muss wohl noch den Fehler suchen. Aber ich will ja nicht zu sehr vom Thema abschweifen.