Skip to content

Commit

Permalink
spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipNelson5 committed Mar 31, 2018
1 parent c8a9710 commit 62a715e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 96 deletions.
197 changes: 112 additions & 85 deletions CS1410_CS2/1-RationalNumber/Rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
/*-------------------------------------------------------------------------------*/
/* [constructor] */
/*-------------------------------------------------------------------------------*/
Rational::Rational(int n, int d) :numerator(n), denominator(d) {
if (d == 0) d = 1;
this->simplify();
Rational::Rational(int n, int d) : numerator(n), denominator(d)
{
if (d == 0) d = 1;
this->simplify();
};

/*-------------------------------------------------------------------------------*/
Expand All @@ -22,138 +23,164 @@ void Rational::setDenominator(int d) { denominator = d; }
/*-------------------------------------------------------------------------------*/
/* [simplification] */
/*-------------------------------------------------------------------------------*/
int GCD(int a, int b) {
if (b == 0) return a;
return GCD(b, a%b);
int GCD(int a, int b)
{
if (b == 0) return a;
return GCD(b, a%b);
}

void Rational::simplify() {
int gcd = GCD(denominator, numerator);
numerator /= gcd;
denominator /= gcd;
void Rational::simplify()
{
int gcd = GCD(denominator, numerator);
numerator /= gcd;
denominator /= gcd;

if (denominator < 0){
numerator = -numerator;
denominator = -denominator;
}
if (denominator < 0)
{
numerator = -numerator;
denominator = -denominator;
}
}

/*-------------------------------------------------------------------------------*/
/* [casting] */
/*-------------------------------------------------------------------------------*/
Rational::operator float() {
return static_cast<float>(numerator) / denominator;
Rational::operator float()
{
return static_cast<float>(numerator) / denominator;
}

Rational::operator double() {
return static_cast<double>(numerator) / denominator;
Rational::operator double()
{
return static_cast<double>(numerator) / denominator;
}

/*-------------------------------------------------------------------------------*/
/* [extraction and insertion] */
/*-------------------------------------------------------------------------------*/

std::ostream& operator<< (std::ostream& o, Rational const & a) {
if (a.getNumerator() == 0)
o << 0;
else if (a.getDenominator() == 1)
o << a.getNumerator();
else
o << a.getNumerator() << "/" << a.getDenominator();

return o;
}

std::istream& operator>> (std::istream& i, Rational& a) {
int t = 0;
char ignore = ' ';
i >> t;
a.setNumerator(t);
if(i >> ignore){
i >> t;
if (t != 0)
a.setDenominator(t);
else a.setDenominator(1);
}
else
a.setDenominator(1);
a.simplify();
return i;
std::ostream& operator<<(std::ostream& o, Rational const& a)
{
if (a.getNumerator() == 0)
o << 0;
else if (a.getDenominator() == 1)
o << a.getNumerator();
else
o << a.getNumerator() << "/" << a.getDenominator();

return o;
}

std::istream& operator>>(std::istream& i, Rational& a)
{
int t = 0;
char ignore = ' ';
i >> t;
a.setNumerator(t);
if (i >> ignore)
{
i >> t;
if (t != 0)
a.setDenominator(t);
else
a.setDenominator(1);
}
else
a.setDenominator(1);
a.simplify();
return i;
}

/*-------------------------------------------------------------------------------*/
/* [arithmetic operators] */
/*-------------------------------------------------------------------------------*/
Rational operator-(Rational const & a) {
return Rational(-a.getNumerator(), a.getDenominator());
Rational operator-(Rational const& a)
{
return Rational(-a.getNumerator(), a.getDenominator());
}

Rational operator+ (Rational const & a, Rational const & b) {
Rational c(
a.getNumerator()*b.getDenominator() + b.getNumerator()*a.getDenominator(),
a.getDenominator()*b.getDenominator());
c.simplify();
return c;
Rational operator+(Rational const& a, Rational const& b)
{
Rational c(a.getNumerator() * b.getDenominator() +
b.getNumerator() * a.getDenominator(),
a.getDenominator() * b.getDenominator());
c.simplify();
return c;
}

Rational operator+= (Rational& a, Rational const & b) {
return a = a + b;
Rational operator+=(Rational& a, Rational const& b)
{
return a = a + b;
}

Rational operator- (Rational const & a, Rational const & b) {
Rational c = a + (-b);
c.simplify();
return c;
Rational operator-(Rational const& a, Rational const& b)
{
Rational c = a + (-b);
c.simplify();
return c;
}

Rational operator-= (Rational& a, Rational const & b) {
return a = a - b;
Rational operator-=(Rational& a, Rational const& b)
{
return a = a - b;
}

Rational operator* (Rational const & a, Rational const & b) {
Rational c(
a.getNumerator()*b.getNumerator(),
a.getDenominator()*b.getDenominator());
c.simplify();
return c;
Rational operator*(Rational const& a, Rational const& b)
{
Rational c(a.getNumerator() * b.getNumerator(),
a.getDenominator() * b.getDenominator());
c.simplify();
return c;
}

Rational operator*= (Rational& a, Rational const & b) {
return a = a * b;
Rational operator*=(Rational& a, Rational const& b)
{
return a = a * b;
}

Rational operator/ (Rational const & a, Rational const & b) {
Rational c(b.getDenominator(), b.getNumerator());
return a*c;
Rational operator/(Rational const& a, Rational const& b)
{
Rational c(b.getDenominator(), b.getNumerator());
return a * c;
}

Rational operator/= (Rational& a, Rational const & b) {
return a = a / b;
Rational operator/=(Rational& a, Rational const& b)
{
return a = a / b;
}

/*-------------------------------------------------------------------------------*/
/* [logic operators] */
/*-------------------------------------------------------------------------------*/
bool operator< (Rational const & a, Rational const & b) {
return a.getNumerator()*b.getDenominator() < b.getNumerator()*a.getDenominator();
bool operator<(Rational const& a, Rational const& b)
{
return a.getNumerator() * b.getDenominator() <
b.getNumerator() * a.getDenominator();
}

bool operator<= (Rational const & a, Rational const & b) {
return a.getNumerator()*b.getDenominator() <= b.getNumerator()*a.getDenominator();
bool operator<=(Rational const& a, Rational const& b)
{
return a.getNumerator() * b.getDenominator() <=
b.getNumerator() * a.getDenominator();
}

bool operator> (Rational const & a, Rational const & b) {
return b < a;
bool operator>(Rational const& a, Rational const& b)
{
return b < a;
}

bool operator>= (Rational const & a, Rational const & b) {
return b <= a;
bool operator>=(Rational const& a, Rational const& b)
{
return b <= a;
}

bool operator== (Rational const & a, Rational const & b) {
return (a.getNumerator() == b.getNumerator() && a.getDenominator() == b.getDenominator());
bool operator==(Rational const& a, Rational const& b)
{
return (a.getNumerator() == b.getNumerator() &&
a.getDenominator() == b.getDenominator());
}

bool operator!= (Rational const & a, Rational const & b) {
return !(a == b);
bool operator!=(Rational const& a, Rational const& b)
{
return !(a == b);
}
22 changes: 11 additions & 11 deletions CS1410_CS2/1-RationalNumber/Rational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ std::istream& operator>> (std::istream& i, Rational& a);
class Rational {

public:
Rational(int n = 0, int d = 1);
Rational(std::string s){std::stringstream ss { s }; ss>>*this;}
int getNumerator() const;
int getDenominator() const;
void setNumerator(int n);
void setDenominator(int d);
void simplify();
Rational(int n = 0, int d = 1);
Rational(std::string s){std::stringstream ss { s }; ss >> *this;}
int getNumerator() const;
int getDenominator() const;
void setNumerator(int n);
void setDenominator(int d);
void simplify();

operator float();
operator double();
operator float();
operator double();

private:
int numerator;
int denominator;
int numerator;
int denominator;

};

Expand Down

0 comments on commit 62a715e

Please sign in to comment.