Commits

Anonymous committed 895a74d

writerfilter09: Fraction: constructor from Fractions, init, assign, inverse, operator=

Comments (0)

Files changed (2)

writerfilter/inc/resourcemodel/Fraction.hxx

 
 class Fraction
 {
+public:
     explicit Fraction(sal_Int32 nNumerator, sal_Int32 nDenominator = 1);
+    explicit Fraction(const Fraction & a, const Fraction & b);
     virtual ~Fraction();
 
-    Fraction operator=(const Fraction & rFraction) const;
+    void init(sal_Int32 nNumerator, sal_Int32 nDenominator);
+    void assign(const Fraction & rFraction);
+    
+    Fraction inverse() const;
+
+    Fraction operator=(const Fraction & rFraction);
     Fraction operator+(const Fraction & rFraction) const;
     Fraction operator-(const Fraction & rFraction) const;
     Fraction operator*(const Fraction & rFraction) const;

writerfilter/source/resourcemodel/Fraction.cxx

 
         if (a < b)
         {
-            a -= b;
+            b -= a;
         }
         else
         {
 
 sal_uInt32 lcm(sal_Int32 a, sal_Int32 b)
 {
-    return abs(a * b) / gcd(a, b);
+    return abs(a * b) / gcd(abs(a), abs(b));
 }
 
 Fraction::Fraction(sal_Int32 nNumerator, sal_Int32 nDenominator)
 {
-    sal_uInt32 nGCD = gcd(nNumerator, nDenominator);
+    init(nNumerator, nDenominator);
+}
 
-    mnNumerator = nNumerator / nGCD;
-    mnDenominator = nDenominator / nGCD;
+Fraction::Fraction(const Fraction & a, const Fraction & b)
+{
+    init(a.mnNumerator * b.mnDenominator, a.mnDenominator * b.mnNumerator);
 }
 
 Fraction::~Fraction()
 {
 }
 
+void Fraction::init(sal_Int32 nNumerator, sal_Int32 nDenominator)
+{
+    sal_uInt32 nGCD = gcd(nNumerator, nDenominator);
+
+    mnNumerator = nNumerator/ nGCD;
+    mnDenominator = nDenominator / nGCD;
+}
+
+void Fraction::assign(const Fraction & rFraction)
+{
+    init(rFraction.mnNumerator, rFraction.mnDenominator);
+}
+
+Fraction Fraction::inverse() const
+{
+    return Fraction(mnDenominator, mnNumerator);
+}
+
 Fraction Fraction::operator + (const Fraction & rFraction) const 
 {
     sal_uInt32 nLCM = lcm(mnDenominator, rFraction.mnDenominator);
     return Fraction(mnNumerator * rFraction.mnNumerator, mnDenominator * rFraction.mnDenominator);
 }
 
+Fraction Fraction::operator / (const Fraction & rFraction) const 
+{
+    return *this * rFraction.inverse();
+}
+
+Fraction Fraction::operator = (const Fraction & rFraction)
+{
+    assign(rFraction);
+
+    return *this;
+}
+
 Fraction::operator sal_Int32() const
 {
     return mnNumerator / mnDenominator;