-
Notifications
You must be signed in to change notification settings - Fork 0
/
RationalNum.h
83 lines (75 loc) · 4.37 KB
/
RationalNum.h
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
#ifndef RATIONAL_NUM_H
#define RATIONAL_NUM_H
#include "NaturalNum.h"
//////////////////////////////////////////////////////////////////////////
// //
// CLASS NAME: RationalNum //
// //
// DESCRIPTION: Stores a rational number of any conceivable size. //
// Supports any arithmetic operation provided by C++. //
// Achieves complete accuracy (unlike type double), by //
// maintaining the number as enumerator and denominator. //
// //
// REMARKS: An exception is thrown when the result is either //
// infinite (INF constant) or undefined (NaN constant). //
// //
// AUTHOR: Barakman ([email protected]) //
// //
//////////////////////////////////////////////////////////////////////////
class RationalNum
{
public: //Constructors + Destructor
RationalNum();
virtual ~RationalNum();
RationalNum(double fRationalNum);
RationalNum(const NaturalNum& cNaturalNum);
RationalNum(const RationalNum& cRationalNum);
//---------------------------------------------------------------------------------------------
public: //Assignment Operators
virtual const RationalNum& operator=(const RationalNum& cRationalNum);
virtual const RationalNum& operator+=(const RationalNum& cRationalNum);
virtual const RationalNum& operator-=(const RationalNum& cRationalNum);
virtual const RationalNum& operator*=(const RationalNum& cRationalNum);
virtual const RationalNum& operator/=(const RationalNum& cRationalNum);
//---------------------------------------------------------------------------------------------
public: //Unary Operators
virtual operator NaturalNum() const;
virtual RationalNum operator+() const;
virtual RationalNum operator-() const;
virtual const RationalNum& operator++();
virtual const RationalNum& operator--();
virtual RationalNum operator++(int dummy);
virtual RationalNum operator--(int dummy);
//---------------------------------------------------------------------------------------------
public: //Binary Operators
friend RationalNum operator+(const RationalNum& cRationalNum1,const RationalNum& cRationalNum2);
friend RationalNum operator-(const RationalNum& cRationalNum1,const RationalNum& cRationalNum2);
friend RationalNum operator*(const RationalNum& cRationalNum1,const RationalNum& cRationalNum2);
friend RationalNum operator/(const RationalNum& cRationalNum1,const RationalNum& cRationalNum2);
//---------------------------------------------------------------------------------------------
public: //Binary Comperators
friend bool operator<(const RationalNum& cRationalNum1,const RationalNum& cRationalNum2);
friend bool operator>(const RationalNum& cRationalNum1,const RationalNum& cRationalNum2);
friend bool operator==(const RationalNum& cRationalNum1,const RationalNum& cRationalNum2);
friend bool operator<=(const RationalNum& cRationalNum1,const RationalNum& cRationalNum2);
friend bool operator>=(const RationalNum& cRationalNum1,const RationalNum& cRationalNum2);
friend bool operator!=(const RationalNum& cRationalNum1,const RationalNum& cRationalNum2);
//---------------------------------------------------------------------------------------------
public: //Interface Routines
NaturalNum Enumerator() const;
NaturalNum Denominator() const;
RationalNum Power(unsigned int iExponent) const;
RationalNum Root(unsigned int iExponent,unsigned int iAccuracy=4) const;
void ToString(char aString[],unsigned int iBase,unsigned int iPrecision) const;
friend RationalNum StringToRationalNum(const char aString[],unsigned int iBase);
//---------------------------------------------------------------------------------------------
protected: //Internal Routines
void RemoveRedundantFactors();
//---------------------------------------------------------------------------------------------
protected: //Object Attributes
bool m_bSign;
NaturalNum m_cEnumerator;
NaturalNum m_cDenominator;
//---------------------------------------------------------------------------------------------
};
#endif