Thema: Delphi Polynom von 2tem Grad

Einzelnen Beitrag anzeigen

haui95

Registriert seit: 1. Feb 2012
Ort: Niedersachsen
29 Beiträge
 
Delphi 7 Personal
 
#5

AW: Polynom von 2tem Grad

  Alt 17. Jun 2012, 17:37
Hier einmal ein Beispielcode, wie man es machen kann, es geht wahrscheinlich auch noch etwas eleganter, z.B. mit der P-Q-Formel, ähnelt aber der a-b-c-Formel, naja
Ich hoffe es hilft dir weiter ! Wie NamenLozer schon gesagt hat, hättest du deinen Quelltext ein bisschen besser formatieren können, da ich zum Beispiel kaum etwas unter deinen Variablennamen verstehen konnte, nur so als kleiner Ratschlag

Delphi-Quellcode:
program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  a, b, c, disk, x1, x2: Real;
begin
  disk := 0;
  readln(a, b, c);

  if (a = 0) then
    writeln('´Not solvable !')
  else
    begin
      disk := (b*b) - 4*a*c;
      if disk < 0 then
        writeln('There are 0 solutions for f(x) = 0 !')
      else
        begin
          disk := sqrt(disk);
          x1 := (-b + disk) / (2 * a);
          x2 := (-b - disk) / (2 * a);
          if (disk = 0) then
            begin
              writeln('There is 1 solution for f(x)=0 !');
              if (x1 >= 0) or (x2 >= 0) then
                writeln('The solution is positive !')
              else
                if (x1 < 0) or (x2 < 0) then
                  writeln('The solution is negative !');
            end
          else
            if (disk > 0) then
              begin
                writeln('There are 2 solutions for f(x)=0 !');
                if (x1 < 0) and (x2 >= 0) then
                  begin
                    writeln('The first solution is negative !');
                    writeln('The second solution is positive !');
                  end
                else
                  if (x1 >= 0) and (x2 < 0) then
                    begin
                      writeln('The first solution is positive !');
                      writeln('The second solution is negative !');
                    end
                else
                  if (x1 >= 0) and (x2 >= 0) then
                    writeln('Both solutions are positive !')
                else
                  if (x1 < 0) and (x2 < 0) then
                    writeln('Both solutions are negative !');
            end;
        end;
    end;
    readln;
end.
MfG

Hauke
Hauke

Geändert von haui95 (17. Jun 2012 um 17:47 Uhr)
  Mit Zitat antworten Zitat