Delphi-PRAXiS
Seite 1 von 4  1 23     Letzte »    

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Falls Variable(n) vorhanden mache das sonst mache dies (https://www.delphipraxis.net/112765-falls-variable-n-vorhanden-mache-das-sonst-mache-dies.html)

thknub 26. Apr 2008 20:15


Falls Variable(n) vorhanden mache das sonst mache dies
 
Hallo,

Ich möchte Programm zur Berechnung von Dreiecken coden. Dabei bin ich auf ein Problem gestoßen:

Ich möchte 6 Variablen, die man eingeben kann, aber nicht eingeben muss. Wenn ich z.B. nur 3 Variablen eingebe, soll das Programm trotzdem alles, was man damit errechnen kann, ausgeben.

Genau genommen würde das bedeuten:

Eingabe sind Variablen a,b,c,d,e,f

Delphi-Quellcode:
m := sin(d)*b/sin(e)
oder wenn b oder e nicht vorhanden ist
Delphi-Quellcode:
m := sin(d)*c/sin(f)
und wenn keine nötige Variable vorhanen ist mache nichts

Ist das irgendwie zu bewerkstelligen?

mkinzler 26. Apr 2008 20:18

Re: Falls Variable(n) vorhanden mache das sonst mache dies
 
Überprüfe doch den Inhalt der Variablen

omata 26. Apr 2008 20:23

Re: Falls Variable(n) vorhanden mache das sonst mache dies
 
Vielleicht hilft dir ja das weiter...

Delphi-Quellcode:
  TDreieck = class
  private
  public
    function Berechnung(a,b,c,d,e,f:integer):real; overload;
    function Berechnung(a,b,c,d,e:integer):real; overload;
    function Berechnung(a,b,c,d:integer):real; overload;
    function Berechnung(a,b,c:integer):real; overload;
    function Berechnung(a,b:integer):real; overload;
    function Berechnung():real; overload;
  end;
Gruss
Thorsten

Stefan92 26. Apr 2008 20:25

Re: Falls Variable(n) vorhanden mache das sonst mache dies
 
Falls es ohne die Variable zu einem Laufzeitfehler kommt, könntest du folgendes versuchen:

Delphi-Quellcode:
try
  m := sin(d)*b/sin(e);
except
  m := sin(d)*c/sin(f);
end;
Gruß

Stefan

Edit:
Vergiss meinen Vorschlag, es gibt bei deinem Problem zu viele Fälle.
@hoika: Ich glaube, thknub möchte wissen, wie man die Variablen auf leeren Inhalt prüft.

hoika 26. Apr 2008 20:26

Re: Falls Variable(n) vorhanden mache das sonst mache dies
 
Halloo,

6 Boolean-Variablen,
die anzeigen, ob die Variablen gefüllt worden sind.


Heiko

inherited 26. Apr 2008 20:27

Re: Falls Variable(n) vorhanden mache das sonst mache dies
 
oder du initalisierst sie einfach mit einem Wert der nicht eingegeben werden kann und überprüfst sie nachher darauf.

thknub 26. Apr 2008 21:22

Re: Falls Variable(n) vorhanden mache das sonst mache dies
 
Wenn ich die Variante von omata benutze antwortet er mir [Pascal Fehler] E2065 Ungenügende Forward- oder External-Deklaration: 'TForm5.Berechnung'.

Hier nochmal der Code

Delphi-Quellcode:
unit Unit5;

interface

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

type
  TForm5 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;
    Edit12: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Button1: TButton;
    Label13: TLabel;
    Label14: TLabel;
    Edit13: TEdit;
    Edit14: TEdit;
    Edit15: TEdit;
    Edit16: TEdit;
    Edit17: TEdit;
    Edit18: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Edit19: TEdit;
    Edit20: TEdit;
    Edit21: TEdit;
    Edit22: TEdit;
    Label24: TLabel;
    Label25: TLabel;
    Edit23: TEdit;
    Edit24: TEdit;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public

  end;

var
  Form5: TForm5;

implementation

{$R *.dfm}

procedure TForm5.Button1Click(Sender: TObject);




 var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r: REAL;

begin

a := StrToFloat(edit1.Text);
b := StrToFloat(edit2.Text);
c := StrToFloat(edit3.Text);
d := StrToFloat(edit4.Text);
e := StrToFloat(edit5.Text);
f := StrToFloat(edit6.Text);
g := sqrt(sqr(b)+sqr(c)-2*b*c*Cos(d));
h := sqrt(sqr(a)+sqr(c)-2*a*c*Cos(e));
i := sqrt(sqr(a)+sqr(b)-2*a*b*Cos(f));
j := ArcCos((sqr(b)+sqr(c)-sqr(a))/(2*b*c));
k := ArcCos((sqr(a)+sqr(c)-sqr(b))/(2*a*c));
l := ArcCos((sqr(a)+sqr(b)-sqr(c))/(2*a*b));
m := sin(d)*b/sin(e);
n := sin(e)*a/sin(d);
o := sin(f)*a/sin(d);
p := ArcSin((a*Sin(e))/b);
q := ArcSin((b*Sin(d))/a);
r := ArcSin((c*Sin(d))/a);

edit7.Text := FloatToStr(g);
edit8.text := FloatToStr(h);
edit9.Text := FloatToStr(i);
edit10.Text := FloatToStr(j);
edit11.Text := FloatToStr(k);
edit12.Text := FloatToStr(l);
edit13.Text := FloatToStr(m);
edit14.Text := FloatToStr(n);
edit15.text := FloatToStr(o);
edit16.Text := FloatToStr(p);
edit17.Text := FloatToStr(q);
edit18.Text := FloatToStr(r);


end;

end.
Wobei g und m sowie h und n das gleiche sind.(Hatte probiert es über verschiedene Ausgabefelder zu machen, aber es kommt immer folgende Nachricht, wenn ich auf testen klicke und nicht in alle Eingabe Felder etwas schreibe: " ist kein gültiger Gleitkommawert

Ich hätte auch nicht wirklich ein Problem damit 3 Ausgabefelder zu machen, wenn man wenigstens nicht überall was eingeben muss, wobei mir zwei oder auch nur eins besser gefielen...

@inherited
Ich versteh jetzt nicht so ganz, was du meinst

mkinzler 26. Apr 2008 21:33

Re: Falls Variable(n) vorhanden mache das sonst mache dies
 
Zitat:

Wenn ich die Variante von omata benutze antwortet er mir [Pascal Fehler] E2065 Ungenügende Forward- oder External-Deklaration: 'TForm5.Berechnung'.
Du musst die Methoden natürlich auch implementieren.

thknub 26. Apr 2008 22:13

Re: Falls Variable(n) vorhanden mache das sonst mache dies
 
Das mache ich wie? Kann mir mal jemand ein Bsp. geben? Sorry, ich bin noch ziemlich neu auf dem Gebiet (hab gestern Abend angefangen ein wenig rumzucoden)

hesubat 27. Apr 2008 00:15

Re: Falls Variable(n) vorhanden mache das sonst mache dies
 
Was meinst Du zu folgender Loesung:

Code:
procedure TForm1.Button1Click(Sender: TObject);
var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r: REAL; bo1,bo2,bo3,bo4,bo5,bo6:boolean;
begin

bo1:=false; bo2:=false; bo3:=false; bo4:=false; bo5:=false; bo6:=false;
if edit1.text<>'' then begin a := StrToFloat(edit1.Text); bo1:=true; end;
if edit2.text<>'' then begin b := StrToFloat(edit2.Text); bo2:=true; end;
if edit3.text<>'' then begin c := StrToFloat(edit3.Text); bo3:=true; end;
if edit4.text<>'' then begin d := StrToFloat(edit4.Text); bo4:=true; end;
if edit5.text<>'' then begin e := StrToFloat(edit5.Text); bo5:=true; end;
if edit6.text<>'' then begin f := StrToFloat(edit6.Text); bo6:=true; end;
if bo2 and bo3 and bo4 then g := sqrt(sqr(b)+sqr(c)-2*b*c*Cos(d));
if bo1 and bo3 and bo5 then h := sqrt(sqr(a)+sqr(c)-2*a*c*Cos(e));
if bo1 and bo2 and bo6 then i := sqrt(sqr(a)+sqr(b)-2*a*b*Cos(f));
if bo1 and bo2 and bo3 then j := ArcCos((sqr(b)+sqr(c)-sqr(a))/(2*b*c));
if bo1 and bo2 and bo3 then k := ArcCos((sqr(a)+sqr(c)-sqr(b))/(2*a*c));
if bo1 and bo2 and bo3 then l := ArcCos((sqr(a)+sqr(b)-sqr(c))/(2*a*b));
if bo2 and bo4 and bo5 then m := sin(d)*b/sin(e);
if bo1 and bo4 and bo5 then n := sin(e)*a/sin(d);
if bo1 and bo4 and bo6 then o := sin(f)*a/sin(d);
if bo1 and bo2 and bo5 then p := ArcSin((a*Sin(e))/b);
if bo1 and bo2 and bo4 then q := ArcSin((b*Sin(d))/a);
if bo1 and bo3 and bo4 then r := ArcSin((c*Sin(d))/a);

edit7.Text := FloatToStr(g);
edit8.text := FloatToStr(h);
edit9.Text := FloatToStr(i);
edit10.Text := FloatToStr(j);
edit11.Text := FloatToStr(k);
edit12.Text := FloatToStr(l);
edit13.Text := FloatToStr(m);
edit14.Text := FloatToStr(n);
edit15.text := FloatToStr(o);
edit16.Text := FloatToStr(p);
edit17.Text := FloatToStr(q);
edit18.Text := FloatToStr(r);

end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:48 Uhr.
Seite 1 von 4  1 23     Letzte »    

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz