File: rational.h

package info (click to toggle)
ocrad 0.17-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 624 kB
  • ctags: 687
  • sloc: cpp: 7,809; sh: 161; makefile: 149
file content (116 lines) | stat: -rw-r--r-- 5,490 bytes parent folder | download | duplicates (2)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*  GNU Ocrad - Optical Character Recognition program
    Copyright (C) 2003, 2004, 2005, 2006, 2007 Antonio Diaz Diaz.

    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/>.
*/

// Rationals are kept normalized at all times.
// Invariant = ( gcd( num, den ) == 1 && den > 0 ).
//
class Rational
  {
  int num, den;

  void normalize() throw();

public:
  explicit Rational( const int n = 0 ) throw() : num( n ), den( 1 ) {}
  Rational( const int n, const int d ) throw() : num( n ), den( d ) { normalize(); }

  Rational & assign( const int n, const int d ) throw()
    { num = n; den = d; normalize(); return *this; }
  Rational & operator=( const int n ) throw()
    { num = n; den = 1; return *this; }

  int numerator() const throw() { return num; }
  int denominator() const throw() { return den; }

  Rational & operator+() throw() { return *this; }		// Unary plus
  Rational operator-() const throw()				// Unary minus
    { Rational tmp = *this; tmp.num = -tmp.num; return tmp; }

  Rational & operator+=( const Rational & r ) throw();
  Rational & operator-=( const Rational & r ) throw() { return operator+=( -r ); }
  Rational & operator*=( const Rational & r ) throw();
  Rational & operator/=( const Rational & r ) throw();

  Rational & operator+=( const int n ) throw() { num += n * den; return *this; }
  Rational & operator-=( const int n ) throw() { num -= n * den; return *this; }
  Rational & operator*=( const int n ) throw() { return operator*=( Rational( n ) ); }
  Rational & operator/=( const int n ) throw() { return operator/=( Rational( n ) ); }

  Rational operator+( const Rational & r ) const throw()
    { Rational tmp = *this; tmp += r; return tmp; }
  Rational operator-( const Rational & r ) const throw()
    { Rational tmp = *this; tmp -= r; return tmp; }
  Rational operator*( const Rational & r ) const throw()
    { Rational tmp = *this; tmp *= r; return tmp; }
  Rational operator/( const Rational & r ) const throw()
    { Rational tmp = *this; tmp /= r; return tmp; }

  Rational operator+( const int n ) const throw()
    { Rational tmp = *this; tmp += n; return tmp; }
  Rational operator-( const int n ) const throw()
    { Rational tmp = *this; tmp -= n; return tmp; }
  Rational operator*( const int n ) const throw()
    { Rational tmp = *this; tmp *= n; return tmp; }
  Rational operator/( const int n ) const throw()
    { Rational tmp = *this; tmp /= n; return tmp; }

  Rational & operator++() throw() { num += den; return *this; }	// prefix
  Rational operator++( int ) throw()				// suffix
    { Rational tmp = *this; num += den; return tmp; }
  Rational & operator--() throw() { num -= den; return *this; }	// prefix
  Rational operator--( int ) throw()				// suffix
    { Rational tmp = *this; num -= den; return tmp; }

  bool operator!() const throw() { return !num; }

  bool operator==( const Rational & r ) const throw()
    { return ( num == r.num && den == r.den ); }
  bool operator==( const int n ) const throw()
    { return ( num == n && den == 1 ); }
  bool operator!=( const Rational & r ) const throw() { return !( *this == r ); }
  bool operator!=( const int n ) const throw()        { return !( *this == n ); }

  bool operator< ( const Rational & r ) const throw();
  bool operator<=( const Rational & r ) const throw() { return ( *this < r || *this == r ); }
  bool operator> ( const Rational & r ) const throw() { return !( *this <= r ); }
  bool operator>=( const Rational & r ) const throw() { return !( *this <  r ); }

  bool operator< ( const int n ) const throw() { return operator< ( Rational( n ) ); }
  bool operator<=( const int n ) const throw() { return operator<=( Rational( n ) ); }
  bool operator> ( const int n ) const throw() { return operator> ( Rational( n ) ); }
  bool operator>=( const int n ) const throw() { return operator>=( Rational( n ) ); }

  Rational abs() const throw()
    { if( num >= 0 ) return *this; else return -*this; }
  Rational inverse() const throw();
  int round() const throw();
  int trunc() const throw() { return ( num / den ); }

  int parse( const char * ptr ) throw();
  const std::string to_decimal( const int iwidth = 1, int prec = -2 ) const throw();
  };


inline Rational operator+( const int n, const Rational & r ) throw() { return r + n; }
inline Rational operator-( const int n, const Rational & r ) throw() { return -r + n; }
inline Rational operator*( const int n, const Rational & r ) throw() { return r * n; }
inline Rational operator/( const int n, const Rational & r ) throw() { return Rational( n ) / r; }

inline bool operator< ( const int n, const Rational & r ) throw() { return r >  n; }
inline bool operator<=( const int n, const Rational & r ) throw() { return r >= n; }
inline bool operator> ( const int n, const Rational & r ) throw() { return r <  n; }
inline bool operator>=( const int n, const Rational & r ) throw() { return r <= n; }