Advertisement
Guest User

TEqSecGrado

a guest
Sep 11th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.16 KB | None | 0 0
  1. unit Unit4;
  2.  
  3. interface
  4.  
  5. uses
  6.  System.SysUtils, System.Math;
  7.  
  8. type
  9.  TArrayOfDouble = array of array of double;
  10.  
  11. type
  12.  TEqSecGrado = class
  13.   private
  14.    //variables
  15.    a, b, c: double;
  16.    delta: double;
  17.    solutions: TArrayOfDouble;
  18.    solRealCount: integer;
  19.    solImaginaryCount: integer;
  20.    class var currentIstances: integer;
  21.    class var totalIstances: integer;
  22.    //methods
  23.    function getDelta(const vala, valb, valc: double): double; overload;
  24.   public
  25.    constructor Create(const a, b, c: double);
  26.    destructor Destroy; override;
  27.    //methods
  28.    function getDelta: double; overload;
  29.    function getSolutions: TArrayOfDouble; virtual;
  30.    //properties
  31.    property valueOfA: double read a;
  32.    property valueOfB: double read b;
  33.    property valueOfC: double read c;
  34.    property realSolutionsCount: integer read solRealCount;
  35.    property imaginarySolutionsCount: integer read solImaginaryCount;
  36.    class property currentEquationsCount: integer read currentIstances;
  37.    class property totalEquationsCount: integer read totalIstances;
  38.  end;
  39.  
  40. type
  41.  TSupport = record helper for Double
  42.    function toFraction: string;
  43.    function toString: string;
  44.  end;
  45.  
  46. implementation
  47.  
  48. //Support class with methods
  49. function TSupport.toString;
  50. begin
  51.  Result := FloatToStr(Self);
  52. end;
  53.  
  54. function TSupport.toFraction: string;
  55. var h1, h2, k1, k2, y, a, aux: double;
  56.     sign: string;
  57. begin
  58.  
  59.   //Setup the values
  60.   h1 := 1;
  61.   h2 := 0;
  62.   k1 := 0;
  63.   k2 := 1;
  64.   y := abs(Self);
  65.  
  66.   //Generates the fraction
  67.   repeat
  68.     begin
  69.       a := floor(y);
  70.       aux := h1;
  71.       h1 := a * h1 + h2;
  72.       h2 := aux;
  73.       aux := k1;
  74.       k1 := a * k1 + k2;
  75.       k2 := aux;
  76.       if (y - a = 0) or (k1 = 0) then break;
  77.       y := 1 / (y - a) ;
  78.     end;
  79.   until (Abs(abs(Self) - h1 / k1) <= abs(Self) * 0.000001);
  80.  
  81.   //Check if returning a - in front of the fraction if 'x' was < 0
  82.   if (Self < 0) then
  83.    begin
  84.     sign := '-';
  85.    end
  86.   else
  87.    begin
  88.     sign := '';
  89.    end;
  90.  
  91.   //Output
  92.   if not(h1 = 0) then
  93.    begin
  94.     Result := sign + FloatToStr(h1) + '/' + FloatToStr(k1);
  95.    end
  96.   else
  97.    begin
  98.     Result := sign + '0';
  99.    end;
  100.  
  101. end;
  102.  
  103. constructor TEqSecGrado.Create(const a, b, c: double);
  104. begin
  105.  //inherit from TObject
  106.  inherited Create;
  107.  
  108.  //Set up the initial parameters
  109.  Self.a := a;
  110.  Self.b := b;
  111.  Self.c := c;
  112.  delta := 0;
  113.  solRealCount := 0;
  114.  solImaginaryCount := 0;
  115.  Inc(currentIstances);
  116.  Inc(totalIstances);
  117. end;
  118.  
  119. destructor TEqSecGrado.Destroy;
  120. begin
  121.   //Reset everything
  122.   SetLength(solutions, 0);
  123.   delta := 0;
  124.   solRealCount := 0;
  125.   solImaginaryCount := 0;
  126.   Dec(currentIstances);
  127.  
  128.   //Destroy inheriting from TObject the method
  129.   inherited;
  130. end;
  131.  
  132. function TEqSecGrado.getDelta: double;
  133. begin
  134.  Result := delta;
  135. end;
  136.  
  137. function TEqSecGrado.getDelta(const vala, valb, valc: double): double;
  138. begin
  139.  Result := (valb*valb) - 4*vala*valc;
  140. end;
  141.  
  142. function TEqSecGrado.getSolutions;
  143. var tempDelta: double;
  144. begin
  145.  
  146.  delta := getDelta(a,b,c);
  147.  
  148.  if (a <> 0) then
  149.   begin
  150.  
  151.   if (delta >= 0) then
  152.    begin
  153.  
  154.     SetLength(solutions, 2, 2);
  155.     solutions[0][0] := ((-b + sqrt(delta))/(2*a));
  156.     solutions[0][1] := 0;
  157.     solutions[1][0] := ((-b - sqrt(delta))/(2*a));
  158.     solutions[1][1] := 0;
  159.  
  160.     //set solutions count
  161.     if (solutions[0] <> solutions[1]) then
  162.      begin
  163.       solRealCount := 2;
  164.      end
  165.     else
  166.      begin
  167.       solRealCount := 1;
  168.      end;
  169.  
  170.     solImaginaryCount := 0;
  171.     Result := solutions;
  172.  
  173.    end
  174.   else
  175.    begin
  176.  
  177.     SetLength(solutions, 2, 2);
  178.     tempDelta := abs(delta);
  179.     solutions[0][0] := (-b/(2*a));
  180.     solutions[0][1] := (sqrt(tempDelta)/(2*a));
  181.     solutions[1][0] := (-b/(2*a));
  182.     solutions[1][1] := -(sqrt(tempDelta)/(2*a));
  183.  
  184.     //set solutions count
  185.     if (solutions[0] <> solutions[1]) then
  186.      begin
  187.       solImaginaryCount := 2;
  188.      end
  189.     else
  190.      begin
  191.       solImaginaryCount := 1;
  192.      end;
  193.  
  194.     solRealCount := 0;
  195.     Result := solutions;
  196.  
  197.    end;
  198.  
  199.   end
  200.  else
  201.   begin
  202.  
  203.    //Raise exception when this is not a second degree equation
  204.    raise Exception.Create('The first parameter "a" cannot be zero.');
  205.  
  206.   end;
  207.  
  208. end;
  209.  
  210. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement