Autor Beitrag
Danf
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 27



BeitragVerfasst: Do 04.02.10 11:51 
Servus,

Ich hatte jetzt ein 2-wöchiges Praktikum. Erster Tag wieder Schule, Informatik.. und alles ist wie weggefegt.

Folgende Aufgabe hab ich bekommen:
Zitat:
Schreibe ein Programm mit Hilfe von Prozedur oder Funktion mit Parametern, dass Zinsen/Zinssatz/Kapital ausrechnet.

// Das Programm soll, je nachdem was man eingibt, die entsprechende Zahl ausrechnen.

Nun heißt es zuerst Formel raussuchen (ohne n (n = Jahre)) die da lautet

Z = K*p/100
Nun die oben genannte Formel umstellen, damit alle 3 Formeln vorhanden sind

Z = K*p/100
K = 100*Z/p
p = Z*100/K

Z = Zinsen
K = Kapital
p = Zinsatz in %

Soweit sogut...

Ohne Prozedur mit Parameter wäre die Aufgabe soweit im Rahmen des Möglichen:

... An dieser Stelle merk ich grade das ich des irgendwie gar net auf die reihe kriege, auch ohne proceudre mit parameter... sieht aber in etwa so aus:
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:
36:
37:
38:
39:
40:
41:
42:
procedure TForm1.Button1Click(Sender: TObject);
  VAR K, Z, p : real;
  begin

 IF edit1.Text = ' '
 THEN
 begin
  K := strtofloat(edit1.Text);
  Z := strtofloat(edit2.text);
  p := strtofloat(edit3.Text);
  K := 100*Z/p;
  edit1.text:=FloatToStr(K);
  edit2.text:=FloatToStr(Z);
  edit3.text:=FloatToStr(p);
 end
   ELSE
     IF edit2.Text = ' '
     THEN
     begin
      K := strtofloat(edit1.Text);
      Z := strtofloat(edit2.text);
      p := strtofloat(edit3.Text);
      Z := K*p/100;
      edit1.text:=FloatToStr(K);
      edit2.text:=FloatToStr(Z);
      edit3.text:=FloatToStr(p);
     end
       ELSE
         IF edit3.Text = ' '
         THEN
         begin
          K := strtofloat(edit1.Text);
          Z := strtofloat(edit2.text);
          p := strtofloat(edit3.Text);
          p := Z*100/K;
          edit1.text:=FloatToStr(K);
          edit2.text:=FloatToStr(Z);
          edit3.text:=FloatToStr(p);
         end;

 end;
end

Das sieht aber auch irgendwie falsch aus.. hab auch noch andere Versionen geschrieben, aber die wollen alle nicht wie ich will :(.

Kann mir evtl jmd helfen :!

Mit freundlichen Grüßen
Danf

Moderiert von user profile iconNarses: Quote-Tags eingefügt.
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Do 04.02.10 12:16 
Hier mal ein einfaches Beispiel, vieleicht hilft dir das etwas weiter.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
function Rechne(Zahl1, Zahl2:Integer):String;
begin
  Result:=IntToStr(Zahl1 + Zahl2);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label.Caption:=Rechne(5,5);
end;
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Do 04.02.10 12:19 
Angesichts dessen das dir viele Grundlagen fehlen empfehle ich dir mal das Delphi-Wikibook und Christian's Crashkurs. Damit solltest du, denk ich, deine Probleme aus der Welt schaffen können. ;)

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
martin300
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 186
Erhaltene Danke: 2



BeitragVerfasst: Do 04.02.10 12:20 
Ist hier ein Leerzeichen zuviel?
ausblenden Delphi-Quelltext
1:
IF edit1.Text = ' '					
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Do 04.02.10 12:26 
das dürfte wohl das geringste Problem sein denn ob ich jetzt versuche ein leerzeichen in eine zahl umzuwandeln oder gar kein zeichen is dem programm schnuppe und in beiden fällen sollte er ne exception werfen. ;)

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
martin300
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 186
Erhaltene Danke: 2



BeitragVerfasst: Do 04.02.10 12:34 
Die Exception wird doch erst zur Laufzeit auftreten. Bei der Abfrage wird aber nie eine Berechnung durchgeführt.
Danf Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 27



BeitragVerfasst: Do 04.02.10 12:53 
Sooo.. ich hab jetzt erstmal nen Kaffee getrunken, gefrühstückt, eine geraucht und mich nochmal drangesetzt...

Ich habs nun hingekriegt, dass das Programm einwandfrei funktioniert, allerdings ohne Prozedur mit Parameter.


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:
procedure TForm1.Button1Click(Sender: TObject);
  VAR K, Z, p : real;
  begin

 IF edit1.Text = ''
 THEN
 begin
  Z := strtofloat(edit2.text);
  p := strtofloat(edit3.Text);
  K := 100*Z/p;
  edit1.text:=FloatToStr(K);
 end
    ELSE
     IF edit2.Text = ''
     THEN
     begin
      K := strtofloat(edit1.Text);
      p := strtofloat(edit3.Text);
      Z := K*p/100;
      edit2.text:=FloatToStr(Z);
     end
       ELSE
         IF edit3.Text = ''
         THEN
         begin
          K := strtofloat(edit1.Text);
          Z := strtofloat(edit2.text);
          p := Z*100/K;
          edit3.text:=FloatToStr(p);
         end;

 end;

end.


@Blackheart666
jo, eine einfache Funktion geht ja noch.. baer diese auf das oben genannte Programm anzuwenden... That's the problem.

@all
Danke für die Antworten, probiere es jetzt nochmal mit function und procedure

LG
Danf
Danf Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 27



BeitragVerfasst: Do 04.02.10 13:27 
So, ich hbas noch ne runde probiert, und es klappt einfach nicht, es ist zum heulen.

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:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
procedure TForm1.Rechne(K, Z, p : real);
  begin

 IF edit1.Text = ''
 THEN
 begin
  Z := strtofloat(edit2.text);
  p := strtofloat(edit3.Text);
  K := 100*Z/p;
  edit1.text:=FloatToStr(K);
 end
    ELSE
     IF edit2.Text = ''
     THEN
     begin
      K := strtofloat(edit1.Text);
      p := strtofloat(edit3.Text);
      Z := K*p/100;
      edit2.text:=FloatToStr(Z);
     end
       ELSE
         IF edit3.Text = ''
         THEN
         begin
          K := strtofloat(edit1.Text);
          Z := strtofloat(edit2.text);
          p := Z*100/K;
          edit3.text:=FloatToStr(p);
         end;

 end;



procedure TForm1.Button1Click(Sender: TObject);
VAR K, Z, p : real;
begin

K := strtofloat(edit1.text);
Z := strtofloat(edit2.text);
p := strtofloat(edit3.text);

Rechne(K, Z, p)
end;

end.


Wo liegt nun der Fehler :(?

PS:
Hilfen hab ich mir durchgelesen.. baer da sind überall nur "einfache" Beispiele aus denen ich nur relativ wenig ersehen kann.
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Do 04.02.10 13:30 
ganz einfach, was passiert wenn in allen feldern was drin steht? ;) dann wird dein code die ausgeführt weil die bedingungen in den if-teilen immer false sind ;)

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: Do 04.02.10 13:32 
Was konkret "klappt nicht"? Fehlermeldungen?

_________________
Wissenschaft schafft Wissenschaft, denn Wissenschaft ist Wissenschaft, die mit Wissen und Schaffen Wissen schafft. (myself)
Danf Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 27



BeitragVerfasst: Do 04.02.10 13:35 
user profile iconelundril hat folgendes geschrieben Zum zitierten Posting springen:
ganz einfach, was passiert wenn in allen feldern was drin steht? ;) dann wird dein code die ausgeführt weil die bedingungen in den if-teilen immer false sind ;)

lg elundril


Leider nicht

(beispiel)
Edit1 = 1000 (Kapital)
Edit2 = 50 (Zinsen)
Edit3 = (Leer) (Zinssatz)

*Auf Button drück*

Krach bum Programm abgestürzt die alte Leier halt ...
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Do 04.02.10 13:36 
ja, weil du versuchst einen nix in einen float umzuwandeln. was isn die gleitkommadarstellung von der zeichenkette ''? eben, der compiler weiß das auch ned ;)

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Danf Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 27



BeitragVerfasst: Do 04.02.10 13:44 
user profile iconelundril hat folgendes geschrieben Zum zitierten Posting springen:
ja, weil du versuchst einen nix in einen float umzuwandeln. was isn die gleitkommadarstellung von der zeichenkette ''? eben, der compiler weiß das auch ned ;)


Achso, jetzt versteh ich. Das heißt ich muss auch im unteren Teil des Programms eine IF-Verschachtelung anwenden nicht wahr?

Also so:


ausblenden 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:
procedure TForm1.Button1Click(Sender: TObject);
VAR K, Z, p : real;
begin

 IF edit1.Text = ''
 THEN
 begin
  Z := strtofloat(edit2.text);
  p := strtofloat(edit3.Text);
 end
    ELSE
     IF edit2.Text = ''
     THEN
     begin
      K := strtofloat(edit1.Text);
      p := strtofloat(edit3.Text);
     end
       ELSE
         IF edit3.Text = ''
         THEN
         begin
          K := strtofloat(edit1.Text);
          Z := strtofloat(edit2.text);
         end;

Rechne(K, Z, p)  // aber dann ist dich hier ein Fehler?

 end;


Oder?
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Do 04.02.10 13:45 
ausblenden Delphi-Quelltext
1:
procedure TForm1.Rechne(K, Z, p : real);					

Drei Paramater stehen Deiner Funktion zur Verfügung.
Zwar übergibst Du Deiner Funktion entsprechende Werte bei Aufruf:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
K := strtofloat(edit1.text);
Z := strtofloat(edit2.text);
p := strtofloat(edit3.text);

Rechne(K, Z, p)

überschreibst diese jedoch wieder zu Beginn der Funktion, was deren Nutzen vollkommend relativiert:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
IF edit1.Text = ''
 THEN
 begin
  Z := strtofloat(edit2.text);
  p := strtofloat(edit3.Text);

Des Weiteren, überlege Dir, wie das Programm entscheiden soll, welcher Wert zu berechnen ist, falls alle Edits einen Wert beinhalten. Sofern überhaupt sinnvolle Werte übergeben werden. (Stichwort TryStrToFloat oder StrToFloatDef) ;)
Was kann passieren, wenn k oder p = 0 ist? ;)
Danf Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 27



BeitragVerfasst: Do 04.02.10 13:52 
Jawoll, es hat geklappt...

Dank die nochmal, elundril, für deinen entscheidenen Typ und Ty an die anderen natürlich auch :D