Autor Beitrag
b43ck3r
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Mi 06.10.04 18:49 
moin,
hab versucht ein programm zu erstellen dass mir ab dem wert 100 um edit feld einen rabatt von 2% gibt, ab 500 dann 5% und ab 1000 einen von 10%...
es klappt bei allen werten bloß nicht bei denen von 100-499, dort gibt er mir garkeinen rabatt!

hier mein quelltext:

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:
implementation

{$R *.DFM}

const einzelpreis = 10;
var anzahl, preis : Real;

procedure TForm1.Button1Click(Sender: TObject);
begin

anzahl := StrToFloat(Edit1.Text);

 begin
 if anzahl >= 100
 then preis := (anzahl*einzelpreis)*0.98
 else preis := (anzahl*einzelpreis)
 end;

   begin
   if anzahl >= 500
   then preis := (anzahl*einzelpreis)*0.95
   else preis := (anzahl*einzelpreis)
   end;

     begin
     if anzahl >= 1000
     then preis := (anzahl*einzelpreis)*0.9
     end;

Memo1.Lines.Add('Der Gesamtpreis beträgt: ' + FloatToStr(preis) + '€');
Memo1.Lines.Add('');
Memo1.Lines.Add('(Bei einem Stückpreis von 10€)');
Memo1.Lines.Add('                     ---------------')
end;


wär spitze wenn mir einer sagen könnte wo mein fehler liegt!

grüße
neurowin
Hält's aus hier
Beiträge: 9

win XP
D6 Personal
BeitragVerfasst: Mi 06.10.04 19:02 
Hallo !!

Das Problem liegt in den Zeilen 20-22 ;

Ist der Wert (z.B. : 250 ) nicht >= 500 wird ja Zeile 22 ausgeführt !


Ich hoffe du siehst es !!!

Vielleicht geht es so einfacher !!!

ausblenden Delphi-Quelltext
1:
2:
3:
 if anzahl >= 100 then preis :=(anzahl*einzelpreis)*0.98;
 if anzahl >= 500 then preis :=(anzahl*einzelpreis)*0.95;
 if anzahl >= 1000 then preis :=(anzahl*einzelpreis)*0.9;


Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt.


Zuletzt bearbeitet von neurowin am Mi 06.10.04 19:08, insgesamt 1-mal bearbeitet
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mi 06.10.04 19:05 
Da wird sogar der obere Teil vom Compiler raus-optimiert. Sieht man gut, wenn man mit dem Debugger durchgeht. Da fehlen dann die schönen blauen Punkte am linken Rand :wink:
grayfox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 800

win98, winXP
D4 Standard; D6 Personal
BeitragVerfasst: Mi 06.10.04 19:15 
hallo b43ck3r!

versuchs mal so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
{$UNTESTED}
  if anzahl >= 1000 then 
    preis:= (anzahl*einzelpreis)*0.9  
  else if anzahl >= 500 then 
    preis:= (anzahl*einzelpreis)*0.95  
  else if anzahl >= 100 then 
    preis:= (anzahl*einzelpreis)*0.98;
  else
    preis:= (anzahl*einzelpreis);

  bzw schöner:

  case anzahl of
    >= 1000: preis:= (anzahl*einzelpreis)*0.9;
    >=  500: preis:= (anzahl*einzelpreis)*0.95;
    >=  100: preis:= (anzahl*einzelpreis)*0.98;
    else 
      preis:= (anzahl*einzelpreis)
  end;


mfg, stefan

ps: den variablentypen 'real' vermeide lieber und warum sind 'anzahl' & 'preis' globale variablen?
grayfox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 800

win98, winXP
D4 Standard; D6 Personal
BeitragVerfasst: Mi 06.10.04 19:17 
hallo neurowin!

nachträglich beiträge zu verändern ist fies! :wink:

deine lösung ist aber auch nicht ganz sauber, da bei einer anzahl von zb 1001 stück der rabatt 3 mal berechnet wird *gg*

mfg, stefan
b43ck3r Threadstarter
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Mi 06.10.04 20:00 
hm gut gut, kann mir jemand meinen quelltext so umschreiben dass er funktionert?

ob globale oder locale variablen is bei dem kleinen programm egal...

gruß
John Doe
Hält's aus hier
Beiträge: 10

Win XP Prof.
D5 Prof., D7 Enterprise
BeitragVerfasst: Mi 06.10.04 20:40 
Hi,

steht zwar eigentlich schon alles da, so dass du deinen Quelltext eigentlich selbst abändern könntest, denn nur durch ausprobieren kommst du weiter, aber ich will mal nicht so sein:

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:
implementation

{$R *.DFM}

const einzelpreis=10;

procedure TForm1.Button1Click(Sender: TObject);
var anzahl, preis:Real;
 begin
  anzahl:=StrToFloat(Edit1.Text);

  if anzahl >= 1000 then
    preis:= (anzahl*einzelpreis)*0.9
  else if anzahl >= 500 then
    preis:= (anzahl*einzelpreis)*0.95
  else if anzahl >= 100 then
    preis:= (anzahl*einzelpreis)*0.98
  else
    preis:= (anzahl*einzelpreis);

  Memo1.Lines.Add('Der Gesamtpreis beträgt: ' + FloatToStr(preis) + '€');
  Memo1.Lines.Add('');
  Memo1.Lines.Add('(Bei einem Stückpreis von 10€)');
  Memo1.Lines.Add('                     ---------------')
 end;

end.


Gruß,
John

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.

_________________
You never know enough...
b43ck3r Threadstarter
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Mi 06.10.04 21:12 
achso so hatte ich das dann auch...
quasi von hinten aufgerollt und mit der 1000 beginnend und nicht mit der 100...

danke trotzdem...

trotzdem ist mir noch nicht klar wieso es andersrum nich funktioniert,
steht ja das gleiche da, nur andere reihenfolge...

gruß
grayfox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 800

win98, winXP
D4 Standard; D6 Personal
BeitragVerfasst: Mi 06.10.04 22:47 
bist du nun ein komponentenschubser oder ein programmierer? :twisted:

als ich programmieren gelernt habe, gabs den sogenannten 'schreibtischtest'.
dazu brauchte man keinen pc, sondern nur ein blatt papier und einen bleistift.

er funktioniert so: man wählte eine beliebige zahl aus, geht damit gedanklich durch die einzelnen programmzeilen, vollzieht die sprünge, die das programm macht und schreibt mit, wie sich die variablen ändern. dadurch konnte man auf einfache weise logische fehler aufspüren.

ok, heute gibts debugger und den einzelschrittmodus. setz dir dazu einen haltepunkt in der ersten zeile des IF-konstruktes und lass das programm bis dorthin laufen.
danach wanderst du mit F7 durch die zeilen und siehst, welche sprünge das programm ausführt.
wenn du den mauszeiger auf eine variable nach der soeben bearbeiteten zeile stellst, wird dir sogar ihr wert angezeigt. na, gibts was schöneres als 'learning by doing'?

dann viel spass damit!
mfg, stefan