Guest User

Untitled

a guest
Aug 8th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.86 KB | None | 0 0
  1. Type TPositiveNumber = 0..$FFFFFF; //Int64, QWord, etc.
  2.  
  3. Type TFraction = Packed Record //Wygodniej byłoby operować na klasach, no ale...
  4.                   Numerator, Denominator: TPositiveNumber;
  5.                  End;
  6.  
  7. (* ============================ *)
  8.  
  9. Function IntToStr(I: Integer): String;
  10. Begin
  11.  Str(I, Result);
  12. End;
  13.  
  14. Function NWW(A, B: TPositiveNumber): TPositiveNumber;
  15. Var C, D: TPositiveNumber;
  16. Begin
  17.  D := A*B;
  18.  While (B <> 0) Do
  19.  Begin
  20.   C := A mod B;
  21.   A := B;
  22.   B := C;
  23.  End;
  24.  Result := D div A;
  25. End;
  26.  
  27. (* ============================ *)
  28.  
  29. Function Fraction(Num, Den: TPositiveNumber): TFraction;
  30. Begin
  31.  Result.Numerator   := Num;
  32.  Result.Denominator := Den;
  33. End;
  34.  
  35. Procedure SameDenominator(var F1, F2: TFraction);
  36. Var Mul1, Mul2, NW: TPositiveNumber;
  37. Begin
  38.  NW   := NWW(F1.Denominator, F2.Denominator);
  39.  Mul1 := NW div F1.Denominator;
  40.  Mul2 := NW div F2.Denominator;
  41.  
  42.  F1.Numerator   := F1.Numerator*Mul1;
  43.  F2.Numerator   := F2.Numerator*Mul2;
  44.  F1.Denominator := NW;
  45.  F2.Denominator := NW;
  46. End;
  47.  
  48. Function Sub(F1, F2: TFraction): TFraction;
  49. Begin
  50.  Result := Fraction(0, 0);
  51.  
  52.  if (F1.Denominator = F2.Denominator) Then
  53.   Result := Fraction(F1.Numerator-F2.Numerator, F1.Denominator) Else
  54.  Begin
  55.   SameDenominator(F1, F2);
  56.  
  57.   Result := Sub(F1, F2);
  58.  End;
  59. End;
  60.  
  61. Function Add(F1, F2: TFraction): TFraction;
  62. Begin
  63.  Result := Fraction(0, 0);
  64.  
  65.  if (F1.Denominator = F2.Denominator) Then
  66.   Result := Fraction(F1.Numerator+F2.Numerator, F1.Denominator) Else
  67.  Begin
  68.   SameDenominator(F1, F2);
  69.  
  70.   Result := Add(F1, F2);
  71.  End;
  72. End;
  73.  
  74. Function toString(F1: TFraction): String; //...np.to powinna być metoda klasy, etc, no ale...
  75. Begin
  76.  Result := IntToStr(F1.Numerator)+'/'+IntToStr(F1.Denominator);
  77. End;
  78.  
  79. Var F1, F2: TFraction;
  80. Begin
  81.  F1 := Fraction(1, 9);
  82.  F2 := Fraction(1, 5);
  83.  Writeln(toString(Sub(F1, F2)));
  84. End.
Add Comment
Please, Sign In to add comment