Autor Beitrag
SiH
Hält's aus hier
Beiträge: 8



BeitragVerfasst: So 07.10.07 19:47 
Hallöchen und guten Abend alle miteinander!

Der Quelltext stellt meine Info-HA dar. Sie ist korrekt gelöst. Als krönendes Sahnehäubchen wollte ich noch die Real-zahlen auf 2 Stellen nach dem Komma runden, was aber nicht funktioniert! Es wird immer nur '0.00' ausgegeben - und das ist falsch. Ohne fffixed zeigt mir Delphi zwar die korrekten Werte an, aber die sind zu lang! Hoffe mir kann noch jemand helfen!

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:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids;

type
  TForm1 = class(TForm)
    ed_e_x: TEdit;
    Label1: TLabel;
    Bt1_berechnen: TButton;
    Label2: TLabel;
    sg1_wertet: TStringGrid;
    procedure Bt1_berechnenClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen}
  public
    { Public-Deklarationen}
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function berechnen (x:real):real;
var y: real;
begin
y:= sqr(x) + 3.4*x - 2.5*abs(x) - 0.4;
end;

{function RundeAufStelle(zahl: double; stellen: integer): double;
var multi: double;
begin
  multi:=IntPower(10, stellen);
  zahl:=round(zahl*multi);
  result:=zahl/multi;
end;    }


procedure TForm1.Bt1_berechnenClick(Sender: TObject);
var x,y:real;
begin
x:=strtofloat(ed_e_x.text);
y:=berechnen(x);
showmessage(floattostrf(y,fffixed,2,2));

end;

procedure TForm1.FormCreate(Sender: TObject);
var n:real;
    i,j:integer;
begin

sg1_wertet.cells[0,1]:='x';
sg1_wertet.cells[0,2]:='y';

n:=-3;
for i:=1 to 14 do
begin
sg1_wertet.cells[i,1]:=floattostr(n);
n:= n + 0.5;
end;

for j:=1 to 14 do
sg1_wertet.cells[j,2]:=floattostrf(berechnen(n),fffixed,2,2);
n:= n + 0.5;

end;

end.


Ich häng mal noch nen Screen der Ausgabe mit dran:

[url=img258.imageshack.us...p?image=neu3dj4.jpg]user defined image[/URL]

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt
Jann1k
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 866
Erhaltene Danke: 43

Win 7
TurboDelphi, Visual Studio 2010
BeitragVerfasst: So 07.10.07 19:56 
Ich schätze mal der Fehler liegt daran, dass deine function berechnen keinen Rückgabewert liefert.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function berechnen (x:real):real;  
var y: real;  
begin  
y:= sqr(x) + 3.4*x - 2.5*abs(x) - 0.4;  
---->result:=Y;<-----
end;
SiH Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: So 07.10.07 20:01 
Mh! Dann sagt er mir, alles wäre 19! Kann auch nicht stimmen!:(
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: So 07.10.07 20:13 
Hallo,

formatier Deinen Code mal vernünftig, dann siehst Du es vermutlich.

begin end ...

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Jann1k
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 866
Erhaltene Danke: 43

Win 7
TurboDelphi, Visual Studio 2010
BeitragVerfasst: So 07.10.07 20:21 
Hmm, hab n bisschen rumprobiert und wenn du aus

ausblenden Delphi-Quelltext
1:
showmessage(floattostrf(y,fffixed,2,2));					


ausblenden Delphi-Quelltext
1:
showmessage(floattostrf(y,fffixed,15,2));					


machst kommt da 19.2 raus
SiH Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: So 07.10.07 20:31 
Ja das stimmt, aber das Ergebnis (also diese 19,2) ist falsch. Das kommt irgendwie immer, egal welchen wert ich für X nehme.
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: So 07.10.07 20:45 
Hallo,

Dein Code:
ausblenden Delphi-Quelltext
1:
2:
3:
for j:=1 to 14 do  
sg1_wertet.cells[j,2]:=floattostrf(berechnen(n),fffixed,2,2);  
n:= n + 0.5;


formatiert:
ausblenden Delphi-Quelltext
1:
2:
3:
for j:=1 to 14 do  
  sg1_wertet.cells[j,2]:=floattostrf(berechnen(n),fffixed,2,2);  
n:= n + 0.5;



sehe nicht das in der Schleife n geändert wird. :wink:

begin end ...

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )