Autor Beitrag
chriss1988
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Mi 28.09.05 11:56 
wollt mal wissen ob das so gut ist wie ich das gemacht hab den bei mir gehts net
das programm ist ein kleiner rechner der +-*/ kann kommazahlen hat und wo ich dran gescheitert bin beim runden.


könnt ihr mal schaun wo der fehler liegt


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:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
unit Unit1;

 interface

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

 type
   TForm1 = class(TForm)
     Button1: TButton;      { steht für die felder, die auf dem Formular sthen}
     Edit1: TEdit;
     Edit2: TEdit;
     Label1: TLabel;
     Label2: TLabel;
     Label4: TLabel;
     Edit3: TEdit;
     Edit4: TEdit;
     Button2: TButton;
     Label3: TLabel;
 procedure Button1Click(Sender: TObject);
 procedure Button2Click(Sender: TObject);
      { Private-Deklarationen }
 public
      { Public-Deklarationen }
 end;

 var
  Form1: TForm1;

 implementation

   {$R *.DFM}


procedure TForm1.Button1Click(Sender: TObject);

function RundeAufStelle(zahl: real; stellen: integer): float;
  var
   multi: real;
  begin
   multi:=IntPower(10, stellen);   //runden von Kommazahlen
   zahl:=round(zahl*multi);
   result:=zahl/multi;

  end;

  var
   multi: real;
   zahl1, zahl2, ergebnis: real ;    //real steht für Kommazhlen
   operator: char;                   //(operator) so nennt man +,-,*,/,
   tmpstring:string;
   error:string;
  begin
    tmpstring:=trim(Edit3.Text);
    operator:=tmpstring[1];
    error:='';

  try
    zahl1:=strTofloat(Edit1.Text);      //wandelt zeichen in gleitzahlen um
    zahl2:=StrTofloat(Edit2.Text);
  except
    error:='Es wurde keine Zahl eingegeben';
    exit;
  end;

  if operator = '+' then             //gibt an, wen '...' im edit3 steht,
    ergebnis := Zahl1 + Zahl2;        // passiert das!
  if operator = '-' then
    ergebnis :=  Zahl1 - Zahl2;
  if operator = '*' then
    ergebnis :=  Zahl1 * Zahl2;

  if operator = '/' then             // *wenn '/' im edit3 steht
  begin
  if zahl2 <> 0 then                 //  **wenn im edit2 auch 0 steht
    ergebnis :=  Zahl1 / Zahl2       // *rechnet er das
  else
  BEGIN
    error:='ERROR';              //**Zeigt wenn im edit2 '0' steht das
    Ergebnis:=0;                 //**Ergebniss 0  und gleichzeitig im label 3
                                 //**Fehler an

  END;
  end;
    edit4.text:=FloatToStr(RundeAufStelle(ergebnis, 2));
    {Runden von Kommastellen bei Stelle (rundeaufstelle(ergebnis, 2)),
     und schreibt ergebnis gleich in edit4}

    label3.caption:=error;
  end;

procedure TForm1.Button2Click(Sender: TObject);
  begin
    close ;    //Beendet das Programm
  end;

end.


Moderiert von user profile iconKlabautermann: Delphi-Tags hinzugefügt.
Moderiert von user profile iconKlabautermann: Topic aus Sonstiges verschoben am Mi 28.09.2005 um 13:00
I.MacLeod
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 109



BeitragVerfasst: Mi 28.09.05 12:15 
Einfacher gehts mit FloatToStrF:

ausblenden Delphi-Quelltext
1:
FloatToStrF(ergebnis, ffFixed, 7, AnzahlStellen);					


oder so ähnlich, ich benutz das eher selten. Im Zweifelsfall kannst du ja mal in die OH gucken.

Cheers

_________________
{$APPTYPE CONSOLE}uses SysUtils;const a='{$APPTYPE CONSOLE}uses SysUtils;const a=%s;begin write(Format(a,[#39+a+#39]))end.';begin write(Format(a,[#39+a+#39]))end.
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Mi 28.09.05 12:43 
I.MacLeod hat Recht so gehts einfacher und Du ersparst Dir die function und eine Menge Tiparbeit.

ausblenden Delphi-Quelltext
1:
Edit4.text := FloatToStrF(ergebnis,fffixed,8,2);					


(8) Stellen nach dem Komma mit denen Gerechnet wird.
(2) Stellen nach dem Komma die Ausgegeben Werden.
(8) Das sollte eine 8 in Klammern werden er macht aber daraus einen Smily komisch.
chriss1988 Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Mi 28.09.05 12:57 
danke aber ich hab gedacht der fehler liegt hier irgendwo

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
function RundeAufStelle(zahl: real; stellen: integer): float;  
  var  
   multi: real;  
  begin  
   multi:=IntPower(10, stellen);   //runden von Kommazahlen  
   zahl:=round(zahl*multi);  
   result:=zahl/multi;  

 
  end;


Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt.
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Mi 28.09.05 13:03 
Du hast die Function irgendwie in deine ButtenClick Methode gehauen.
Die Function gehört darüber sonst Funktioniert dein Programm.

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:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
unit Unit1;  

 
 interface  

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

 
 type  
   TForm1 = class(TForm)  
     Button1: TButton;      { steht für die felder, die auf dem Formular sthen}  
     Edit1: TEdit;  
     Edit2: TEdit;  
     Label1: TLabel;  
     Label2: TLabel;  
     Label4: TLabel;  
     Edit3: TEdit;  
     Edit4: TEdit;  
     Button2: TButton;  
     Label3: TLabel;  
 procedure Button1Click(Sender: TObject);  
 procedure Button2Click(Sender: TObject);  
      { Private-Deklarationen }  
 public  
      { Public-Deklarationen }  
 end;  

 
 var  
  Form1: TForm1;  

 
 implementation  

 
   {$R *.DFM}  

function RundeAufStelle(zahl: real; stellen: integer): float;  
  var  
   multi: real;  
  begin  
   multi:=IntPower(10, stellen);   //runden von Kommazahlen  
   zahl:=round(zahl*multi);  
   result:=zahl/multi;  
end;  

 
procedure TForm1.Button1Click(Sender: TObject);  
var  
   multi: real;  
   zahl1, zahl2, ergebnis: real ;    //real steht für Kommazhlen  
   operator: char;                   //(operator) so nennt man +,-,*,/,  
   tmpstring:string;  
   error:string;  
  begin  
    tmpstring:=trim(Edit3.Text);  
    operator:=tmpstring[1];  
    error:='';  

 
  try  
    zahl1:=strTofloat(Edit1.Text);      //wandelt zeichen in gleitzahlen um  
    zahl2:=StrTofloat(Edit2.Text);  
  except  
    error:='Es wurde keine Zahl eingegeben';  
    exit;  
  end;  

 
  if operator = '+' then             //gibt an, wen '...' im edit3 steht,  
    ergebnis := Zahl1 + Zahl2;        // passiert das!  
  if operator = '-' then  
    ergebnis :=  Zahl1 - Zahl2;  
  if operator = '*' then  
    ergebnis :=  Zahl1 * Zahl2;  

 
  if operator = '/' then             // *wenn '/' im edit3 steht  
  begin  
  if zahl2 <> 0 then                 //  **wenn im edit2 auch 0 steht  
    ergebnis :=  Zahl1 / Zahl2       // *rechnet er das  
  else  
  BEGIN  
    error:='ERROR';              //**Zeigt wenn im edit2 '0' steht das  
    Ergebnis:=0;                 //**Ergebniss 0  und gleichzeitig im label 3  
                                 //**Fehler an  
 
  END;  
  end;  
    edit4.text:=FloatToStr(RundeAufStelle(ergebnis, 2));  
    {Runden von Kommastellen bei Stelle (rundeaufstelle(ergebnis, 2)),  
     und schreibt ergebnis gleich in edit4}
  
    label3.caption:=error;  
  end;  

 
procedure TForm1.Button2Click(Sender: TObject);  
  begin  
    close ;    //Beendet das Programm  
  end;  

 
end.
chriss1988 Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Mi 28.09.05 13:07 
ok danke jetzt mekert er deshalb hier würde ein fehöer vorliegen

function RundeAufStelle(zahl: real; stellen: integer): float;
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Mi 28.09.05 13:10 
Mach mal aus dem Float bei der function ein Real dann gehts.
chriss1988 Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Mi 28.09.05 13:11 
danke es geht