Einzelnen Beitrag anzeigen

Benutzerbild von Wolfgang Mix
Wolfgang Mix

Registriert seit: 13. Mai 2009
Ort: Lübeck
1.222 Beiträge
 
Delphi 2005 Personal
 
#18

Re: Quadratische Gleichungen vollständig lösen

  Alt 26. Jul 2009, 16:53
Ich hoffe, die meisten von Euch können jetzt mit dieser Variante leben:

Delphi-Quellcode:
type
  TSolution = Array of String;

function pq( A, B, C : Double ): TSolution;
var
  Radikand, re, im: Double;
begin
// ax² + bx + c = 0
  if A = 0 then exit;
    B := B / A; //p
    C := C / A; //q
  // Radikand berechnen
  Radikand := sqr(B/2) - C;
  //Realteil berechnen
  re:=-B/2;
  //Imaginärteil berechnen
  im:=sqrt(abs(Radikand));

  if Radikand > 0 then
  begin // zwei Lösungen
    SetLength( Result, 2 );
    Result[0] := FloatToStr(-B/2 + sqrt( Radikand ));
    Result[1] := FloatToStr(-B/2 - sqrt( Radikand ));
  end
  else
  if abs(Radikand) < 1e-6 then
  begin // eine Lösung
    SetLength( Result, 1 );
    Result[0] := FloatToStr(-B/2);
  end
  else
  if Radikand < 0 then
  begin
     ///Radikand:=-Radikand;
     SetLength( Result, 2 );
     Result[0] := FloatToStr(re) + ' + ' + FloatToStr(im) + ' i ';
     Result[1] := FloatToStr(re) + ' - ' + FloatToStr(im) + ' i ';
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var a,b,c: double;
begin
   a:=StrToFloat(Edit1.Text);
   b:=StrToFloat(Edit2.Text);
   c:=StrToFloat(Edit3.Text);
   if (a=0) or (b=0) or (c=0) then
   begin
     //Werte der Funktion gar nicht erst übergeben
     showmessage ('Keine reine quadratische Gleichung!');
     sleep(2000);
     exit;
   end
   else
   begin
     Edit4.Text:=pq(a,b,c)[0];
     Edit5.Text:=pq(a,b,c)[1];
   end;
end;
Wolfgang Mix
if you can't explain it simply you don't understand it well enough - A. Einstein
Mein Baby:http://www.epubli.de/shop/buch/Grund...41818516/52824
  Mit Zitat antworten Zitat