If you want to see a preview in the play store go here.

This code is helpful to solve quadratic equations with delphi!

unit TEqSecDeg;

interface

uses

System.SysUtils, System.Math, System.Generics.Collections;

type

TInequalitySolKind = (extsol, intsol, always, never, almostalways);

type

TQuadraticErr = class(Exception)

constructor Create(const errMsg: string);

end;

//main class that solves second degree equations

type

TEqSecDegree = class

private

//variables

a, b, c: double;

delta: double;

solRealCount: integer;

solImaginaryCount: integer;

class var currentIstances: integer;

class var totalIstances: integer;

//methods

function getDelta(const vala, valb, valc: double): double; overload;

public

constructor Create(const a, b, c: double); overload;

constructor Create(const a, b, c: string); overload;

destructor Destroy; override;

//methods

function getDecimal(const s: string): double; //fraction to decimal

function getDelta: double; overload;

function getSolutions(aList: TList

class function toFraction(value: double): string;

//properties

property valueOfA: double read a;

property valueOfB: double read b;

property valueOfC: double read c;

property realSolutionsCount: integer read solRealCount;

property imaginarySolutionsCount: integer read solImaginaryCount;

class property currentEquationsCount: integer read currentIstances;

class property totalEquationsCount: integer read totalIstances;

end;

implementation

class function TEqSecDegree.toFraction(value: double): string;

var h1, h2, k1, k2, y, a, aux: double;

sign: string;

begin

//Setup the values

h1 := 1;

h2 := 0;

k1 := 0;

k2 := 1;

y := abs(value);

//Generates the fraction

repeat

begin

a := floor(y);

aux := h1;

h1 := a * h1 + h2;

h2 := aux;

aux := k1;

k1 := a * k1 + k2;

k2 := aux;

if (y – a = 0) or (k1 = 0) then break;

y := 1 / (y – a) ;

end;

until (Abs(abs(value) – h1 / k1) <= abs(value) * 0.000001);
//Check if returning a - in front of the fraction if 'x' was < 0
if (value < 0) then
begin
sign := '-';
end
else
begin
sign := '';
end;
//Output
if not(h1 = 0) then
begin
Result := sign + FloatToStr(h1) + '/' + FloatToStr(k1);
end
else
begin
Result := sign + '0';
end;
end;
{ TEqSecGrado }
constructor TEqSecDegree.Create(const a, b, c: double);
begin
//inherit from TObject
inherited Create;
//Set up the initial parameters
Self.a := a;
Self.b := b;
Self.c := c;
delta := 0;
solRealCount := 0;
solImaginaryCount := 0;
Inc(currentIstances);
Inc(totalIstances);
end;
constructor TEqSecDegree.Create(const a, b, c: string);
begin
//inherit from TObject
inherited Create;
//Set up the initial parameters
Self.a := getDecimal(a);
Self.b := getDecimal(b);
Self.c := getDecimal(c);
delta := 0;
solRealCount := 0;
solImaginaryCount := 0;
Inc(currentIstances);
Inc(totalIstances);
end;
destructor TEqSecDegree.Destroy;
begin
//Reset everything
delta := 0;
solRealCount := 0;
solImaginaryCount := 0;
Dec(currentIstances);
//Destroy inheriting from TObject the method
inherited;
end;
function TEqSecDegree.getDecimal(const s: string): double;
var
BarPos: integer;
numStr, denomStr: string;
x: double;
begin
BarPos := Pos('/', S);
if (TryStrToFloat(s, x)) then
begin
Result := x;
exit();
end;
if BarPos = 0 then
raise TQuadraticErr.Create('Fraction separator symbol "/" not found.');
numStr := Trim(Copy(S, 1, BarPos - 1));
denomStr := Trim(Copy(S, BarPos + 1, Length(S)));
Result := StrToFloat(numStr)/StrToFloat(denomStr);
end;
function TEqSecDegree.getDelta: double;
begin
Result := delta;
end;
function TEqSecDegree.getDelta(const vala, valb, valc: double): double;
begin
Result := (valb*valb) - 4*vala*valc;
end;
function TEqSecDegree.getSolutions(aList: TList

var tempDelta: double;

begin

try

aList.Clear;

delta := getDelta(a,b,c);

if (a <> 0) then

begin

if (delta >= 0) then

begin

aList.Add((-b + sqrt(delta))/(2*a));

aList.Add((-b – sqrt(delta))/(2*a));

//set solutions count

if (aList[0] <> aList[1]) then

begin

solRealCount := 2;

end

else

begin

solRealCount := 1;

end;

solImaginaryCount := 0;

Result := true;

end

else

begin

tempDelta := abs(delta);

aList.Add(-b/(2*a));

aList.Add(sqrt(tempDelta)/(2*a));

aList.Add(-b/(2*a));

aList.Add(-(sqrt(tempDelta)/(2*a)));

//set solutions count

if (aList[1] <> aList[3]) then

begin

solImaginaryCount := 2;

end

else

begin

solImaginaryCount := 1;

end;

solRealCount := 0;

Result := true;

end;

end

else

begin

//Raise exception when this is not a second degree equation

raise TQuadraticErr.Create(‘The first parameter “a” cannot be zero.’);

end;

except

Result := false;

end;

end;

{ TQuadraticErr }

constructor TQuadraticErr.Create(const errMsg: string);

begin

inherited Create(errMsg);

end;

end.