You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.2 KiB

#include <iostream>
#include <string>
using namespace std;
class fraction {
private:
signed int n = 0;
signed int d = 0;
int gcd (int a, int b) const {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int get_precision(double a) {
string s = to_string(a);
bool dec = false;
int count = 0;
for(int i = 0; i < s.length(); i++) {
if(s[i] == '.') {
dec = true;
} else {
if(dec) {
count++;
}
}
}
return count;
}
public:
// Constructors
fraction() {
n = 0;
d = 1;
}
fraction(int a, int b) {
int hcf = gcd(a,b);
n = a / hcf;
d = b / hcf;
}
fraction(int a) {
n = a;
d = 1;
}
fraction(double dec) {
int precision = get_precision(dec);
int denominator = 1;
for(int i = 0; i < precision; i++) {
denominator *= 10;
}
int numerator = dec * denominator;
int hcf = gcd(numerator,denominator);
n = numerator / hcf;
d = denominator / hcf;
}
fraction(const fraction &q) {
int hcf = gcd(q.n,q.d);
n = q.n / hcf;
d = q.d / hcf;
}
// Member functions
bool sign() const {
return (n >= 0);
}
int get_n() const{
return n;
}
int get_d() const{
return d;
}
// Operators
fraction operator+(const fraction &that) const {
int numerator = this->n * that.d + that.n * this->d;
int denominator = this->d * that.d;
fraction q(numerator, denominator);
return q;
}
fraction operator-(const fraction &that) const {
int numerator = this->n * that.d - that.n * this->d;
int denominator = this->d * that.d;
fraction q(numerator, denominator);
return q;
}
fraction operator*(const fraction &that) const {
int numerator = this->n * that.n;
int denominator = this->d * that.d;
fraction q(numerator, denominator);
return q;
}
fraction operator*(const int i) const {
int numerator = this->n * i;
int denominator = this->d;
fraction q(numerator, denominator);
return q;
}
fraction operator/(const fraction &that) const {
fraction a(this->n,this->d);
fraction b(that.d,that.n);
return a * b;
}
friend ostream &operator<<( ostream &os, const fraction &q ) {
if( q.d == q.n) {
os << 1;
} else if(q.d == 1) {
os << q.n;
} else {
os << q.n << '/' << q.d;
}
return os;
}
void operator=(const fraction &q ) {
n = q.n;
d = q.d;
}
void operator=(const int i ) {
n = i;
d = 1;
}
void operator=(const double dec ) {
const fraction q(dec);
*this = q;
}
bool operator>(const fraction &q ) const {
int hcf = gcd(d,q.d);
fraction a(*this * hcf);
fraction b(q * hcf);
return (a.n > b.n);
}
bool operator>(const int i ) const {
fraction q(i);
return (*this > q);
}
bool operator==(const fraction &q ) const {
return ((n == q.n) && (d == q.d));
}
bool operator==(const int i) const {
return ((n == i) && (d == 1));
}
bool operator==(const double dec) const {
fraction a(dec);
return ((n == a.n) && (d == a.d));
}
bool operator!=(const fraction &q ) const {
return ((n != q.n) || (d != q.d));
}
bool operator!=(const int i) const {
return ((n != i) || (d != 1));
}
bool operator!=(const double dec) const {
fraction a(dec);
return ((n != a.n) || (d != a.d));
}
};