Skip to content

Instantly share code, notes, and snippets.

@boki1
Created June 18, 2019 18:47
Show Gist options
  • Save boki1/4bcc3cf3b31c966d202c14658ad841ac to your computer and use it in GitHub Desktop.
Save boki1/4bcc3cf3b31c966d202c14658ad841ac to your computer and use it in GitHub Desktop.
Fraction class
#include <iostream>
#include "fraction.h"
namespace num
{
fraction::fraction(int n, int d) : nom(n), denom(d) { (*this).shorten(); }
fraction::fraction(const fraction &f) : denom(f.denom), nom(f.nom) {}
fraction &fraction::set(int n, int d)
{
this->nom = n;
this->denom = d;
return *this;
}
int fraction::get_nom() const
{
return this->nom;
}
int fraction::get_denom() const
{
return this->nom;
}
int gcd(int a, int b)
{
if (!b)
return a;
return gcd(b, a % b);
}
int lcd(int a, int b)
{
return a / gcd(a, b) * b;
}
fraction &fraction::operator=(const fraction &other)
{
this->nom = other.nom;
this->denom = other.denom;
return *this;
}
fraction fraction::operator+(const fraction &other)
{
int _lcd = lcd(other.denom, this->denom);
return fraction(this->nom * _lcd / this->denom + other.nom * _lcd / other.denom, _lcd).shorten();
}
fraction fraction::operator-(const num::fraction &other)
{
int _lcd = lcd(other.denom, this->denom);
return fraction(this->nom * _lcd / this->denom - other.nom * _lcd / other.denom, _lcd).shorten();
}
fraction fraction::operator*(const num::fraction &other)
{
return (fraction(this->nom * other.nom, this->denom * other.denom).shorten());
}
fraction fraction::operator/(const num::fraction &other)
{
return fraction::operator*(fraction(other.denom, other.nom)).shorten();
}
std::ostream &operator<<(std::ostream &l, const fraction &This)
{
l << This.nom;
if (This.nom != 0 && This.denom != 1)
return l << '/' << This.denom;
return l ;
}
std::istream &operator>>(std::istream &l, fraction &This)
{
return l >> This.nom >> This.denom;
}
fraction fraction::expand(int x)
{
return fraction(this->nom * x, this->denom * x);
}
fraction &fraction::shorten()
{
while (true)
{
int _gcd = gcd(this->nom, this->denom);
if (_gcd == 1)
break;
this->nom /= _gcd;
this->denom /= _gcd;
}
return *this;
}
bool fraction::operator<(const fraction &other) const
{
int _lcd = lcd(this->denom, other.denom);
return (this->nom * _lcd / this->denom) > (other.nom * _lcd / other.denom);
}
bool fraction::operator>(const fraction &other) const
{
return other < *this;
}
bool fraction::operator>=(const fraction &other) const
{
return !(*this < other);
}
bool fraction::operator<=(const fraction &other) const
{
return !(*this > other);
}
bool fraction::operator!=(const fraction &other) const
{
return *this < other || *this > other;
}
bool fraction::operator==(const fraction &other) const
{
return !(*this != other);
}
void init()
{
fraction a(1, 2), b(2, 4);
std::cout << "Original: " << a << ' ' << b << '\n';
std::cout << "ADD: " << a + b << '\n';
std::cout << "SUB: " << a - b << '\n';
std::cout << "MUL: " << a * b << '\n';
std::cout << "DIV: " << a / b << '\n';
std::cout << (a > b ? "a > b" : "") << '\n';
std::cout << (a < b ? "a < b" : "") << '\n';
std::cout << (a == b ? "a == b" : "") << '\n';
std::cout << (a != b ? "a != b" : "") << '\n';
std::cout << (a >= b ? "a >= b" : "") << '\n';
std::cout << (a <= b ? "a <= b" : "") << '\n';
}
}
int main()
{
num::init();
return 0;
}
#include <iostream>
namespace num
{
struct fraction
{
explicit fraction(int n = 0, int d = 1);
fraction(const fraction &f);
fraction &set(int n = 0, int d = 1);
int get_nom() const;
int get_denom() const;
fraction &operator=(const fraction &);
fraction operator+(const fraction &);
fraction operator*(const fraction &);
fraction operator-(const fraction &);
fraction operator/(const fraction &);
bool operator<(const fraction &) const;
bool operator>(const fraction &) const;
bool operator<=(const fraction &) const;
bool operator>=(const fraction &) const;
bool operator==(const fraction &) const;
bool operator!=(const fraction &) const;
friend std::ostream &operator<<(std::ostream &, const fraction &);
friend std::istream &operator>>(std::istream &, fraction &);
fraction &shorten(void);
fraction expand(int);
friend int gcd(int, int);
friend int lcd(int, int);
private:
int nom;
int denom;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment