summaryrefslogtreecommitdiffstats
path: root/general/Frac.cpp
blob: 9cf47aca744b13b786c448085356fcb4ef2b1ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/////////////////////////////////////////////////////////////////////////////
// Name:        general/Frac.cpp
// Purpose:     A class to make handling fractions easier
// Author:      Ben Boeckel
// Modified by: Ben Boeckel
// Created:     Sun Mar 18 15:25:16 2007
// Copyright:   ©2007-2008 Ben Boeckel and Nerdy Productions
// Licence:
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License along
//    with this program.  If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////////

#include "Frac.h"

void Frac::set(const int n, const int d) throw(Exception)
{
    if (((type == Improper) || ((n <= d) && (type == Proper)) || ((d < n) && (type == Over1))) && (0 < d))
    {
        num = n;
        denom = d;
    }
    else
        throw(Exception("Frac", "values conflict with type"));
}

void Frac::set(const int n, const int d, const int t) throw(Exception)
{
    setType(t);
    set(n, d);
}

void Frac::setNum(const int n) throw(Exception)
{
    set(n, denom);
}

void Frac::setDenom(const int d) throw(Exception)
{
    set(num, d);
}

void Frac::setType(const int t) throw(BoundsException)
{
    if (t < End)
    {
        type = t;
        set(1, 1);
    }
    else
        throw(BoundsException("Frac", "type"));
}

void Frac::reduce()
{
    int i = num;
    int j = denom;
    int k;
    while (j)
    {
        k = j;
        j = i % j;
        i = k;
    }
    num /= i;
    denom /= i;
}