summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenning Brinkmann <hbrinkm@openoffice.org>2010-08-17 16:44:32 +0200
committerHenning Brinkmann <hbrinkm@openoffice.org>2010-08-17 16:44:32 +0200
commit1fd3d9c3074b80f59279189d1b06f138990d8075 (patch)
treeb606b35a215e20a30a1461b8fe002979dd3e5ab8
parent86898639d4144a078ed295d0a8bef406868802cb (diff)
writerfilter09: Fraction: constructor from Fractions, init, assign, inverse, operator=
-rw-r--r--writerfilter/inc/resourcemodel/Fraction.hxx9
-rw-r--r--writerfilter/source/resourcemodel/Fraction.cxx40
2 files changed, 44 insertions, 5 deletions
diff --git a/writerfilter/inc/resourcemodel/Fraction.hxx b/writerfilter/inc/resourcemodel/Fraction.hxx
index 1916bd433651..a038d25c98f1 100644
--- a/writerfilter/inc/resourcemodel/Fraction.hxx
+++ b/writerfilter/inc/resourcemodel/Fraction.hxx
@@ -34,10 +34,17 @@ namespace resourcemodel {
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;
diff --git a/writerfilter/source/resourcemodel/Fraction.cxx b/writerfilter/source/resourcemodel/Fraction.cxx
index 2fea20772878..98892a759bfb 100644
--- a/writerfilter/source/resourcemodel/Fraction.cxx
+++ b/writerfilter/source/resourcemodel/Fraction.cxx
@@ -53,7 +53,7 @@ sal_uInt32 gcd(sal_uInt32 a, sal_uInt32 b)
if (a < b)
{
- a -= b;
+ b -= a;
}
else
{
@@ -71,19 +71,39 @@ sal_uInt32 gcd(sal_uInt32 a, sal_uInt32 b)
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)
{
+ init(nNumerator, nDenominator);
+}
+
+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;
+ mnNumerator = nNumerator/ nGCD;
mnDenominator = nDenominator / nGCD;
}
-Fraction::~Fraction()
+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
@@ -105,6 +125,18 @@ Fraction Fraction::operator * (const Fraction & rFraction) const
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;