| Autor |
Beitrag |
chriss1988
      
Beiträge: 389
windows xp prof,home,windows98
delphi5
|
Verfasst: 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
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; 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); public 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); zahl:=round(zahl*multi); result:=zahl/multi;
end;
var multi: real; zahl1, zahl2, ergebnis: real ; operator: char; tmpstring:string; error:string; begin tmpstring:=trim(Edit3.Text); operator:=tmpstring[1]; error:='';
try zahl1:=strTofloat(Edit1.Text); zahl2:=StrTofloat(Edit2.Text); except error:='Es wurde keine Zahl eingegeben'; exit; end;
if operator = '+' then ergebnis := Zahl1 + Zahl2; if operator = '-' then ergebnis := Zahl1 - Zahl2; if operator = '*' then ergebnis := Zahl1 * Zahl2;
if operator = '/' then begin if zahl2 <> 0 then ergebnis := Zahl1 / Zahl2 else BEGIN error:='ERROR'; Ergebnis:=0; END; end; edit4.text:=FloatToStr(RundeAufStelle(ergebnis, 2)); label3.caption:=error; end;
procedure TForm1.Button2Click(Sender: TObject); begin close ; end;
end. |
Moderiert von Klabautermann: Delphi-Tags hinzugefügt.Moderiert von Klabautermann: Topic aus Sonstiges verschoben am Mi 28.09.2005 um 13:00
|
|
I.MacLeod
      
Beiträge: 109
|
Verfasst: Mi 28.09.05 12:15
Einfacher gehts mit FloatToStrF:
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
      
Beiträge: 2195
XP
D3Prof, D6Pers.
|
Verfasst: Mi 28.09.05 12:43
I.MacLeod hat Recht so gehts einfacher und Du ersparst Dir die function und eine Menge Tiparbeit.
Delphi-Quelltext 1:
| Edit4.text := FloatToStrF(ergebnis,fffixed,8,2); |
(  Stellen nach dem Komma mit denen Gerechnet wird.
(2) Stellen nach dem Komma die Ausgegeben Werden.
(  Das sollte eine 8 in Klammern werden er macht aber daraus einen Smily komisch.
|
|
chriss1988 
      
Beiträge: 389
windows xp prof,home,windows98
delphi5
|
Verfasst: Mi 28.09.05 12:57
|
|
Blackheart666
      
Beiträge: 2195
XP
D3Prof, D6Pers.
|
Verfasst: 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.
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; 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); public end;
var Form1: TForm1;
implementation
{$R *.DFM}
function RundeAufStelle(zahl: real; stellen: integer): float; var multi: real; begin multi:=IntPower(10, stellen); zahl:=round(zahl*multi); result:=zahl/multi; end;
procedure TForm1.Button1Click(Sender: TObject); var multi: real; zahl1, zahl2, ergebnis: real ; operator: char; tmpstring:string; error:string; begin tmpstring:=trim(Edit3.Text); operator:=tmpstring[1]; error:='';
try zahl1:=strTofloat(Edit1.Text); zahl2:=StrTofloat(Edit2.Text); except error:='Es wurde keine Zahl eingegeben'; exit; end;
if operator = '+' then ergebnis := Zahl1 + Zahl2; if operator = '-' then ergebnis := Zahl1 - Zahl2; if operator = '*' then ergebnis := Zahl1 * Zahl2;
if operator = '/' then begin if zahl2 <> 0 then ergebnis := Zahl1 / Zahl2 else BEGIN error:='ERROR'; Ergebnis:=0; END; end; edit4.text:=FloatToStr(RundeAufStelle(ergebnis, 2)); label3.caption:=error; end;
procedure TForm1.Button2Click(Sender: TObject); begin close ; end;
end. |
|
|
chriss1988 
      
Beiträge: 389
windows xp prof,home,windows98
delphi5
|
Verfasst: 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
      
Beiträge: 2195
XP
D3Prof, D6Pers.
|
Verfasst: Mi 28.09.05 13:10
Mach mal aus dem Float bei der function ein Real dann gehts.
|
|
chriss1988 
      
Beiträge: 389
windows xp prof,home,windows98
delphi5
|
Verfasst: Mi 28.09.05 13:11
|
|
|