I have created a component that allows you to easily manage fractions in Delphi! here you can see its implementation:
type TFraction = class(TComponent) strict private aNumerator: integer; aDenominator: integer; FOnReduced: TFracEvent; function GCD(a, b: integer): integer; public constructor Create(AOwner: TComponent; aNumerator: integer; aDenominator: integer); reintroduce; function toString: string; reintroduce; function toDouble: double; procedure Reduce; class function Add(fraction1, fraction2: TFraction): TFraction; class function Subtract(fraction1, fraction2: TFraction): TFraction; class function Multiply(fraction1, fraction2: TFraction): TFraction; class function Divide(fraction1, fraction2: TFraction): TFraction; class function Negative(const Value: TFraction): TFraction; class function GetFraction(value: double): TFraction; published property Numerator: integer read aNumerator write aNumerator; property Denominator: integer read aDenominator write aDenominator; property OnReduce: TFracEvent read FOnReduced write FOnReduced; end;
Basically that is a component that has the ability to create an in-memory representation of a fraction. Then you can make conversions (double to fraction or vice-versa) or operations among them. Here you can see a video in which I show you how to use it, it’s very easy!
Here there is the component registration part. Ask in a video comment for the complete source code! I have created two separated files (design time and runtime) so that you can add whatever you want to my component. Have fun!
unit uFractionRegister; interface uses Classes, uFraction; procedure Register; implementation procedure Register; begin RegisterComponents('AlbertoComp.', [TFraction]); end; end.