Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Type TPositiveNumber = 0..$FFFFFF; //Int64, QWord, etc.
- Type TFraction = Packed Record //Wygodniej byłoby operować na klasach, no ale...
- Numerator, Denominator: TPositiveNumber;
- End;
- (* ============================ *)
- Function IntToStr(I: Integer): String;
- Begin
- Str(I, Result);
- End;
- Function NWW(A, B: TPositiveNumber): TPositiveNumber;
- Var C, D: TPositiveNumber;
- Begin
- D := A*B;
- While (B <> 0) Do
- Begin
- C := A mod B;
- A := B;
- B := C;
- End;
- Result := D div A;
- End;
- (* ============================ *)
- Function Fraction(Num, Den: TPositiveNumber): TFraction;
- Begin
- Result.Numerator := Num;
- Result.Denominator := Den;
- End;
- Procedure SameDenominator(var F1, F2: TFraction);
- Var Mul1, Mul2, NW: TPositiveNumber;
- Begin
- NW := NWW(F1.Denominator, F2.Denominator);
- Mul1 := NW div F1.Denominator;
- Mul2 := NW div F2.Denominator;
- F1.Numerator := F1.Numerator*Mul1;
- F2.Numerator := F2.Numerator*Mul2;
- F1.Denominator := NW;
- F2.Denominator := NW;
- End;
- Function Sub(F1, F2: TFraction): TFraction;
- Begin
- Result := Fraction(0, 0);
- if (F1.Denominator = F2.Denominator) Then
- Result := Fraction(F1.Numerator-F2.Numerator, F1.Denominator) Else
- Begin
- SameDenominator(F1, F2);
- Result := Sub(F1, F2);
- End;
- End;
- Function Add(F1, F2: TFraction): TFraction;
- Begin
- Result := Fraction(0, 0);
- if (F1.Denominator = F2.Denominator) Then
- Result := Fraction(F1.Numerator+F2.Numerator, F1.Denominator) Else
- Begin
- SameDenominator(F1, F2);
- Result := Add(F1, F2);
- End;
- End;
- Function toString(F1: TFraction): String; //...np.to powinna być metoda klasy, etc, no ale...
- Begin
- Result := IntToStr(F1.Numerator)+'/'+IntToStr(F1.Denominator);
- End;
- Var F1, F2: TFraction;
- Begin
- F1 := Fraction(1, 9);
- F2 := Fraction(1, 5);
- Writeln(toString(Sub(F1, F2)));
- End.
Add Comment
Please, Sign In to add comment