| Autor |
Beitrag |
jonathanschmid
Ehemaliges Mitglied
Erhaltene Danke: 1
|
Verfasst: Sa 11.02.06 23:46
Hallo!
Ich habe grad zu einer Aufgabenstellung in meinem Grundlagen-Delphi-Buch folgenden Quelltext geschrieben:
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 public end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); var i,lepos: Byte; sum : Integer; inhalt : ShortString; begin inhalt:=Edit1.Text; lepos:=Length(inhalt); sum:=0; 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)) end; Label1.Caption:=IntToStr(sum) 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
      
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)
|
Verfasst: So 12.02.06 00:06
jonathanschmid hat folgendes geschrieben: | Hallo!
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; lepos:=Length(inhalt) - 1; sum:=0; 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)) end; Label1.Caption:=IntToStr(sum) 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
|
Verfasst: 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
      
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)
|
Verfasst: 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
      
Beiträge: 2195
XP
D3Prof, D6Pers.
|
Verfasst: So 12.02.06 09:46
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 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
|
Verfasst: So 12.02.06 10:47
Vielen Dank Blackheart! Das ist die Lösung...
|
|
Gausi
      
Beiträge: 8554
Erhaltene Danke: 481
Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
|
Verfasst: So 12.02.06 11:22
_________________ We are, we were and will not be.
|
|
jonathanschmid
Ehemaliges Mitglied
Erhaltene Danke: 1
|
Verfasst: 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
      
Beiträge: 2598
Erhaltene Danke: 156
Ubuntu 13.04, Win 7
C# (VS 2013)
|
Verfasst: 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 
|
|
Gausi
      
Beiträge: 8554
Erhaltene Danke: 481
Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
|
Verfasst: 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. 
_________________ We are, we were and will not be.
|
|
Yogu
      
Beiträge: 2598
Erhaltene Danke: 156
Ubuntu 13.04, Win 7
C# (VS 2013)
|
Verfasst: 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.
|
|