Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit4;
- interface
- uses
- System.SysUtils, System.Math;
- type
- TArrayOfDouble = array of array of double;
- type
- TEqSecGrado = class
- private
- //variables
- a, b, c: double;
- delta: double;
- solutions: TArrayOfDouble;
- 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);
- destructor Destroy; override;
- //methods
- function getDelta: double; overload;
- function getSolutions: TArrayOfDouble; virtual;
- //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;
- type
- TSupport = record helper for Double
- function toFraction: string;
- function toString: string;
- end;
- implementation
- //Support class with methods
- function TSupport.toString;
- begin
- Result := FloatToStr(Self);
- end;
- function TSupport.toFraction: 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(Self);
- //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(Self) - h1 / k1) <= abs(Self) * 0.000001);
- //Check if returning a - in front of the fraction if 'x' was < 0
- if (Self < 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;
- constructor TEqSecGrado.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;
- destructor TEqSecGrado.Destroy;
- begin
- //Reset everything
- SetLength(solutions, 0);
- delta := 0;
- solRealCount := 0;
- solImaginaryCount := 0;
- Dec(currentIstances);
- //Destroy inheriting from TObject the method
- inherited;
- end;
- function TEqSecGrado.getDelta: double;
- begin
- Result := delta;
- end;
- function TEqSecGrado.getDelta(const vala, valb, valc: double): double;
- begin
- Result := (valb*valb) - 4*vala*valc;
- end;
- function TEqSecGrado.getSolutions;
- var tempDelta: double;
- begin
- delta := getDelta(a,b,c);
- if (a <> 0) then
- begin
- if (delta >= 0) then
- begin
- SetLength(solutions, 2, 2);
- solutions[0][0] := ((-b + sqrt(delta))/(2*a));
- solutions[0][1] := 0;
- solutions[1][0] := ((-b - sqrt(delta))/(2*a));
- solutions[1][1] := 0;
- //set solutions count
- if (solutions[0] <> solutions[1]) then
- begin
- solRealCount := 2;
- end
- else
- begin
- solRealCount := 1;
- end;
- solImaginaryCount := 0;
- Result := solutions;
- end
- else
- begin
- SetLength(solutions, 2, 2);
- tempDelta := abs(delta);
- solutions[0][0] := (-b/(2*a));
- solutions[0][1] := (sqrt(tempDelta)/(2*a));
- solutions[1][0] := (-b/(2*a));
- solutions[1][1] := -(sqrt(tempDelta)/(2*a));
- //set solutions count
- if (solutions[0] <> solutions[1]) then
- begin
- solImaginaryCount := 2;
- end
- else
- begin
- solImaginaryCount := 1;
- end;
- solRealCount := 0;
- Result := solutions;
- end;
- end
- else
- begin
- //Raise exception when this is not a second degree equation
- raise Exception.Create('The first parameter "a" cannot be zero.');
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement