Much better gcd, actual Euclids algorithm

main
Micke Nordin 5 years ago
parent 3878684f85
commit 75d8d7a152

@ -5,23 +5,13 @@ using namespace std;
class fraction { class fraction {
private: private:
int n, d; signed int n = 0;
signed int d = 0;
int gcd (int a, int b) const { int gcd (int a, int b) const {
if (a == 0) {
return b;
}
if (b == 0) { if (b == 0) {
return a; return a;
} }
if (a == b) { return gcd(b, a % b);
return a;
}
if (a > b) {
return gcd(a-b, b);
}
return gcd(a, b-a);
} }
int get_precision(double a) { int get_precision(double a) {
string s = to_string(a); string s = to_string(a);
@ -73,6 +63,12 @@ class fraction {
bool sign() const { bool sign() const {
return (n >= 0); return (n >= 0);
} }
int get_n() const{
return n;
}
int get_d() const{
return d;
}
// Operators // Operators
fraction operator+(const fraction &that) const { fraction operator+(const fraction &that) const {
int numerator = this->n * that.d + that.n * this->d; int numerator = this->n * that.d + that.n * this->d;
@ -93,8 +89,9 @@ class fraction {
return q; return q;
} }
fraction operator*(const int i) const { fraction operator*(const int i) const {
fraction a(i); int numerator = this->n * i;
fraction q(*this * a); int denominator = this->d;
fraction q(numerator, denominator);
return q; return q;
} }
fraction operator/(const fraction &that) const { fraction operator/(const fraction &that) const {
@ -114,7 +111,7 @@ class fraction {
} }
void operator=(const fraction &q ) { void operator=(const fraction &q ) {
n = q.n; n = q.n;
d = q.n; d = q.d;
} }
void operator=(const int i ) { void operator=(const int i ) {
n = i; n = i;

Loading…
Cancel
Save