Steppable 0.0.1
A CAS project written from scratch in C++
Loading...
Searching...
No Matches
steppable::Fraction Class Reference

Acts as a fraction in math. It can be added, subtracted, multiplied and divided. More...

#include <fraction.hpp>

Public Member Functions

 Fraction (const std::string &top="1", const std::string &bottom="1")
 Initializes a fraction with a specified top component and bottom component.
 
 Fraction (const Number &number)
 Initialized a fraction from a number.
 
 Fraction ()
 Initializes a fraction with no top component and bottom component specified. By default, this fraction equals to 1.
 
std::string present (bool inLine=true)
 Returns the fraction as a string. The string is formatted as "top/bottom", and it will automatically simplify the fraction.
 
std::array< std::string, 2 > asArray () const
 Returns the fraction as an array of its top and bottom components.
 
Fraction operator+ (const Fraction &rhs) const
 Adds two fractions together. This function does it by doing a simple fraction addition and returns the sum.
 
Fraction operator- (const Fraction &rhs) const
 Subtracts a fraction from another fraction. This function does it by doing a simple fraction subtraction and returns the difference.
 
Fraction operator* (const Fraction &rhs) const
 Multiplies two fractions together. This function does it by doing a simple fraction multiplication and returns the sum.
 
Fraction operator/ (const Fraction &rhs) const
 Divides a fraction by abother fraction. This function does it by doing a simple fraction division (multiply by inverse) and returns the sum.
 
Fraction operator^ (const Number &rhs)
 Raises the fraction to a power.
 
Fractionoperator+= (const Fraction &rhs)
 Adds two fractions together and stores the result in the current one. This function does it by doing a simple fraction addition and returns the sum.
 
Fractionoperator-= (const Fraction &rhs)
 Subtracts a fraction from another fraction and stores the result in the current one. This function does it by doing a simple fraction subtraction and returns the difference.
 
Fractionoperator*= (const Fraction &rhs)
 Multiplies two fractions together and stores the result in the current one. This function does it by doing a simple fraction multiplication and returns the sum.
 
Fractionoperator/= (const Fraction &rhs)
 Divides a fraction by abother fraction and stores the result in the current one. This function does it by doing a simple fraction division (multiply by inverse) and returns the sum.
 
Fractionoperator^= (const Number &rhs)
 Raises the fraction to a power and stores the result in the current one.
 
bool operator== (const Fraction &rhs) const
 Compares two numbers for equality.
 
bool operator!= (const Fraction &rhs) const
 
bool operator< (const Fraction &rhs) const
 Compares two numbers for greater than.
 
bool operator> (const Fraction &rhs) const
 Compare two fractions for less than.
 
bool operator<= (const Fraction &rhs) const
 Compare two fractions for less or equal than.
 
bool operator>= (const Fraction &rhs) const
 Compare two fractions for greater or equal than.
 
void reciprocal ()
 Converts a fraction to its reciprocal. This function converts the fraction to its reciprocal.
 
void simplify ()
 Simplifies a fraction. This function simplifies the fraction by dividing the top and bottom components by their greatest common divisor.
 

Private Attributes

std::string top
 The numerator. (Top component)
 
std::string bottom
 The denominator. (Bottom component)
 

Detailed Description

Acts as a fraction in math. It can be added, subtracted, multiplied and divided.

Constructor & Destructor Documentation

◆ Fraction() [1/3]

steppable::Fraction::Fraction ( const std::string & top = "1",
const std::string & bottom = "1" )
explicit

Initializes a fraction with a specified top component and bottom component.

Note
By default, the top and bottom components are 1.
Exceptions
ZeroDenominatorExceptionwhen the bottom component is zero.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Fraction() [2/3]

steppable::Fraction::Fraction ( const Number & number)
explicit

Initialized a fraction from a number.

Parameters
numberThe number to convert to a fraction.
Here is the call graph for this function:

◆ Fraction() [3/3]

steppable::Fraction::Fraction ( )

Initializes a fraction with no top component and bottom component specified. By default, this fraction equals to 1.

Here is the caller graph for this function:

Member Function Documentation

◆ asArray()

std::array< std::string, 2 > steppable::Fraction::asArray ( ) const
nodiscard

Returns the fraction as an array of its top and bottom components.

Returns
The array of top and bottom components.

◆ operator!=()

bool steppable::Fraction::operator!= ( const Fraction & rhs) const

@briefCompares two numbers for inequality.

Parameters
rhsAnother fraction.
Returns
True if the numbers are not equal, false otherwise.
Here is the call graph for this function:

◆ operator*()

Fraction steppable::Fraction::operator* ( const Fraction & rhs) const

Multiplies two fractions together. This function does it by doing a simple fraction multiplication and returns the sum.

Parameters
[in]rhsThe other fraction.
Returns
The product of fractions.
Here is the call graph for this function:

◆ operator*=()

Fraction & steppable::Fraction::operator*= ( const Fraction & rhs)

Multiplies two fractions together and stores the result in the current one. This function does it by doing a simple fraction multiplication and returns the sum.

Parameters
[in]rhsThe other fraction.
Returns
The current fraction.
Here is the call graph for this function:

◆ operator+()

Fraction steppable::Fraction::operator+ ( const Fraction & rhs) const

Adds two fractions together. This function does it by doing a simple fraction addition and returns the sum.

Parameters
[in]rhsThe other fraction.
Returns
The sum of fractions.
Here is the call graph for this function:

◆ operator+=()

Fraction & steppable::Fraction::operator+= ( const Fraction & rhs)

Adds two fractions together and stores the result in the current one. This function does it by doing a simple fraction addition and returns the sum.

Parameters
[in]rhsThe other fraction.
Returns
The current fraction.
Here is the call graph for this function:

◆ operator-()

Fraction steppable::Fraction::operator- ( const Fraction & rhs) const

Subtracts a fraction from another fraction. This function does it by doing a simple fraction subtraction and returns the difference.

Parameters
[in]rhsThe other fraction.
Returns
The difference of fractions.
Here is the call graph for this function:

◆ operator-=()

Fraction & steppable::Fraction::operator-= ( const Fraction & rhs)

Subtracts a fraction from another fraction and stores the result in the current one. This function does it by doing a simple fraction subtraction and returns the difference.

Parameters
[in]rhsThe other fraction.
Returns
The current fraction.
Here is the call graph for this function:

◆ operator/()

Fraction steppable::Fraction::operator/ ( const Fraction & rhs) const

Divides a fraction by abother fraction. This function does it by doing a simple fraction division (multiply by inverse) and returns the sum.

Parameters
[in]rhsThe other fraction.
Returns
The sum of fractions.
Here is the call graph for this function:

◆ operator/=()

Fraction & steppable::Fraction::operator/= ( const Fraction & rhs)

Divides a fraction by abother fraction and stores the result in the current one. This function does it by doing a simple fraction division (multiply by inverse) and returns the sum.

Parameters
[in]rhsThe other fraction.
Returns
The sum of fractions.
Here is the call graph for this function:

◆ operator<()

bool steppable::Fraction::operator< ( const Fraction & rhs) const

Compares two numbers for greater than.

Parameters
rhsAnother fraction.
Returns
True if the the fraction is greater than the other, false otherwise.
Here is the call graph for this function:

◆ operator<=()

bool steppable::Fraction::operator<= ( const Fraction & rhs) const

Compare two fractions for less or equal than.

Parameters
rhsAnother fraction.
Returns
True if the the fraction is less or equal than the other, false otherwise.
Here is the call graph for this function:

◆ operator==()

bool steppable::Fraction::operator== ( const Fraction & rhs) const

Compares two numbers for equality.

Parameters
rhsAnother fraction.
Returns
True if the fractions are equal, false otherwise.
Here is the call graph for this function:

◆ operator>()

bool steppable::Fraction::operator> ( const Fraction & rhs) const

Compare two fractions for less than.

Parameters
rhsAnother fraction.
Returns
True if the the fraction is less than the other, false otherwise.
Here is the call graph for this function:

◆ operator>=()

bool steppable::Fraction::operator>= ( const Fraction & rhs) const

Compare two fractions for greater or equal than.

Parameters
rhsAnother fraction.
Returns
True if the the fraction is greater or equal than the other, false otherwise.
Here is the call graph for this function:

◆ operator^()

Fraction steppable::Fraction::operator^ ( const Number & rhs)

Raises the fraction to a power.

Parameters
rhsAny number.
Returns
The result of the power operation.
Here is the call graph for this function:

◆ operator^=()

Fraction & steppable::Fraction::operator^= ( const Number & rhs)

Raises the fraction to a power and stores the result in the current one.

Parameters
rhsAny number.
Returns
The current fraction.
Here is the call graph for this function:

◆ present()

std::string steppable::Fraction::present ( bool inLine = true)

Returns the fraction as a string. The string is formatted as "top/bottom", and it will automatically simplify the fraction.

Parameters
inLineWhether to present the fraction in a single line.
Returns
The fraction as a string.
Here is the call graph for this function:

◆ reciprocal()

void steppable::Fraction::reciprocal ( )

Converts a fraction to its reciprocal. This function converts the fraction to its reciprocal.

Here is the call graph for this function:

◆ simplify()

void steppable::Fraction::simplify ( )

Simplifies a fraction. This function simplifies the fraction by dividing the top and bottom components by their greatest common divisor.

Note
The fraction will be simplified in-place.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ bottom

std::string steppable::Fraction::bottom
private

The denominator. (Bottom component)

◆ top

std::string steppable::Fraction::top
private

The numerator. (Top component)


The documentation for this class was generated from the following files:
Untitled