其实,这些功能我都实现了,关键我是想看看大家没有有效率更高的算法:)
//得到两个数的最大公约数;
function MaxCommonDivisor(iSmall : integer ; iBig : integer) : integer ;
//两个分数相加 ,结果为最简分数
function AddFraction(First : TFraction ; Second : TFraction) : TFraction;
//两个分数相乘 ,结果为最简分数
function MulFraction(First : TFraction ; Second : TFraction) : TFraction;
//两个分数相减 ,结果为最简分数
function SubFraction(First : TFraction ; Second : TFraction) : TFraction;
//两个分数相除 ,结果为最简分数
function DivFraction(First : TFraction ; Second : TFraction) : TFraction;
//把某个浮点型数转换成最简分数; 比如 x:=1.8 ,result:=9/5
function DecimalToFraction(x : Double) : TFraction;
//把某个字符串型分数转换成最简分数; 比如 s:='8/6' ,result:=4/3
function StringToFraction(s : string) : TFraction;
==========
function TAccessNum.AddFraction(First, Second: TFraction): TFraction;
var
tempI : int64;
numerator : int64;
denominator : int64;
begin
numerator:=First.numerator * Second.denominator + First.denominator * Second.numerator;
denominator := First.denominator * Second.denominator;
tempI := MaxCommonDivisor(numerator,denominator);
Result.numerator:=numerator div tempI;
Result.denominator:=denominator div tempI;
end;
function TAccessNum.DecimalToFraction(x: Double): TFraction;
var
str : String;
numerator : int64;
denominator : int64;
n : Integer;
tempI : int64;
begin
str:=floattostr(x);
n:=length(str)- pos('.',str);
numerator:=round(x*power(10,n));
denominator:=round(power(10,n));
tempI := MaxCommonDivisor(numerator,denominator);
Result.numerator:=numerator div tempI;
Result.denominator:=denominator div tempI;
end;
function TAccessNum.StringToFraction(s: string): TFraction;
var
numerator : int64;
denominator : int64;
iLenNumerator:Integer;
iLenDenominator: Integer;
tempI : int64;
begin
iLenDenominator:=pos('/',s)-1;
iLenNumerator:=Length(s)-1-iLenDenominator;
if iLenDenominator=-1 then
begin
Result.numerator:=StrToIntDef(s,0);
result.denominator:=1;
exit;
end;
numerator:=StrToInt64Def(copy(s,1,iLenNumerator),0);
Delete(s,1,iLenNumerator+1);
denominator:=StrToInt64Def(s,1);
tempI := MaxCommonDivisor(numerator,denominator);
Result.numerator:=numerator div tempI;
Result.denominator:=denominator div tempI;
end;
function TAccessNum.DivFraction(First, Second: TFraction): TFraction;
var
tempI : int64;
numerator : int64;
denominator : int64;
begin
numerator:=First.numerator * Second.denominator;
denominator := First.denominator * Second.numerator;
tempI := MaxCommonDivisor(numerator,denominator);
Result.numerator:=numerator div tempI;
Result.denominator:=denominator div tempI;
end;
function TAccessNum.MaxCommonDivisor(iSmall, iBig: integer): integer;
begin
if iBig=0 then MaxCommonDivisor:=iSmall
else MaxCommonDivisor:=MaxCommonDivisor(iBig,iSmall mod iBig);
end;
function TAccessNum.MulFraction(First, Second: TFraction): TFraction;
var
tempI : int64;
numerator : int64;
denominator : int64;
begin
numerator:=First.numerator * Second.numerator;
denominator := First.denominator * Second.denominator;
tempI := MaxCommonDivisor(numerator,denominator);
Result.numerator:=numerator div tempI;
Result.denominator:=denominator div tempI;
end;
function TAccessNum.SubFraction(First, Second: TFraction): TFraction;
var
tempI : int64;
numerator : int64;
denominator : int64;
begin
numerator:=First.numerator * Second.denominator - First.denominator * Second.numerator;
denominator := First.denominator * Second.denominator;
tempI := MaxCommonDivisor(numerator,denominator);
Result.numerator:=numerator div tempI;
Result.denominator:=denominator div tempI;
end;
----------------------------------------------