|
|
|
@ -4,9 +4,9 @@
|
|
|
|
|
|
|
|
|
|
class fraction {
|
|
|
|
|
private:
|
|
|
|
|
uint64_t n = 0;
|
|
|
|
|
uint64_t d = 1;
|
|
|
|
|
uint64_t gcd(uint64_t a, uint64_t b) const {
|
|
|
|
|
int64_t n = 0;
|
|
|
|
|
int64_t d = 1;
|
|
|
|
|
int64_t gcd(int64_t a, int64_t b) const {
|
|
|
|
|
if (b == 0) {
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
@ -37,19 +37,19 @@ public:
|
|
|
|
|
// Constructors
|
|
|
|
|
fraction() {}
|
|
|
|
|
fraction(const fraction &q) {
|
|
|
|
|
uint64_t hcf = gcd(q.n, q.d);
|
|
|
|
|
int64_t hcf = gcd(q.n, q.d);
|
|
|
|
|
n = q.n / hcf;
|
|
|
|
|
d = q.d / hcf;
|
|
|
|
|
}
|
|
|
|
|
fraction(uint64_t a, uint64_t b) {
|
|
|
|
|
uint64_t hcf = gcd(a, b);
|
|
|
|
|
fraction(int64_t a, int64_t b) {
|
|
|
|
|
int64_t hcf = gcd(a, b);
|
|
|
|
|
n = a / hcf;
|
|
|
|
|
d = b / hcf;
|
|
|
|
|
}
|
|
|
|
|
fraction(int a, int b) {
|
|
|
|
|
uint64_t hcf = gcd(a, b);
|
|
|
|
|
n = (uint64_t)a / hcf;
|
|
|
|
|
d = (uint64_t)b / hcf;
|
|
|
|
|
int64_t hcf = gcd(a, b);
|
|
|
|
|
n = (int64_t)a / hcf;
|
|
|
|
|
d = (int64_t)b / hcf;
|
|
|
|
|
}
|
|
|
|
|
fraction(int a) {
|
|
|
|
|
fraction q(a, 1);
|
|
|
|
@ -58,11 +58,11 @@ public:
|
|
|
|
|
}
|
|
|
|
|
fraction(double dec) {
|
|
|
|
|
int precision = get_precision(dec);
|
|
|
|
|
uint64_t denominator = 1;
|
|
|
|
|
int64_t denominator = 1;
|
|
|
|
|
for (int i = 0; i < precision; i++) {
|
|
|
|
|
denominator *= 10;
|
|
|
|
|
}
|
|
|
|
|
uint64_t numerator = dec * denominator;
|
|
|
|
|
int64_t numerator = dec * denominator;
|
|
|
|
|
fraction q(numerator, denominator);
|
|
|
|
|
n = q.n;
|
|
|
|
|
d = q.d;
|
|
|
|
@ -70,34 +70,34 @@ public:
|
|
|
|
|
~fraction() {}
|
|
|
|
|
// Member functions
|
|
|
|
|
int get_sign() const { return (!(n >= 0) != !(d >= 0)) ? -1 : 1; }
|
|
|
|
|
uint64_t get_n() const { return this->n; }
|
|
|
|
|
uint64_t get_d() const { return this->d; }
|
|
|
|
|
int64_t get_n() const { return this->n; }
|
|
|
|
|
int64_t get_d() const { return this->d; }
|
|
|
|
|
double to_double() const {
|
|
|
|
|
double dec = (double)n / (double)d;
|
|
|
|
|
return dec;
|
|
|
|
|
}
|
|
|
|
|
// Operators
|
|
|
|
|
fraction operator+(const fraction &that) const {
|
|
|
|
|
uint64_t numerator = this->n * that.d + that.n * this->d;
|
|
|
|
|
uint64_t denominator = this->d * that.d;
|
|
|
|
|
int64_t numerator = this->n * that.d + that.n * this->d;
|
|
|
|
|
int64_t denominator = this->d * that.d;
|
|
|
|
|
fraction q(numerator, denominator);
|
|
|
|
|
return q;
|
|
|
|
|
}
|
|
|
|
|
fraction operator-(const fraction &that) const {
|
|
|
|
|
uint64_t numerator = this->n * that.d - that.n * this->d;
|
|
|
|
|
uint64_t denominator = this->d * that.d;
|
|
|
|
|
int64_t numerator = this->n * that.d - that.n * this->d;
|
|
|
|
|
int64_t denominator = this->d * that.d;
|
|
|
|
|
fraction q(numerator, denominator);
|
|
|
|
|
return q;
|
|
|
|
|
}
|
|
|
|
|
fraction operator*(const fraction &that) const {
|
|
|
|
|
uint64_t numerator = this->n * that.n;
|
|
|
|
|
uint64_t denominator = this->d * that.d;
|
|
|
|
|
int64_t numerator = this->n * that.n;
|
|
|
|
|
int64_t denominator = this->d * that.d;
|
|
|
|
|
fraction q(numerator, denominator);
|
|
|
|
|
return q;
|
|
|
|
|
}
|
|
|
|
|
fraction operator*(const int i) const {
|
|
|
|
|
uint64_t numerator = this->n * i;
|
|
|
|
|
uint64_t denominator = this->d;
|
|
|
|
|
int64_t numerator = this->n * i;
|
|
|
|
|
int64_t denominator = this->d;
|
|
|
|
|
fraction q(numerator, denominator);
|
|
|
|
|
return q;
|
|
|
|
|
}
|
|
|
|
@ -107,8 +107,8 @@ public:
|
|
|
|
|
return a * b;
|
|
|
|
|
}
|
|
|
|
|
friend std::ostream &operator<<(std::ostream &os, const fraction &q) {
|
|
|
|
|
uint64_t num = q.n;
|
|
|
|
|
uint64_t den = q.d;
|
|
|
|
|
int64_t num = q.n;
|
|
|
|
|
int64_t den = q.d;
|
|
|
|
|
std::string s = "";
|
|
|
|
|
if (q.n < 0) {
|
|
|
|
|
s = "-";
|
|
|
|
@ -135,8 +135,8 @@ public:
|
|
|
|
|
this->d = q.d;
|
|
|
|
|
}
|
|
|
|
|
void operator=(const int i) {
|
|
|
|
|
this->n = (uint64_t)i;
|
|
|
|
|
this->d = (uint64_t)1;
|
|
|
|
|
this->n = (int64_t)i;
|
|
|
|
|
this->d = (int64_t)1;
|
|
|
|
|
}
|
|
|
|
|
void operator=(const double dec) {
|
|
|
|
|
const fraction q(dec);
|
|
|
|
@ -144,7 +144,7 @@ public:
|
|
|
|
|
this->d = q.d;
|
|
|
|
|
}
|
|
|
|
|
bool operator>(const fraction &q) const {
|
|
|
|
|
uint64_t hcf = gcd(d, q.d);
|
|
|
|
|
int64_t hcf = gcd(d, q.d);
|
|
|
|
|
fraction a(*this * hcf);
|
|
|
|
|
fraction b(q * hcf);
|
|
|
|
|
return (a.n > b.n);
|
|
|
|
|