Fraction Library
A C++ Fraction library.

This class allows you to handle improper as well as mixed fractions just like how basic types such as integers and doubles are handled in c++. More...
#include <MFraction.h>
Public Member Functions  
Constructors/Destructor  
A constructor initilizes (creates) an object of type MFraction. A destructor destroys an object of type MFraction when it goes out of scope.  
MFraction ()  
Default Constructor: sets fraction to 0.  
MFraction (const double &number)  
Converts a decimal number (or integer) into a fraction.  
MFraction (const int &numerator, const int &denominator)  
Takes numerator and denominator of fraction (not reduced) and creates a reduced mixed fraction.  
MFraction (const int &whole, const int &numerator, const int &denominator)  
Takes whole, numerator and denominator of fraction (not reduced) and creates a reduced mixed fraction.  
MFraction (const char *frac)  
Converts a valid character array into a fraction.  
MFraction (const Fraction &frac)  
Copy Constructor: copies another fraction (of type Fraction) into the created fraction (of type MFraction).  
MFraction (const MFraction &frac)  
Copy Constructor: copies another fraction (of type MFraction) into the created fraction (of type MFraction).  
~MFraction ()  
Destructor: used to destory objects of type MFraction.  
Mutator Methods  
Use these methods to set the whole/numerator/denominator independently. (to get the values of the whole, numerator and denominator, see MFraction::operator [])  
void  setWhole (int whole) 
Sets the whole number portion of the fraction.  
void  setNum (int numerator) 
Sets the numerator of the fraction.  
void  setDen (int denominator) 
Sets the denomerator of the fraction.  
Member Overloaded Operators  
The following are overloaded operators which are members of the class. One of the "parameters" to the operation is implicit and is the "calling object". The calling object must be of type MFraction. For instance, x[0] and x, where x is the calling object.  
int  operator[] (const unsigned int &subscript) const 
Subscript operator: Pass in 0 to get whole (signed), 1 to get numerator (signed), 2 for denominator (unsigned).  
MFraction &  operator= (const MFraction &right) 
Assignment operator: Calling object is made equal to object on the right of operator.  
MFraction  operator () const 
Negation operator: returns the negated value of the calling MFraction object.  
MFraction  operator++ () 
Prefix increment operator: adds 1 to fraction.  
MFraction  operator () 
Prefix decrement operator: subtracts 1 from fraction.  
MFraction  operator++ (int inc) 
Postfix increment operator: increments fraction by inc; if inc is 0, increments by 1.  
MFraction  operator (int dec) 
Postfix decrement operator: decrements fraction by dec; if dec is 0, decrements by 1.  
MFraction  operator+= (const MFraction &right) 
Addition/assignment operator: adds fraction on the right of operator to fraction on the left.  
MFraction  operator= (const MFraction &right) 
Subtraction/assignment operator: subtracts fraction on the right of operator from fraction on the left.  
MFraction  operator*= (const MFraction &right) 
Multiplication/assignment operator: calling object is multiplied by fraction on the right of the operator.  
MFraction  operator/= (const MFraction &right) 
Divition/assignment operator: calling object is divided by fraction on the right of the operator.  
Static Public Member Functions  
static void  setFormat (const FracFormat &format) 
Sets the format of how objects of type MFraction is outputted.  
static FracFormat  getFormat () 
Use to see what the current output format of fractions of type MFraction is.  
Private Member Functions  
void  reduce () 
Reduces a reduced improper fraction into a mixed fraction.  
Private Attributes  
C  __pad0__:/Users/EnemyUnited/My Stuff/Fraction Library 1.1/include/MFraction.h" 2 public: enum FracFormat { MIX_FRAC = 0 
= 0: output as mixed fraction when it applies, ie 5/6 and 1/2/3  
C  IM_FRAC = 1 
= 1: output as improper fractions ie. 5/6, 8/5  
C  DECI 
= 2: output as a decimal number ie. 3.5  
unsigned int  whole 
Stores the whole number portion of the fraction.  
Static Private Attributes  
static FracFormat  FORMAT = MIX_FRAC 
Stores how objects of type MFraction is to be outputted.  
Friends  
Comparison Operations  
These are declared as friends so that it is possible to 25 < x or 5==x where x is a MFraction object. Parameters are automatically converted to MFraction objects when values of integer, double, string or Fraction objects are provided. However, note that at least one of the parameters must be an object of type MFraction.  
bool  operator== (const MFraction &left, const MFraction &right) 
Equality operator: checks if two fractions are same.  
bool  operator!= (const MFraction &left, const MFraction &right) 
Inequality operator: checks if two fractions arent equal.  
bool  operator< (const MFraction &left, const MFraction &right) 
Less than operator: checks if fraction on the left of operator is less than fraction on the right.  
bool  operator<= (const MFraction &left, const MFraction &right) 
Less than or equal to operator: checks if fraction on the left of operator is less than or equal to fraction on the right.  
bool  operator> (const MFraction &left, const MFraction &right) 
Greater than operator: checks if fraction on the left of operator is greater than fraction on the right.  
bool  operator>= (const MFraction &left, const MFraction &right) 
Greater than or equal to operator: checks if fraction on the left of operator is greater than or equal to fraction on the right.  
Arithmitic Operations  
These are declared as friends so that it is possible to 25 + x or 5/x where x is a MFraction object. Parameters are automatically converted to MFraction objects when values of integer, double, string or Fraction objects are provided. However, note that at least one of the parameters must be an object of type MFraction.  
MFraction  operator+ (const MFraction &left, const MFraction &right) 
Addition operator: adds the two fractions on either side of the operator.  
MFraction  operator (const MFraction &left, const MFraction &right) 
Subtraction operator: subtracts the two fractions on either side of the operator.  
MFraction  operator* (const MFraction &left, const MFraction &right) 
Multiplication operator: multiplies the two fractions on either side of the operator.  
MFraction  operator/ (const MFraction &left, const MFraction &right) 
Division operator: left fraction is divided by the fraction on the right of the operator.  
Stream input/output operations  
ostream &  operator<< (ostream &out, const MFraction &fraction) 
Stream output: output fraction in the format specified by MFraction::FORMAT.  
istream &  operator>> (istream &in, MFraction &fraction) 
Stream input: assign input to a fraction object. 
This class allows you to handle improper as well as mixed fractions just like how basic types such as integers and doubles are handled in c++.
Improper fractions are of the form numerator/denominator whereas mixed fractions of the form whole/numerator/denominator where numerator in this case is always less than the denominator. For instance, 5/6 or 4/2/3. These fractions will always be in their reduced form.
The programmer has a choice of outputting the fraction as a decimal number, as an improper fraction or as a mixed fraction. This applies to all fraction objects that are of type MFraction. For instance, 3.5 or 7/2 or 3/1/2. See documentation of "Static Public Member Functions" for details.
The programmer can initilize fraction objects by providing (note the integer or string values provided do not have to be in their reduced fractional form):
Arithmetic and comparison operations can also be performed on MFraction objects, analogous to how these operations work on values of basic types such as an int.
I/O with MFractions works similarly to how it works with integers as well. For instance, 6/4/2, 2/3, 8/4 and 5.6 are all valid inputs. These inputs will be stored in their reduced form.
Definition at line 44 of file MFraction.h.
MFraction::MFraction  (  ) 
Default Constructor: sets fraction to 0.
This is the constructor that is used when no constructor is explicitely specifed.
Definition at line 48 of file MFraction.cpp.
MFraction::MFraction  (  const double &  number  ) 
Converts a decimal number (or integer) into a fraction.
number  Decimal number (or integer) to convert to fraction. 
Definition at line 55 of file MFraction.cpp.
MFraction::MFraction  (  const int &  numerator, 
const int &  denominator  
) 
Takes numerator and denominator of fraction (not reduced) and creates a reduced mixed fraction.
numerator  Numerator of fraction. 
denominator  Denominator of fraction. 
If  denominator is zero, throws FR_ERROR with value FR_DENOM_ZERO. Object is left uncreated (also means destructor will not run as well). 
Definition at line 62 of file MFraction.cpp.
MFraction::MFraction  (  const int &  whole, 
const int &  numerator,  
const int &  denominator  
) 
Takes whole, numerator and denominator of fraction (not reduced) and creates a reduced mixed fraction.
whole  Whole number portion of the fraction. 
numerator  Numerator of fraction. 
denominator  Denominator of fraction. 
If  denominator is zero, throws FR_ERROR with value FR_DENOM_ZERO. Object is left uncreated (also means destructor will not run as well). 
Definition at line 69 of file MFraction.cpp.
MFraction::MFraction  (  const char *  frac  ) 
Converts a valid character array into a fraction.
frac  Character array to convert. Possible values include "3.4", "4/7/3" and "16/8". Invalid values include "3.4.5" and "3.4/5.6". 
If  input is invalid, throws FR_ERROR with value FR_STR_INVALID. Object is left uncreated (also means destructor will not run as well). 
Definition at line 77 of file MFraction.cpp.
MFraction::MFraction  (  const Fraction &  frac  ) 
Copy Constructor: copies another fraction (of type Fraction) into the created fraction (of type MFraction).
frac  Fraction object to copy. 
Definition at line 117 of file MFraction.cpp.
MFraction::MFraction  (  const MFraction &  frac  ) 
Copy Constructor: copies another fraction (of type MFraction) into the created fraction (of type MFraction).
frac  MFraction object to copy. 
Definition at line 124 of file MFraction.cpp.
MFraction::~MFraction  (  ) 
Destructor: used to destory objects of type MFraction.
This cannot be called explicitely.
Definition at line 131 of file MFraction.cpp.
MFraction::FracFormat MFraction::getFormat  (  )  [static] 
Use to see what the current output format of fractions of type MFraction is.
Reimplemented from Fraction.
Definition at line 26 of file MFraction.cpp.
Multiplication/assignment operator: calling object is multiplied by fraction on the right of the operator.
Calling object must be of type MFraction. The calling object is the fraction that is on the left of the operator.
right  MFraction object on the right of operator that is to be multiplied with. If values of integer, double, strings or Fraction are provided, they will be automatically converted to MFraction objects prior to multiplication/assignment. 
Definition at line 255 of file MFraction.cpp.
MFraction MFraction::operator++  (  ) 
Prefix increment operator: adds 1 to fraction.
Reimplemented from Fraction.
Definition at line 190 of file MFraction.cpp.
MFraction MFraction::operator++  (  int  inc  ) 
Postfix increment operator: increments fraction by inc; if inc is 0, increments by 1.
inc 

Reimplemented from Fraction.
Definition at line 202 of file MFraction.cpp.
Addition/assignment operator: adds fraction on the right of operator to fraction on the left.
Calling object must be of type Mraction. The calling object is the fraction that is on the left of the operator.
right  MFraction object on the right of operator that is to be added. If values of integer, double or strings are provided, they will be automatically converted to MFraction objects prior to addition/assignment. 
Definition at line 239 of file MFraction.cpp.
MFraction MFraction::operator  (  )  const 
Negation operator: returns the negated value of the calling MFraction object.
Reimplemented from Fraction.
Definition at line 183 of file MFraction.cpp.
MFraction MFraction::operator  (  ) 
Prefix decrement operator: subtracts 1 from fraction.
Reimplemented from Fraction.
Definition at line 196 of file MFraction.cpp.
MFraction MFraction::operator  (  int  dec  ) 
Postfix decrement operator: decrements fraction by dec; if dec is 0, decrements by 1.
dec 

Reimplemented from Fraction.
Definition at line 220 of file MFraction.cpp.
Subtraction/assignment operator: subtracts fraction on the right of operator from fraction on the left.
Calling object must be of type MFraction. The calling object is the fraction that is on the left of the operator.
right  MFraction object on the right of operator that is to be subtracted. If values of integer, double, strings or Fraction are provided, they will be automatically converted to MFraction objects prior to subtraction/assignment. 
Definition at line 247 of file MFraction.cpp.
Divition/assignment operator: calling object is divided by fraction on the right of the operator.
Calling object must be of type MFraction. The calling object is the fraction that is on the left of the operator.
right  MFraction object on the right of operator that is to be divided by. If values of integer, double, strings or Fraction are provided, they will be automatically converted to Fraction objects prior to division/assignment. 
Definition at line 263 of file MFraction.cpp.
Assignment operator: Calling object is made equal to object on the right of operator.
If a Fraction object is on the right of the operator, it will be automatically converted to a MFraction (using the available constructor) and then assigned to the calling object. This wont work if a MFraction object is being assigned to a Fraction object (the whole portion will be removed in that case).
Definition at line 175 of file MFraction.cpp.
int MFraction::operator[]  (  const unsigned int &  subscript  )  const 
Subscript operator: Pass in 0 to get whole (signed), 1 to get numerator (signed), 2 for denominator (unsigned).
This operator must be a member function.
subscript  value of 0, 1 or 2. Any other value will throw an exception. 
throws  FR_ERROR with value FR_INDEX_OUT_BOUNDS if subscript is greater than 2. 
Reimplemented from Fraction.
Definition at line 162 of file MFraction.cpp.
void MFraction::reduce  (  )  [private] 
Reduces a reduced improper fraction into a mixed fraction.
For instance, 6/5 > 1/1/5
Reimplemented from Fraction.
Definition at line 34 of file MFraction.cpp.
void MFraction::setDen  (  int  denominator  ) 
Sets the denomerator of the fraction.
denominator  The number to set the denomerator of the fraction to. 
Throws  FR_ERROR with value FR_DENOM_ZERO if parameter's value is 0. 
Reimplemented from Fraction.
Definition at line 152 of file MFraction.cpp.
void MFraction::setFormat  (  const FracFormat &  format  )  [static] 
Sets the format of how objects of type MFraction is outputted.
format  What the format of the fraction should be:

Reimplemented from Fraction.
Definition at line 20 of file MFraction.cpp.
void MFraction::setNum  (  int  numerator  ) 
Sets the numerator of the fraction.
numerator  The number to set the numerator of the fraction to. 
Reimplemented from Fraction.
Definition at line 145 of file MFraction.cpp.
void MFraction::setWhole  (  int  whole  ) 
Sets the whole number portion of the fraction.
whole  The number to set the whole of the fraction to. 
Definition at line 138 of file MFraction.cpp.
Inequality operator: checks if two fractions arent equal.
left  MFraction object to the left of the operator. 
right  MFraction object to the right of the operator. 
Definition at line 278 of file MFraction.cpp.
Multiplication operator: multiplies the two fractions on either side of the operator.
left  MFraction on the left of the operator 
right  MFraction on the right of the operator 
Definition at line 322 of file MFraction.cpp.
Addition operator: adds the two fractions on either side of the operator.
left  MFraction on the left of the operator 
right  MFraction on the right of the operator 
Definition at line 310 of file MFraction.cpp.
Subtraction operator: subtracts the two fractions on either side of the operator.
left  MFraction on the left of the operator 
right  MFraction on the right of the operator 
Definition at line 316 of file MFraction.cpp.
Division operator: left fraction is divided by the fraction on the right of the operator.
left  MFraction on the left of the operator 
right  MFraction on the right of the operator 
Definition at line 328 of file MFraction.cpp.
Less than operator: checks if fraction on the left of operator is less than fraction on the right.
left  MFraction object to the left of the operator. 
right  MFraction object to the right of the operator. 
Definition at line 284 of file MFraction.cpp.
ostream& operator<<  (  ostream &  out, 
const MFraction &  fraction  
)  [friend] 
Stream output: output fraction in the format specified by MFraction::FORMAT.
out  output stream (operation assumes stream is already setup (ie. connected to file, screen etc.) 
fraction  MFraction object to output. 
Definition at line 336 of file MFraction.cpp.
Less than or equal to operator: checks if fraction on the left of operator is less than or equal to fraction on the right.
left  MFraction object to the left of the operator. 
right  MFraction object to the right of the operator. 
Definition at line 292 of file MFraction.cpp.
Equality operator: checks if two fractions are same.
left  MFraction object to the left of the operator. 
right  MFraction object to the right of the operator. 
Definition at line 272 of file MFraction.cpp.
Greater than operator: checks if fraction on the left of operator is greater than fraction on the right.
left  MFraction object to the left of the operator. 
right  MFraction object to the right of the operator. 
Definition at line 298 of file MFraction.cpp.
Greater than or equal to operator: checks if fraction on the left of operator is greater than or equal to fraction on the right.
left  MFraction object to the left of the operator. 
right  MFraction object to the right of the operator. 
Definition at line 304 of file MFraction.cpp.
istream& operator>>  (  istream &  in, 
MFraction &  fraction  
)  [friend] 
Stream input: assign input to a fraction object.
in  input stream (operation assumes stream is already setup (ie. connected to file, screen etc.) 
fraction  The input from stream is assigned to this fraction object. 
Definition at line 358 of file MFraction.cpp.
C MFraction::__pad0__ [private] 
= 0: output as mixed fraction when it applies, ie 5/6 and 1/2/3
Definition at line 54 of file MFraction.h.
C MFraction::DECI [private] 
2 }
= 2: output as a decimal number ie. 3.5
Reimplemented from Fraction.
Definition at line 56 of file MFraction.h.
MFraction::FracFormat MFraction::FORMAT = MIX_FRAC [static, private] 
Stores how objects of type MFraction is to be outputted.
This applies to all objects of type MFraction. The default behaviour is as a mixed fraction. Use MFraction::setFormat() to set MFraction format and MFraction::getFormat() to get the format. You can set/get the following values:
Reimplemented from Fraction.
Definition at line 71 of file MFraction.h.
C MFraction::IM_FRAC = 1 [private] 
= 1: output as improper fractions ie. 5/6, 8/5
Reimplemented from Fraction.
Definition at line 55 of file MFraction.h.
unsigned int MFraction::whole [private] 
Stores the whole number portion of the fraction.
Definition at line 97 of file MFraction.h.